From 4fdaae2197238b695ab5627dca6694c5d2d3618c Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Mon, 11 Jan 2021 14:54:05 -0300 Subject: [PATCH] Normalized Options and Swatches --- components/product/ProductView/ProductView.tsx | 12 ++++-------- framework/bigcommerce/lib/normalize.ts | 17 +++++++++++++++-- framework/types.d.ts | 12 ++++++++++++ tsconfig.json | 8 +++++++- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/components/product/ProductView/ProductView.tsx b/components/product/ProductView/ProductView.tsx index 4f9844fcc..290af8b68 100644 --- a/components/product/ProductView/ProductView.tsx +++ b/components/product/ProductView/ProductView.tsx @@ -25,8 +25,6 @@ interface Props { } const ProductView: FC = ({ product }) => { - console.log(product) - const addItem = useAddItem() const { price } = usePrice({ amount: product.price.value, @@ -36,8 +34,6 @@ const ProductView: FC = ({ product }) => { const { openSidebar } = useUI() - // const options = getProductOptions(product) - const [loading, setLoading] = useState(false) const [choices, setChoices] = useState({ size: null, @@ -111,16 +107,16 @@ const ProductView: FC = ({ product }) => {
- {/* {options?.map((opt: any) => ( + {product.options?.map((opt) => (

{opt.displayName}

- {opt.values.map((v: any, i: number) => { + {opt.values.map((v, i: number) => { const active = (choices as any)[opt.displayName] return ( = ({ product }) => { })}
- ))} */} + ))}
diff --git a/framework/bigcommerce/lib/normalize.ts b/framework/bigcommerce/lib/normalize.ts index 73c477d9a..a2be86478 100644 --- a/framework/bigcommerce/lib/normalize.ts +++ b/framework/bigcommerce/lib/normalize.ts @@ -8,6 +8,7 @@ export function normalizeProduct(productNode: BCProduct): Product { productOptions, prices, path, + options: _, ...rest } = productNode @@ -29,8 +30,20 @@ export function normalizeProduct(productNode: BCProduct): Product { ...rest, })) : [], - productOptions: productOptions.edges - ? productOptions.edges.map(({ node }: any) => node) + options: productOptions.edges + ? productOptions.edges.map( + ({ + node: { + entityId, + values: { edges }, + ...rest + }, + }: any) => ({ + id: entityId, + values: edges.map(({ node }: any) => node), + ...rest, + }) + ) : [], price: { value: prices?.price.value, diff --git a/framework/types.d.ts b/framework/types.d.ts index b189d26b6..e44f23bbb 100644 --- a/framework/types.d.ts +++ b/framework/types.d.ts @@ -11,7 +11,19 @@ interface Product extends Entity { images: ProductImage[] variants: ProductVariant[] price: ProductPrice + options: ProductOption[] } + +interface ProductOption extends Entity { + displayName: string + values: ProductOptionValues[] +} + +interface ProductOptionValues { + label: string + hexColors?: string[] +} + interface ProductImage { url: string alt?: string diff --git a/tsconfig.json b/tsconfig.json index 98639f61e..c45e16c4d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -26,6 +26,12 @@ "@framework": ["framework/bigcommerce"] } }, - "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"], + "include": [ + "next-env.d.ts", + "./framework/types.d.ts", + "**/*.ts", + "**/*.tsx", + "**/*.js" + ], "exclude": ["node_modules"] }