update product normalization

This commit is contained in:
Greg Hoskin 2021-04-11 16:53:57 -05:00
parent 644f196c97
commit 7e0d126531

View File

@ -27,22 +27,24 @@ const normalizeProductOption = ({
name: displayName, name: displayName,
values, values,
}: ProductOption) => { }: ProductOption) => {
let returnValues = values.map((value) => {
let output: any = {
label: value.name,
}
if (displayName === 'Color') {
output = {
...output,
hexColors: [value.name],
}
}
return output
})
return { return {
__typename: 'MultipleChoiceOption', __typename: 'MultipleChoiceOption',
id, id,
displayName, displayName,
values: values.map((value) => { values: returnValues,
let output: any = {
label: value.name,
}
if (displayName === 'Color') {
output = {
...output,
hexColors: [value],
}
}
return output
}),
} }
} }
@ -60,56 +62,46 @@ const normalizeProductImages = (images) => {
} }
return images?.map(({ file, ...rest }: SwellImage) => ({ return images?.map(({ file, ...rest }: SwellImage) => ({
url: file?.url, url: file?.url,
height: file.height, height: file?.height,
width: file.width, width: file?.width,
...rest, ...rest,
})) }))
} }
const normalizeProductVariants = (variants) => { const normalizeProductVariants = (variants) => {
return variants?.map(({ id, name, values, price, stock_status }) => ({ return variants?.map(({ id, name, values, price, sku }) => ({
id, id,
name, name,
sku: sku ?? id, sku: sku ?? id,
price, price: price ?? null,
listPrice: price, listPrice: price ?? null,
// requiresShipping: true, // requiresShipping: true,
options: values.map(({ name, value }: SelectedOption) => options: values.map(({ name, value }: SelectedOption) =>
normalizeProductOption({ normalizeProductOption({
id, id,
name, name,
values: [value], values: value ? [value] : [],
}) })
), ),
})) }))
} }
export function normalizeProduct(productNode: SwellProduct): Product { export function normalizeProduct(productNode: SwellProduct): Product {
const { const { images, options, slug, price } = productNode
id,
name, const productOptions = options.map((o) => normalizeProductOption(o))
vendor, const productVariants = normalizeProductVariants(
images, options.filter((option) => option.variant)
variants, )
description, const productImages = normalizeProductImages(images)
slug,
price,
options,
...rest
} = productNode
const product = { const product = {
id, ...productNode,
name,
vendor: 'our brands', vendor: 'our brands',
description,
path: `/${slug}`, path: `/${slug}`,
slug, images: productImages ?? [],
price, variants: productVariants,
images: normalizeProductImages(images), options: productOptions,
variants: [], //variants ? normalizeProductVariants(options) : [],
options: options ? options.map((o) => normalizeProductOption(o)) : [],
...rest,
} }
return product return product
@ -158,7 +150,7 @@ function normalizeLineItem({
sku: variant?.sku ?? '', sku: variant?.sku ?? '',
name: variant?.name!, name: variant?.name!,
image: { image: {
url: product?.images[0].file.url, url: product.images ? product?.images[0].file.url : '',
}, },
requiresShipping: false, requiresShipping: false,
price: price, price: price,