refactor(wizard): improve basket CRUD and submit lifecycle with editingIdx state
This commit is contained in:
@@ -148,6 +148,7 @@ export function OrderWizard({
|
||||
})
|
||||
})
|
||||
const [deviceName, setDeviceName] = useState<string>('')
|
||||
const [editingIdx, setEditingIdx] = useState<number | null>(null)
|
||||
|
||||
// Partner-Profil (Fallback)
|
||||
const [customerData] = useState<Partial<Profile>>(initialProfile || {})
|
||||
@@ -595,12 +596,23 @@ export function OrderWizard({
|
||||
|
||||
const addToBasket = () => {
|
||||
const currentItem = {
|
||||
deviceName: deviceName || `Kasse ${basketItems.length + 1}`,
|
||||
deviceName: deviceName || (editingIdx !== null ? basketItems[editingIdx]?.deviceName : `Kasse ${basketItems.length + 1}`),
|
||||
selections: JSON.parse(JSON.stringify(selections)),
|
||||
moduleQuantities: { ...moduleQuantities },
|
||||
billingInterval: selectedBillingInterval,
|
||||
}
|
||||
|
||||
if (editingIdx !== null) {
|
||||
setBasketItems(prev => {
|
||||
const next = [...prev]
|
||||
next[editingIdx] = currentItem
|
||||
return next
|
||||
})
|
||||
setEditingIdx(null)
|
||||
} else {
|
||||
setBasketItems(prev => [...prev, currentItem])
|
||||
}
|
||||
|
||||
setModuleQuantities({})
|
||||
setDeviceName('')
|
||||
|
||||
@@ -622,22 +634,7 @@ export function OrderWizard({
|
||||
const item = basketItems[idx]
|
||||
if (!item) return
|
||||
|
||||
const hasProduct = Object.values(selections).some(sel => sel.productId || (sel.productIds && sel.productIds.length > 0))
|
||||
if (hasProduct) {
|
||||
const currentItem = {
|
||||
deviceName: deviceName || `Kasse ${basketItems.length + 1}`,
|
||||
selections: JSON.parse(JSON.stringify(selections)),
|
||||
moduleQuantities: { ...moduleQuantities },
|
||||
billingInterval: selectedBillingInterval,
|
||||
}
|
||||
setBasketItems(prev => {
|
||||
const filtered = prev.filter((_, i) => i !== idx)
|
||||
return [...filtered, currentItem]
|
||||
})
|
||||
} else {
|
||||
setBasketItems(prev => prev.filter((_, i) => i !== idx))
|
||||
}
|
||||
|
||||
setEditingIdx(idx)
|
||||
setSelections(item.selections)
|
||||
setModuleQuantities(item.moduleQuantities)
|
||||
setDeviceName(item.deviceName)
|
||||
@@ -645,6 +642,25 @@ export function OrderWizard({
|
||||
}
|
||||
|
||||
const deleteBasketItem = (idx: number) => {
|
||||
if (editingIdx === idx) {
|
||||
setEditingIdx(null)
|
||||
setModuleQuantities({})
|
||||
setDeviceName('')
|
||||
|
||||
const resetSels: Record<string, CategorySelection> = {}
|
||||
categories.forEach(cat => {
|
||||
const isVisible = selectedBillingInterval === 'one_time' ? cat.show_in_kauf !== false : cat.show_in_abo !== false
|
||||
if (!isVisible || cat.allow_multiselect || cat.preselect === false) {
|
||||
resetSels[cat.id] = { productId: null, productIds: [], moduleIds: [] }
|
||||
} else {
|
||||
const first = products.find(p => p.category_id === cat.id && (selectedBillingInterval === 'one_time' ? p.show_in_kauf !== false : p.show_in_abo !== false))
|
||||
resetSels[cat.id] = { productId: first?.id ?? null, productIds: first ? [first.id] : [], moduleIds: [] }
|
||||
}
|
||||
})
|
||||
setSelections(resetSels)
|
||||
} else if (editingIdx !== null && idx < editingIdx) {
|
||||
setEditingIdx(editingIdx - 1)
|
||||
}
|
||||
setBasketItems(prev => prev.filter((_, i) => i !== idx))
|
||||
}
|
||||
|
||||
@@ -653,13 +669,20 @@ export function OrderWizard({
|
||||
setIsSubmitting(true)
|
||||
try {
|
||||
let finalItems = [...basketItems]
|
||||
if (finalItems.length === 0 && allCategoriesFilled && productValidationErrors.length === 0) {
|
||||
finalItems.push({
|
||||
deviceName: deviceName || 'Kasse 1',
|
||||
selections,
|
||||
moduleQuantities,
|
||||
billingInterval: selectedBillingInterval
|
||||
})
|
||||
const hasProduct = Object.values(selections).some(sel => sel.productId || (sel.productIds && sel.productIds.length > 0))
|
||||
|
||||
if (hasProduct && allCategoriesFilled && productValidationErrors.length === 0) {
|
||||
const currentItem = {
|
||||
deviceName: deviceName || (editingIdx !== null ? basketItems[editingIdx]?.deviceName : `Kasse ${basketItems.length + 1}`),
|
||||
selections: JSON.parse(JSON.stringify(selections)),
|
||||
moduleQuantities: { ...moduleQuantities },
|
||||
billingInterval: selectedBillingInterval,
|
||||
}
|
||||
if (editingIdx !== null) {
|
||||
finalItems[editingIdx] = currentItem
|
||||
} else {
|
||||
finalItems.push(currentItem)
|
||||
}
|
||||
}
|
||||
|
||||
if (finalItems.length === 0) {
|
||||
@@ -1514,7 +1537,7 @@ export function OrderWizard({
|
||||
onClick={addToBasket}
|
||||
disabled={isNextStepDisabled}
|
||||
>
|
||||
<UserPlus className="w-4 h-4" /> Noch eine Kasse hinzufügen
|
||||
<UserPlus className="w-4 h-4" /> {editingIdx !== null ? 'Kasse aktualisieren' : 'Noch eine Kasse hinzufügen'}
|
||||
</Button>
|
||||
<Separator className="bg-white/10 my-1" />
|
||||
<Button
|
||||
|
||||
Reference in New Issue
Block a user