4
0
forked from crowetic/commerce
B 3a7d5e2489
Latest Release (#187)
* Remove duplicated css rules. (#185)

* Fix typo in the Marquee component (#176)

Co-authored-by: Hugo Lopes <hugo.lopes@present-technologies.com>

* Remove duplicated css rules.
Fix invalid JSX props.

Co-authored-by: Hugo Lopes <hugo.lopes@present-technologies.com>

* Fix the body scroll when the sidebar is open (#184)

* Fix typo in the Marquee component (#176)

Co-authored-by: Hugo Lopes <hugo.lopes@present-technologies.com>

* Fix the body scroll when the sidebar is open

Co-authored-by: Hugo Lopes <hugo.lopes@present-technologies.com>

* Remove duplicate class in the I18nWidget comp (#183)

* Fix typo in the Marquee component (#176)

Co-authored-by: Hugo Lopes <hugo.lopes@present-technologies.com>

* Remove duplicate class name in the I18nWidget comp

This PR removes a duplicate class name in the I18nWidget component.

Co-authored-by: Hugo Lopes <hugo.rodrigues.lopes@gmail.com>
Co-authored-by: Hugo Lopes <hugo.lopes@present-technologies.com>

* add horizontal margin to pages when mobile screen (#180)

* Add cart item options like color and size (#177)

Co-authored-by: hlopes <hugo.lopes@present-technologies.com>

* Changes

Co-authored-by: Hugo Lopes <hugo.rodrigues.lopes@gmail.com>
Co-authored-by: Hugo Lopes <hugo.lopes@present-technologies.com>
Co-authored-by: Jamie Isaksen <jamie@jamie.no>
Co-authored-by: Vinicius Zucatti <51221635+vczb@users.noreply.github.com>
2021-01-29 12:00:16 -03:00

57 lines
1.7 KiB
TypeScript

import type { ProductNode } from '@framework/api/operations/get-product'
export type SelectedOptions = {
size: string | null
color: string | null
}
export type ProductOption = {
displayName: string
values: any
}
// Returns the available options of a product
export function getProductOptions(product: ProductNode) {
const options = product.productOptions.edges?.reduce<ProductOption[]>(
(arr, edge) => {
if (edge?.node.__typename === 'MultipleChoiceOption') {
arr.push({
displayName: edge.node.displayName.toLowerCase(),
values: edge.node.values.edges?.map((edge) => edge?.node),
})
}
return arr
},
[]
)
return options
}
// Finds a variant in the product that matches the selected options
export function getCurrentVariant(product: ProductNode, opts: SelectedOptions) {
const variant = product.variants.edges?.find((edge) => {
const { node } = edge ?? {}
const numberOfDefinedOpts = Object.values(opts).filter(value => value !== null).length;
const numberOfEdges = node?.productOptions?.edges?.length;
const isEdgeEqualToOption = ([key, value]:[string, string | null]) =>
node?.productOptions.edges?.find((edge) => {
if (
edge?.node.__typename === 'MultipleChoiceOption' &&
edge.node.displayName.toLowerCase() === key
) {
return edge.node.values.edges?.find(
(valueEdge) => valueEdge?.node.label === value
)
}
});
return numberOfDefinedOpts === numberOfEdges ?
Object.entries(opts).every(isEdgeEqualToOption)
: Object.entries(opts).some(isEdgeEqualToOption)
})
return variant ?? product.variants.edges?.[0]
}