Mapped options to variants

This commit is contained in:
royderks 2021-04-29 18:27:36 +02:00 committed by Zaiste
parent f7796b9683
commit 85d203e85c
No known key found for this signature in database
GPG Key ID: 15DF7EBC7F2FFE35
3 changed files with 36 additions and 28 deletions

View File

@ -119,7 +119,8 @@ const ProductView: FC<Props> = ({ product }) => {
<Swatch <Swatch
key={`${opt.id}-${i}`} key={`${opt.id}-${i}`}
active={v.label.toLowerCase() === active} active={v.label.toLowerCase() === active}
variant={opt.displayName} // variant={opt.displayName}
variant={opt.variant}
color={v.hexColors ? v.hexColors[0] : ''} color={v.hexColors ? v.hexColors[0] : ''}
label={v.label} label={v.label}
onClick={() => { onClick={() => {

View File

@ -22,10 +22,8 @@ const getProduct = async (options: {
variables, variables,
}) })
const { product } = data
return { return {
product: product ? normalizeProduct(product) : null, product: data?.product ? normalizeProduct(data.product) : null,
} }
} }

View File

@ -21,33 +21,41 @@ const money = ({ amount, currency }: Money) => {
} }
const normalizeProductOptions = (options: ProductVariant[]) => { const normalizeProductOptions = (options: ProductVariant[]) => {
const optionNames = options return options
.map((option) => { ?.map((option) => option?.attributes)
// can a variant have multiple attributes? .flat(1)
return option.attributes[0].attribute.name .reduce<any>((acc, x) => {
}) if (
.filter((x, i, a) => a.indexOf(x) == i) acc.find(({ displayName }: any) => displayName === x.attribute.name)
) {
return acc.map((opt: any) => {
return opt.displayName === x.attribute.name
? {
...opt,
values: [
...opt.values,
...x.values.map((value: any) => ({
label: value?.name,
})),
],
}
: opt
})
}
return optionNames.map((displayName) => { return acc.concat({
const matchedOptions = options.filter( __typename: 'MultipleChoiceOption',
({ attributes }) => attributes[0].attribute.name === displayName // can a variant have multiple attributes? displayName: x.attribute.name,
) variant: 'size',
values: x.values.map((value: any) => ({
return { label: value?.name,
__typename: 'MultipleChoiceOption', })),
// next-commerce can only display labels for options with displayName 'size', or colors })
displayName: displayName?.toLowerCase().includes('size') }, [])
? 'size'
: displayName,
values: matchedOptions.map(({ name }) => ({
label: name,
})),
}
})
} }
const normalizeProductVariants = (variants: ProductVariant[]) => const normalizeProductVariants = (variants: ProductVariant[]) => {
variants?.map((variant) => { return variants?.map((variant) => {
const { id, sku, name, pricing } = variant const { id, sku, name, pricing } = variant
const price = pricing?.price?.net && money(pricing.price.net)?.value const price = pricing?.price?.net && money(pricing.price.net)?.value
@ -61,6 +69,7 @@ const normalizeProductVariants = (variants: ProductVariant[]) =>
options: normalizeProductOptions([variant]), options: normalizeProductOptions([variant]),
} }
}) })
}
export function normalizeProduct(productNode: SaleorProduct): Product { export function normalizeProduct(productNode: SaleorProduct): Product {
const { const {