Skip to content

Commit b7083b9

Browse files
willbouchwilliam bouchard
andauthored
fix(dashboard): variants disappearing when removing an option on product creation (medusajs#13150)
* fix(dashboard): variants disappearing when removing an option on product creation * error border on option without values --------- Co-authored-by: william bouchard <[email protected]>
1 parent acf6bbc commit b7083b9

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

.changeset/tasty-donuts-laugh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/dashboard": patch
3+
---
4+
5+
fix(dashboard): variants disappearing when removing an option on product creation

packages/admin/dashboard/src/routes/products/product-create/components/product-create-details-form/components/product-create-details-variant-section/product-create-details-variant-section.tsx

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ import {
33
Alert,
44
Button,
55
Checkbox,
6+
clx,
67
Heading,
78
Hint,
89
IconButton,
910
InlineTip,
1011
Input,
1112
Label,
1213
Text,
13-
clx,
1414
} from "@medusajs/ui"
1515
import {
1616
Controller,
1717
FieldArrayWithId,
18-
UseFormReturn,
1918
useFieldArray,
19+
UseFormReturn,
2020
useWatch,
2121
} from "react-hook-form"
2222
import { useTranslation } from "react-i18next"
@@ -104,7 +104,9 @@ export const ProductCreateVariantsSection = ({
104104
const newOptions = [...watchedOptions]
105105
newOptions[index].values = value
106106

107-
const permutations = getPermutations(newOptions)
107+
const permutations = getPermutations(
108+
newOptions.filter(({ values }) => values.length)
109+
)
108110
const oldVariants = [...watchedVariants]
109111

110112
const findMatchingPermutation = (options: Record<string, string>) => {
@@ -157,46 +159,29 @@ export const ProductCreateVariantsSection = ({
157159

158160
const newOptions = [...watchedOptions]
159161
newOptions.splice(index, 1)
162+
const validOptionTitles = new Set(newOptions.map((option) => option.title))
160163

161164
const permutations = getPermutations(newOptions)
162165
const oldVariants = [...watchedVariants]
163166

164-
const findMatchingPermutation = (options: Record<string, string>) => {
165-
return permutations.find((permutation) =>
166-
Object.keys(options).every((key) => options[key] === permutation[key])
167+
const newVariants = permutations.reduce((variants, permutation) => {
168+
const variant = oldVariants.find(({ options }) =>
169+
Object.keys(options)
170+
.filter((option) => validOptionTitles.has(option))
171+
.every((key) => options[key] === permutation[key])
167172
)
168-
}
169-
170-
const newVariants = oldVariants.reduce((variants, variant) => {
171-
const match = findMatchingPermutation(variant.options)
172173

173-
if (match) {
174+
if (variant) {
174175
variants.push({
175176
...variant,
176-
title: getVariantName(match),
177-
options: match,
177+
title: variant.title,
178+
options: permutation,
178179
})
179180
}
180181

181182
return variants
182183
}, [] as typeof oldVariants)
183184

184-
const usedPermutations = new Set(
185-
newVariants.map((variant) => variant.options)
186-
)
187-
const unusedPermutations = permutations.filter(
188-
(permutation) => !usedPermutations.has(permutation)
189-
)
190-
191-
unusedPermutations.forEach((permutation) => {
192-
newVariants.push({
193-
title: getVariantName(permutation),
194-
options: permutation,
195-
should_create: false,
196-
variant_rank: newVariants.length,
197-
})
198-
})
199-
200185
form.setValue("variants", newVariants)
201186
}
202187

@@ -347,10 +332,18 @@ export const ProductCreateVariantsSection = ({
347332
)}
348333
<ul className="flex flex-col gap-y-4">
349334
{options.fields.map((option, index) => {
335+
const hasError =
336+
!!form.formState.errors.options?.[index]
350337
return (
351338
<li
352339
key={option.id}
353-
className="bg-ui-bg-component shadow-elevation-card-rest grid grid-cols-[1fr_28px] items-center gap-1.5 rounded-xl p-1.5"
340+
className={clx(
341+
"bg-ui-bg-component shadow-elevation-card-rest grid grid-cols-[1fr_28px] items-center gap-1.5 rounded-xl p-1.5",
342+
{
343+
"border-ui-border-error shadow-borders-error":
344+
hasError,
345+
}
346+
)}
354347
>
355348
<div className="grid grid-cols-[min-content,1fr] items-center gap-1.5">
356349
<div className="flex items-center px-2 py-1.5">

0 commit comments

Comments
 (0)