feat(category): add resets_others option
All checks were successful
Staging Build / build (push) Successful in 2m46s

This commit is contained in:
DanielS
2026-07-09 17:27:16 +02:00
parent 567d1f226b
commit 6b35917a06
5 changed files with 124 additions and 42 deletions

View File

@@ -379,30 +379,61 @@ export function OrderWizard({
nextProductId = productId
}
const isSelectedNow = cat?.allow_multiselect
? nextProductIds.includes(productId)
: nextProductId === productId
const shouldReset = isSelectedNow && cat?.resets_others
const next = {
...prev,
[catId]: { productId: nextProductId, productIds: nextProductIds, moduleIds: [] },
}
// Automatically deselect now conflicting products in other categories
const selectedIds: string[] = []
Object.values(next).forEach(s => {
if (s.productIds && s.productIds.length > 0) {
s.productIds.forEach(id => {
if (id) selectedIds.push(id)
})
} else if (s.productId) {
selectedIds.push(s.productId)
}
})
if (shouldReset) {
// Reset all other categories
categories.forEach(c => {
if (c.id !== catId) {
next[c.id] = { productId: null, productIds: [], moduleIds: [] }
}
})
} else {
// Automatically deselect now conflicting products in other categories
const selectedIds: string[] = []
Object.values(next).forEach(s => {
if (s.productIds && s.productIds.length > 0) {
s.productIds.forEach(id => {
if (id) selectedIds.push(id)
})
} else if (s.productId) {
selectedIds.push(s.productId)
}
})
categories.forEach(c => {
if (c.id === catId) return
const s = next[c.id]
if (c.allow_multiselect && s?.productIds) {
const validProductIds = s.productIds.filter(pId => {
const prod = products.find(p => p.id === pId)
const otherSelectedProductIds = selectedIds.filter(id => id !== pId)
categories.forEach(c => {
if (c.id === catId) return
const s = next[c.id]
if (c.allow_multiselect && s?.productIds) {
const validProductIds = s.productIds.filter(pId => {
const prod = products.find(p => p.id === pId)
const otherSelectedProductIds = selectedIds.filter(id => id !== pId)
const isExcluded = prod && (
otherSelectedProductIds.some(otherId => {
const otherProd = products.find(p => p.id === otherId)
return otherProd?.exclusions?.includes(prod.id)
}) ||
prod.exclusions?.some(exId => otherSelectedProductIds.includes(exId))
)
return !isExcluded
})
next[c.id] = {
productId: validProductIds[0] || null,
productIds: validProductIds,
moduleIds: []
}
} else if (s?.productId) {
const prod = products.find(p => p.id === s.productId)
const otherSelectedProductIds = selectedIds.filter(id => id !== s.productId)
const isExcluded = prod && (
otherSelectedProductIds.some(otherId => {
const otherProd = products.find(p => p.id === otherId)
@@ -410,28 +441,12 @@ export function OrderWizard({
}) ||
prod.exclusions?.some(exId => otherSelectedProductIds.includes(exId))
)
return !isExcluded
})
next[c.id] = {
productId: validProductIds[0] || null,
productIds: validProductIds,
moduleIds: []
if (isExcluded) {
next[c.id] = { productId: null, productIds: [], moduleIds: [] }
}
}
} else if (s?.productId) {
const prod = products.find(p => p.id === s.productId)
const otherSelectedProductIds = selectedIds.filter(id => id !== s.productId)
const isExcluded = prod && (
otherSelectedProductIds.some(otherId => {
const otherProd = products.find(p => p.id === otherId)
return otherProd?.exclusions?.includes(prod.id)
}) ||
prod.exclusions?.some(exId => otherSelectedProductIds.includes(exId))
)
if (isExcluded) {
next[c.id] = { productId: null, productIds: [], moduleIds: [] }
}
}
})
})
}
return next
})