Update helpers.ts

This commit is contained in:
cond0r 2021-03-03 20:36:36 +02:00
parent 59dedf58cc
commit 639863bb35

View File

@ -8,18 +8,18 @@ export type SelectedOptions = {
export function getVariant(product: Product, opts: SelectedOptions) { export function getVariant(product: Product, opts: SelectedOptions) {
const variant = product.variants.find((variant) => { const variant = product.variants.find((variant) => {
return Object.entries(opts).every(([key, value]) => return Object.entries(opts).every(([key, value]) =>
variant.options.find((option) => { value
? variant.options.find((option) => {
if ( if (
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) => v.label.toLowerCase() === value)
} else if (!value) {
return !variant.options.find(
(v) => v.displayName.toLowerCase() === key
)
} }
}) })
: !variant.options.find(
(v) => v.displayName.toLowerCase() === key.toLowerCase()
)
) )
}) })
return variant return variant