Skip to content

Commit

Permalink
feat(shoppable-image): basic product tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
Benj0s committed Nov 27, 2023
1 parent cc1757a commit 87ea259
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import { Tooltip } from '@mui/material';
import Link from 'next/link';
import React, { ReactElement } from 'react';
import { ShoppableProductTooltip } from './ShoppableProductTooltip';

export enum InteractableType {
PAGE = '.page',
LINK = '.link',
PRODUCT = '.product',
CATEGORY = '.category',
}

const urlBuilder = (selector: string, target: string) => {
let url = '#';
switch (selector) {
case '.page':
case InteractableType.PAGE:
url = `/${target}`;
break;
case '.link':
case InteractableType.LINK:
url = target;
break;
case '.product':
case InteractableType.PRODUCT:
url = `/product/${target}`;
break;
case '.category':
case InteractableType.CATEGORY:
url = `/category/${target}`;
break;
default:
Expand All @@ -34,13 +42,26 @@ type ShoppableImageInteractableProps = {
};

const ShoppableImageInteractable = ({ children, selector, target }: ShoppableImageInteractableProps) => {
return (
<Link passHref href={urlBuilder(selector, target)}>
<Tooltip title={titleBuilder(selector, target)} followCursor>
{children}
</Tooltip>
</Link>
);
switch (selector) {
case InteractableType.PRODUCT: {
return (
<Link passHref href={urlBuilder(selector, target)}>
<ShoppableProductTooltip title={urlBuilder(selector, target)} target={target}>
{children}
</ShoppableProductTooltip>
</Link>
);
}
default: {
return (
<Link passHref href={urlBuilder(selector, target)}>
<Tooltip title={titleBuilder(selector, target)} followCursor>
{children}
</Tooltip>
</Link>
);
}
}
};

export default ShoppableImageInteractable;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Tooltip } from '@mui/material';
import { ReactElement, useEffect, useState } from 'react';
import { commerceApi } from '@pages/api';

type ShoppableProductTooltipProps = {
children: ReactElement;
title: string;
target: string;
};

export const ShoppableProductTooltip = ({ children, title, target }: ShoppableProductTooltipProps) => {
const [tooltip, setTooltip] = useState(title);

useEffect(() => {
commerceApi.getProduct({ id: target }).then((product) => {
setTooltip(`${product.name} - ${product.variants[0]?.listPrice}`);
});
});
return (
<Tooltip title={tooltip} followCursor>
{children}
</Tooltip>
);
};

0 comments on commit 87ea259

Please sign in to comment.