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 [deviceName, setDeviceName] = useState<string>('')
|
||||||
|
const [editingIdx, setEditingIdx] = useState<number | null>(null)
|
||||||
|
|
||||||
// Partner-Profil (Fallback)
|
// Partner-Profil (Fallback)
|
||||||
const [customerData] = useState<Partial<Profile>>(initialProfile || {})
|
const [customerData] = useState<Partial<Profile>>(initialProfile || {})
|
||||||
@@ -595,12 +596,23 @@ export function OrderWizard({
|
|||||||
|
|
||||||
const addToBasket = () => {
|
const addToBasket = () => {
|
||||||
const currentItem = {
|
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)),
|
selections: JSON.parse(JSON.stringify(selections)),
|
||||||
moduleQuantities: { ...moduleQuantities },
|
moduleQuantities: { ...moduleQuantities },
|
||||||
billingInterval: selectedBillingInterval,
|
billingInterval: selectedBillingInterval,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (editingIdx !== null) {
|
||||||
|
setBasketItems(prev => {
|
||||||
|
const next = [...prev]
|
||||||
|
next[editingIdx] = currentItem
|
||||||
|
return next
|
||||||
|
})
|
||||||
|
setEditingIdx(null)
|
||||||
|
} else {
|
||||||
setBasketItems(prev => [...prev, currentItem])
|
setBasketItems(prev => [...prev, currentItem])
|
||||||
|
}
|
||||||
|
|
||||||
setModuleQuantities({})
|
setModuleQuantities({})
|
||||||
setDeviceName('')
|
setDeviceName('')
|
||||||
|
|
||||||
@@ -622,22 +634,7 @@ export function OrderWizard({
|
|||||||
const item = basketItems[idx]
|
const item = basketItems[idx]
|
||||||
if (!item) return
|
if (!item) return
|
||||||
|
|
||||||
const hasProduct = Object.values(selections).some(sel => sel.productId || (sel.productIds && sel.productIds.length > 0))
|
setEditingIdx(idx)
|
||||||
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))
|
|
||||||
}
|
|
||||||
|
|
||||||
setSelections(item.selections)
|
setSelections(item.selections)
|
||||||
setModuleQuantities(item.moduleQuantities)
|
setModuleQuantities(item.moduleQuantities)
|
||||||
setDeviceName(item.deviceName)
|
setDeviceName(item.deviceName)
|
||||||
@@ -645,6 +642,25 @@ export function OrderWizard({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const deleteBasketItem = (idx: number) => {
|
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))
|
setBasketItems(prev => prev.filter((_, i) => i !== idx))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -653,13 +669,20 @@ export function OrderWizard({
|
|||||||
setIsSubmitting(true)
|
setIsSubmitting(true)
|
||||||
try {
|
try {
|
||||||
let finalItems = [...basketItems]
|
let finalItems = [...basketItems]
|
||||||
if (finalItems.length === 0 && allCategoriesFilled && productValidationErrors.length === 0) {
|
const hasProduct = Object.values(selections).some(sel => sel.productId || (sel.productIds && sel.productIds.length > 0))
|
||||||
finalItems.push({
|
|
||||||
deviceName: deviceName || 'Kasse 1',
|
if (hasProduct && allCategoriesFilled && productValidationErrors.length === 0) {
|
||||||
selections,
|
const currentItem = {
|
||||||
moduleQuantities,
|
deviceName: deviceName || (editingIdx !== null ? basketItems[editingIdx]?.deviceName : `Kasse ${basketItems.length + 1}`),
|
||||||
billingInterval: selectedBillingInterval
|
selections: JSON.parse(JSON.stringify(selections)),
|
||||||
})
|
moduleQuantities: { ...moduleQuantities },
|
||||||
|
billingInterval: selectedBillingInterval,
|
||||||
|
}
|
||||||
|
if (editingIdx !== null) {
|
||||||
|
finalItems[editingIdx] = currentItem
|
||||||
|
} else {
|
||||||
|
finalItems.push(currentItem)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (finalItems.length === 0) {
|
if (finalItems.length === 0) {
|
||||||
@@ -1514,7 +1537,7 @@ export function OrderWizard({
|
|||||||
onClick={addToBasket}
|
onClick={addToBasket}
|
||||||
disabled={isNextStepDisabled}
|
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>
|
</Button>
|
||||||
<Separator className="bg-white/10 my-1" />
|
<Separator className="bg-white/10 my-1" />
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
Reference in New Issue
Block a user