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