fix(admin-ui): Ensure order of variant options matches order of product options (#4178)

This commit is contained in:
Kasper Fabricius Kristensen
2023-05-25 20:36:27 +02:00
committed by GitHub
parent 9a46c11f2a
commit 063d9b6d55
2 changed files with 27 additions and 7 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/admin-ui": patch
---
fix(admin-ui): On duplicate product ensure order of variant options matches product options

View File

@@ -74,6 +74,22 @@ const useCopyProduct = () => {
{} as Partial<AdminPostProductsReq>
)
const optionRankMap: Record<string, number> = {}
if (options && options.length) {
options.forEach((option, i) => {
if (!base.options) {
base.options = []
}
optionRankMap[option.id] = i
base.options.push({
title: option.title,
})
})
}
if (variants && variants.length) {
const copiedVariants: AdminPostProductsReq["variants"] = []
@@ -108,7 +124,12 @@ const useCopyProduct = () => {
}
if (options && options.length) {
variantBase.options = options.map((option) => ({
// Sort the options by rank by looking up the rank in the optionRankMap
const sortedOptions = options.sort(
(a, b) => optionRankMap[a.option_id] - optionRankMap[b.option_id]
)
variantBase.options = sortedOptions.map((option) => ({
value: option.value,
}))
}
@@ -119,12 +140,6 @@ const useCopyProduct = () => {
base.variants = copiedVariants
}
if (options && options.length) {
base.options = options.map((option) => ({
title: option.title,
}))
}
if (images && images.length) {
base.images = images.map((image) => image.url)
}