Added the options for a variant to the product page

This commit is contained in:
royderks 2021-04-20 15:58:40 +02:00 committed by Zaiste
parent c26c6889b4
commit e3a9fd6c51
No known key found for this signature in database
GPG Key ID: 15DF7EBC7F2FFE35
3 changed files with 60 additions and 56 deletions

View File

@ -9,7 +9,9 @@ export function getVariant(product: Product, opts: SelectedOptions) {
option.__typename === 'MultipleChoiceOption' && option.__typename === 'MultipleChoiceOption' &&
option.displayName.toLowerCase() === key.toLowerCase() option.displayName.toLowerCase() === key.toLowerCase()
) { ) {
return option.values.find((v) => v.label.toLowerCase() === value) return option.values.find((v) => {
return v.label.toLowerCase() === value
})
} }
}) })
) )

View File

@ -5,6 +5,7 @@ import {
Checkout, Checkout,
CheckoutLine, CheckoutLine,
Money, Money,
ProductVariant,
} from '../schema' } from '../schema'
import type { Cart, LineItem } from '../types' import type { Cart, LineItem } from '../types'
@ -19,39 +20,36 @@ const money = ({ amount, currency }: Money) => {
} }
} }
const normalizeProductOptions = (options: ProductOption[]) => { const normalizeProductOptions = (options: ProductVariant[]) => {
return options?.map(({ id, name: displayName, values }) => ({ const optionNames = options
.map((option) => {
// can a variant have multiple attributes?
return option.attributes[0].attribute.name
})
.filter((x, i, a) => a.indexOf(x) == i)
return optionNames.map((displayName) => {
const matchedOptions = options.filter(
({ attributes }) => attributes[0].attribute.name === displayName // can a variant have multiple attributes?
)
return {
__typename: 'MultipleChoiceOption', __typename: 'MultipleChoiceOption',
id, id: 123,
displayName, // next-commerce can only display labels for options with displayName 'size', or colors
// values: values.map((value) => { displayName: displayName?.toLowerCase().includes('size')
// let output: any = { ? 'size'
// label: value, : displayName,
// } values: matchedOptions.map(({ name }) => ({
// if (displayName.match(/colou?r/gi)) { label: name,
// output = { })),
// ...output, }
// hexColors: [value], })
// }
// }
// return output
// })
values: [],
}))
} }
const normalizeProductImages = (images: any) => const normalizeProductVariants = (variants: ProductVariant[]) =>
images.map(({ node: { originalSrc: url, ...rest } }) => ({ variants?.map(({ id, sku, name, pricing }) => {
url, const price = pricing?.price?.net && money(pricing.price.net)?.value
...rest,
}))
const normalizeProductVariants = (variants: any) => {
return variants?.map(
({ id, selectedOptions, sku, name, priceV2, pricing }) => {
const price = money(pricing?.price?.net)?.value
console.log({ price })
return { return {
id, id,
@ -60,19 +58,9 @@ const normalizeProductVariants = (variants: any) => {
price, price,
listPrice: price, listPrice: price,
requiresShipping: true, requiresShipping: true,
// options: selectedOptions.map(({ name, value }: SelectedOption) => { options: normalizeProductOptions(variants),
// const options = normalizeProductOption({
// id,
// name,
// values: [value],
// })
// return options
// }),
options: [],
} }
} })
)
}
export function normalizeProduct(productNode: SaleorProduct): Product { export function normalizeProduct(productNode: SaleorProduct): Product {
const { const {
@ -83,22 +71,28 @@ export function normalizeProduct(productNode: SaleorProduct): Product {
description, description,
slug, slug,
pricing, pricing,
// options,
...rest ...rest
} = productNode } = productNode
const { blocks } = JSON.parse(description)
const product = { const product = {
id, id,
name, name,
vendor: '', vendor: '',
description, description: blocks[0]?.data.text,
path: `/${slug}`, path: `/${slug}`,
slug: slug?.replace(/^\/+|\/+$/g, ''), slug: slug?.replace(/^\/+|\/+$/g, ''),
price: money(pricing?.priceRange?.start?.net) || 0, price:
(pricing?.priceRange?.start?.net &&
money(pricing.priceRange.start.net)) ||
0,
// TODO: Check nextjs-commerce bug if no images are added for a product // TODO: Check nextjs-commerce bug if no images are added for a product
images: media?.length ? media : [{ url: placeholderImg }], images: media?.length ? media : [{ url: placeholderImg }],
variants: variants ? normalizeProductVariants(variants) : [], variants:
options: variants ? normalizeProductOptions(variants) : [], variants && variants.length > 0 ? normalizeProductVariants(variants) : [],
options:
variants && variants.length > 0 ? normalizeProductOptions(variants) : [],
...rest, ...rest,
} }

View File

@ -17,6 +17,14 @@ const getProductQuery = /* GraphQL */ `
variants { variants {
id id
name name
attributes {
attribute {
name
}
values {
name
}
}
pricing { pricing {
price { price {
net { net {