From 79ed72a7102f5bb93f3374a8281796f3e8003b96 Mon Sep 17 00:00:00 2001 From: Greg Hoskin Date: Sun, 18 Apr 2021 19:33:23 -0500 Subject: [PATCH] update product normalization --- framework/swell/product/get-all-products.ts | 7 +++- framework/swell/product/get-product.ts | 7 ++-- framework/swell/product/use-search.tsx | 7 +++- framework/swell/types.ts | 14 +++++++- framework/swell/utils/normalize.ts | 39 ++++++++++++++------- 5 files changed, 57 insertions(+), 17 deletions(-) diff --git a/framework/swell/product/get-all-products.ts b/framework/swell/product/get-all-products.ts index e212a2494..1f4f4152c 100644 --- a/framework/swell/product/get-all-products.ts +++ b/framework/swell/product/get-all-products.ts @@ -26,7 +26,12 @@ const getAllProducts = async (options: { limit: variables.first, }, ]) - const products = results.map((product) => normalizeProduct(product)) ?? [] + const products = results.map((product) => { + if (product.variants) { + product.variants = product.variants.results + } + return normalizeProduct(product) ?? [] + }) return { products, } diff --git a/framework/swell/product/get-product.ts b/framework/swell/product/get-product.ts index a95610a10..49bc5b67f 100644 --- a/framework/swell/product/get-product.ts +++ b/framework/swell/product/get-product.ts @@ -19,9 +19,12 @@ const getProduct = async (options: { config = getConfig(config) const product = await config.fetchSwell('products', 'get', [variables.slug]) - + if (product.variants) { + product.variants = product.variants?.results + } + // console.log('product', product) return { - product: product ? normalizeProduct(product) : null, + product: normalizeProduct(product), } } diff --git a/framework/swell/product/use-search.tsx b/framework/swell/product/use-search.tsx index 204c65352..14b966319 100644 --- a/framework/swell/product/use-search.tsx +++ b/framework/swell/product/use-search.tsx @@ -35,7 +35,12 @@ export const handler: SWRHook< variables: { category: categoryId, search }, }) - const products = results.map((product) => normalizeProduct(product)) + const products = results.map((product) => { + if (product.variants) { + product.variants = product.variants?.results + } + return normalizeProduct(product) + }) return { products, diff --git a/framework/swell/types.ts b/framework/swell/types.ts index 0d1e85657..456b3da05 100644 --- a/framework/swell/types.ts +++ b/framework/swell/types.ts @@ -21,9 +21,21 @@ export type SwellCart = { // TODO: add missing fields } -export interface SwellProduct extends Core.Product { +export type VariantResult = { + id: string + option_value_ids: string[] +} + +export interface SwellProduct { + id: string + description: string name: string slug: string + currency: string + price: number + images: any[] + options: any[] + variants: any[] } export interface SwellCustomer extends Core.Customer { diff --git a/framework/swell/utils/normalize.ts b/framework/swell/utils/normalize.ts index f40a7d8ec..ea12acbae 100644 --- a/framework/swell/utils/normalize.ts +++ b/framework/swell/utils/normalize.ts @@ -69,12 +69,16 @@ const normalizeProductImages = (images) => { } const normalizeProductVariants = (variants) => { - return variants?.map(({ id, name, values, price, sku }) => { - const options = values.map((option: SelectedOption) => { + return variants?.map(({ id, name, price, sku }) => { + const values = name + .split(',') + .map((i) => ({ name: i.trim(), label: i.trim() })) + + const options = values.map((value) => { return normalizeProductOption({ id, name, - values: option ? [option] : [], + values: [value], }) }) @@ -90,22 +94,30 @@ const normalizeProductVariants = (variants) => { }) } -export function normalizeProduct(productNode: SwellProduct): Product { - const { images, options, slug, price } = productNode +export function normalizeProduct(swellProduct: SwellProduct): Product { + const { + id, + description, + images, + options, + slug, + variants, + price: value, + currency: currencyCode, + } = swellProduct const productOptions = options ? options.map((o) => normalizeProductOption(o)) : [] - const productVariants = normalizeProductVariants( - options.filter((option) => option.variant) - ) + const productVariants = variants ? normalizeProductVariants(variants) : [] // ProductView.tsx assumes the existence of at least one product variant const emptyVariants = [{ options: [{ id: 123 }] }] const productImages = normalizeProductImages(images) - const product = { - ...productNode, - vendor: 'our brands', + ...swellProduct, + description, + id, + vendor: '', path: `/${slug}`, images: productImages, variants: @@ -113,8 +125,11 @@ export function normalizeProduct(productNode: SwellProduct): Product { ? productVariants : emptyVariants, options: productOptions, + price: { + value, + currencyCode, + }, } - return product }