fix(admin-ui): Ensure order of variant options matches order of product options (#4178)
This commit is contained in:
1 parent
9a46c11f2a
commit
063d9b6d55
2 files changed
+27
-7
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/admin-ui": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(admin-ui): On duplicate product ensure order of variant options matches product options
|
||||||
@@ -74,6 +74,22 @@ const useCopyProduct = () => {
|
|||||||
{} as Partial<AdminPostProductsReq>
|
{} 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) {
|
if (variants && variants.length) {
|
||||||
const copiedVariants: AdminPostProductsReq["variants"] = []
|
const copiedVariants: AdminPostProductsReq["variants"] = []
|
||||||
|
|
||||||
@@ -108,7 +124,12 @@ const useCopyProduct = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (options && options.length) {
|
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,
|
value: option.value,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@@ -119,12 +140,6 @@ const useCopyProduct = () => {
|
|||||||
base.variants = copiedVariants
|
base.variants = copiedVariants
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options && options.length) {
|
|
||||||
base.options = options.map((option) => ({
|
|
||||||
title: option.title,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (images && images.length) {
|
if (images && images.length) {
|
||||||
base.images = images.map((image) => image.url)
|
base.images = images.map((image) => image.url)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user