mirror of
https://github.com/vercel/commerce.git
synced 2025-03-14 14:42:31 +00:00
update product normalization
This commit is contained in:
parent
d489f59171
commit
79ed72a710
@ -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,
|
||||
}
|
||||
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user