mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 06:56:59 +00:00
normalize option and variant
This commit is contained in:
parent
e4dad2f8f4
commit
5e9300af5f
@ -1,20 +1,26 @@
|
||||
import {
|
||||
Product,
|
||||
ProductImage,
|
||||
ProductOption,
|
||||
ProductOptionValues,
|
||||
ProductPrice,
|
||||
ProductVariant,
|
||||
} from '@vercel/commerce/types/product'
|
||||
import {
|
||||
SyliusProduct,
|
||||
SyliusProductImage,
|
||||
SyliusProductOption,
|
||||
SyliusProductOptionValues,
|
||||
SyliusProductVariant,
|
||||
} from '../types'
|
||||
} from '../types/products'
|
||||
|
||||
export const normalizeProduct = (product: SyliusProduct): Product => {
|
||||
return {
|
||||
id: product.id.toString(),
|
||||
name: product.name,
|
||||
description: product.description,
|
||||
description: product.shortDescription,
|
||||
descriptionHtml: product.description,
|
||||
slug: product.slug,
|
||||
images: product.images.map((image) => normalizeProductImage(image)),
|
||||
variants: product.variants.map((variant) =>
|
||||
normalizeProductVariant(variant)
|
||||
@ -23,20 +29,50 @@ export const normalizeProduct = (product: SyliusProduct): Product => {
|
||||
product.variants[0].price,
|
||||
product.variants[0].originalPrice
|
||||
),
|
||||
options: [],
|
||||
options: product.options.map((option) => normalizeProductOption(option)),
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeProductVariant = (
|
||||
variant: SyliusProductVariant
|
||||
): ProductVariant => {
|
||||
const options =
|
||||
variant.optionValues.length > 0
|
||||
? [
|
||||
{
|
||||
id: '',
|
||||
displayName: '',
|
||||
values: variant.optionValues.map((optionValue) =>
|
||||
normalizeProductOptionValue(optionValue)
|
||||
),
|
||||
},
|
||||
]
|
||||
: []
|
||||
return {
|
||||
id: variant.id,
|
||||
options: [],
|
||||
options: options,
|
||||
availableForSale: variant.inStock,
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeProductOption = (option: SyliusProductOption): ProductOption => {
|
||||
return {
|
||||
id: option.id.toString(),
|
||||
displayName: option.name,
|
||||
values: option.values.map((optionValue) =>
|
||||
normalizeProductOptionValue(optionValue)
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeProductOptionValue = (
|
||||
optionValue: SyliusProductOptionValues
|
||||
): ProductOptionValues => {
|
||||
return {
|
||||
label: optionValue.value ?? '',
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeProductImage = (image: SyliusProductImage): ProductImage => {
|
||||
return {
|
||||
url: process.env.NEXT_PUBLIC_SYLIUS_ALLOWED_IMAGE_URL + image.path,
|
||||
|
Loading…
x
Reference in New Issue
Block a user