diff --git a/components/product/ProductView/ProductView.tsx b/components/product/ProductView/ProductView.tsx index 46614bfa6..c4c8fccbb 100644 --- a/components/product/ProductView/ProductView.tsx +++ b/components/product/ProductView/ProductView.tsx @@ -7,6 +7,8 @@ import { Button, Container } from '@components/ui' import { Swatch, ProductSlider } from '@components/product' import useAddItem from '@lib/bigcommerce/cart/use-add-item' import type { Product } from '@lib/bigcommerce/api/operations/get-product' +import { getProductOptions } from '../helpers' + interface Props { className?: string children?: any @@ -22,6 +24,9 @@ const COLORS: Colors[] = ['pink', 'black', 'white'] const SIZES = ['s', 'm', 'l', 'xl', 'xxl'] const ProductView: FC = ({ product, className }) => { + const options = getProductOptions(product) + console.log(options) + const addItem = useAddItem() const { openSidebar } = useUI() @@ -100,41 +105,33 @@ const ProductView: FC = ({ product, className }) => {
-
-
-

Color

-
- {COLORS.map((color) => ( - - setChoices((choices) => { - return { ...choices, color } - }) - } - /> - ))} -
-
-
-

Size

-
- {SIZES.map((size) => { - return ( - - setChoices((choices) => ({ ...choices, size })) - } - /> - ) - })} -
-
+ +
+ {options?.map((opt) => ( +
+

{opt.displayName}

+
+ {opt.values.map((v: any) => { + return ( + + setChoices((choices) => { + return { + ...choices, + [opt.displayName.toLowerCase()]: v.label, + } + }) + } + /> + ) + })} +
+
+ ))}
= ({ className, size, color, active, ...props }) => { +const Swatch: FC = ({ + className, + label, + variant = 'size', + active, + ...props +}) => { + variant = variant?.toLowerCase() + label = label?.toLowerCase() + const rootClassName = cn( s.root, { [s.active]: active, - [s.size]: size, - [s.colorPink]: color === 'pink', - [s.colorWhite]: color === 'white', - [s.colorBlack]: color === 'black', - [s.colorViolet]: color === 'violet', + [s.size]: variant === 'size', + [s.colorPink]: label === 'pink', + [s.colorWhite]: label === 'white', + [s.colorBlack]: label === 'black', + [s.colorViolet]: label === 'violet', }, className ) return ( - ) } diff --git a/components/product/helpers.ts b/components/product/helpers.ts new file mode 100644 index 000000000..a67266b07 --- /dev/null +++ b/components/product/helpers.ts @@ -0,0 +1,10 @@ +import type { Product } from '@lib/bigcommerce/api/operations/get-product' + +export function getProductOptions(product: Product) { + const options = product.options.edges?.map(({ node }: any) => ({ + displayName: node.displayName, + values: node.values.edges?.map(({ node }: any) => node), + })) + + return options +}