mirror of
https://github.com/vercel/commerce.git
synced 2025-03-31 17:25:53 +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,
|
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 {
|
return {
|
||||||
products,
|
products,
|
||||||
}
|
}
|
||||||
|
@ -19,9 +19,12 @@ const getProduct = async (options: {
|
|||||||
config = getConfig(config)
|
config = getConfig(config)
|
||||||
|
|
||||||
const product = await config.fetchSwell('products', 'get', [variables.slug])
|
const product = await config.fetchSwell('products', 'get', [variables.slug])
|
||||||
|
if (product.variants) {
|
||||||
|
product.variants = product.variants?.results
|
||||||
|
}
|
||||||
|
// console.log('product', product)
|
||||||
return {
|
return {
|
||||||
product: product ? normalizeProduct(product) : null,
|
product: normalizeProduct(product),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +35,12 @@ export const handler: SWRHook<
|
|||||||
variables: { category: categoryId, search },
|
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 {
|
return {
|
||||||
products,
|
products,
|
||||||
|
@ -21,9 +21,21 @@ export type SwellCart = {
|
|||||||
// TODO: add missing fields
|
// 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
|
name: string
|
||||||
slug: string
|
slug: string
|
||||||
|
currency: string
|
||||||
|
price: number
|
||||||
|
images: any[]
|
||||||
|
options: any[]
|
||||||
|
variants: any[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SwellCustomer extends Core.Customer {
|
export interface SwellCustomer extends Core.Customer {
|
||||||
|
@ -69,12 +69,16 @@ const normalizeProductImages = (images) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const normalizeProductVariants = (variants) => {
|
const normalizeProductVariants = (variants) => {
|
||||||
return variants?.map(({ id, name, values, price, sku }) => {
|
return variants?.map(({ id, name, price, sku }) => {
|
||||||
const options = values.map((option: SelectedOption) => {
|
const values = name
|
||||||
|
.split(',')
|
||||||
|
.map((i) => ({ name: i.trim(), label: i.trim() }))
|
||||||
|
|
||||||
|
const options = values.map((value) => {
|
||||||
return normalizeProductOption({
|
return normalizeProductOption({
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
values: option ? [option] : [],
|
values: [value],
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -90,22 +94,30 @@ const normalizeProductVariants = (variants) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function normalizeProduct(productNode: SwellProduct): Product {
|
export function normalizeProduct(swellProduct: SwellProduct): Product {
|
||||||
const { images, options, slug, price } = productNode
|
const {
|
||||||
|
id,
|
||||||
|
description,
|
||||||
|
images,
|
||||||
|
options,
|
||||||
|
slug,
|
||||||
|
variants,
|
||||||
|
price: value,
|
||||||
|
currency: currencyCode,
|
||||||
|
} = swellProduct
|
||||||
const productOptions = options
|
const productOptions = options
|
||||||
? options.map((o) => normalizeProductOption(o))
|
? options.map((o) => normalizeProductOption(o))
|
||||||
: []
|
: []
|
||||||
const productVariants = normalizeProductVariants(
|
const productVariants = variants ? normalizeProductVariants(variants) : []
|
||||||
options.filter((option) => option.variant)
|
|
||||||
)
|
|
||||||
|
|
||||||
// ProductView.tsx assumes the existence of at least one product variant
|
// ProductView.tsx assumes the existence of at least one product variant
|
||||||
const emptyVariants = [{ options: [{ id: 123 }] }]
|
const emptyVariants = [{ options: [{ id: 123 }] }]
|
||||||
const productImages = normalizeProductImages(images)
|
const productImages = normalizeProductImages(images)
|
||||||
|
|
||||||
const product = {
|
const product = {
|
||||||
...productNode,
|
...swellProduct,
|
||||||
vendor: 'our brands',
|
description,
|
||||||
|
id,
|
||||||
|
vendor: '',
|
||||||
path: `/${slug}`,
|
path: `/${slug}`,
|
||||||
images: productImages,
|
images: productImages,
|
||||||
variants:
|
variants:
|
||||||
@ -113,8 +125,11 @@ export function normalizeProduct(productNode: SwellProduct): Product {
|
|||||||
? productVariants
|
? productVariants
|
||||||
: emptyVariants,
|
: emptyVariants,
|
||||||
options: productOptions,
|
options: productOptions,
|
||||||
|
price: {
|
||||||
|
value,
|
||||||
|
currencyCode,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return product
|
return product
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user