From 4a18354cc01a0366f6c7484cbdad23eacda2b8f7 Mon Sep 17 00:00:00 2001 From: soniaklimas <57659625+soniaklimas@users.noreply.github.com> Date: Wed, 23 Oct 2024 10:35:09 +0200 Subject: [PATCH] fix: Fix disappearing product variants while clicking (#42) * Fix disappearing product variants * Improve if condition --- .../[slug]/components/variant-selector.tsx | 112 +++++++++--------- 1 file changed, 55 insertions(+), 57 deletions(-) diff --git a/apps/storefront/src/app/[locale]/(main)/products/[slug]/components/variant-selector.tsx b/apps/storefront/src/app/[locale]/(main)/products/[slug]/components/variant-selector.tsx index 9d13563..099bad9 100644 --- a/apps/storefront/src/app/[locale]/(main)/products/[slug]/components/variant-selector.tsx +++ b/apps/storefront/src/app/[locale]/(main)/products/[slug]/components/variant-selector.tsx @@ -58,7 +58,7 @@ const generateFullAttributeMap = (variants: ProductVariant[]) => { type: Attribute["type"]; values: AttributeValue[]; }) => { - if (slug && values) { + if (slug && values?.length > 0) { const attributeMatch = selectionAttributesMap.find( (attribute) => attribute.slug === slug, ); @@ -303,64 +303,62 @@ export const VariantSelector = ({

{getPrice()}

- {selectionAttributesMap - .filter(({ values }) => values.length > 0) - .map(({ slug, name, values }, index) => { - const arePreviousAttributesSelected = - index < selectedAttributes.length + 1; - const hasValues = values.length > 0; - - if (!(arePreviousAttributesSelected && hasValues)) { - return null; + {selectionAttributesMap.map(({ slug, name, values }, index) => { + const arePreviousAttributesSelected = + index < selectedAttributes.length + 1; + const hasValues = values.length > 0; + + if (!(arePreviousAttributesSelected && hasValues)) { + return null; + } + + const defaultValue = selectedAttributes.find((val) => { + if (val.slug === slug) { + return values.some((v) => v.slug === val.value); } - const defaultValue = selectedAttributes.find((val) => { - if (val.slug === slug) { - return values.some((v) => v.slug === val.value); - } - - return false; - })?.value; - - return ( -
- - { - setVariants([]); - setSelectedVariantId(""); - setSelectedAttributes((values) => { - if (index < values.length - 1) { - // Undo if someone selects one of the previous attribute. - values.splice(index, values.length - index, { - slug, - value: valueSlug, - }); - } else if (valueSlug) { - // Insert new attribute choice. - values.splice(index, 1, { slug, value: valueSlug }); - } else { - // Replace current attribute choice. - values.splice(index, 1); - } - - return [...values]; - }); - }} - > - {values.map(({ slug, name }) => ( - - {name} - - ))} - -
- ); - })} + return false; + })?.value; + + return ( +
+ + { + setVariants([]); + setSelectedVariantId(""); + setSelectedAttributes((values) => { + if (index < values.length - 1) { + // Undo if someone selects one of the previous attribute. + values.splice(index, values.length - index, { + slug, + value: valueSlug, + }); + } else if (valueSlug) { + // Insert new attribute choice. + values.splice(index, 1, { slug, value: valueSlug }); + } else { + // Replace current attribute choice. + values.splice(index, 1); + } + + return [...values]; + }); + }} + > + {values.map(({ slug, name }) => ( + + {name} + + ))} + +
+ ); + })}