fix(wizard): prevent duplicate devices on summary step

This commit is contained in:
DanielS
2026-08-18 22:55:15 +02:00
parent fff40a68fe
commit 3545b77803

View File

@@ -429,11 +429,15 @@ export function OrderWizard({
}) })
}, [visibleCategories, selections, products, selectedBillingInterval]) }, [visibleCategories, selections, products, selectedBillingInterval])
const finalItemsToShow = basketItems.length > 0 const finalItemsToShow = useMemo(() => {
? basketItems if (basketItems.length > 0) {
: (allCategoriesFilled && productValidationErrors.length === 0 return basketItems
? [{ deviceName: deviceName || 'Kasse 1', selections, moduleQuantities, billingInterval: selectedBillingInterval }] }
: []) if (allCategoriesFilled && productValidationErrors.length === 0 && hasActiveSelection) {
return [{ deviceName: deviceName || 'Kasse 1', licenseNumber, selections, moduleQuantities, billingInterval: selectedBillingInterval }]
}
return []
}, [basketItems, allCategoriesFilled, productValidationErrors, hasActiveSelection, deviceName, licenseNumber, selections, moduleQuantities, selectedBillingInterval])
// Gesamtsummen für alle Items in der Bestellung (finalItemsToShow) // Gesamtsummen für alle Items in der Bestellung (finalItemsToShow)
const { overallMonthlyTotal, overallOneTimeTotal, linkedFeeProducts } = useMemo(() => { const { overallMonthlyTotal, overallOneTimeTotal, linkedFeeProducts } = useMemo(() => {
@@ -542,7 +546,7 @@ export function OrderWizard({
const nextStep = () => { const nextStep = () => {
if (step === 3 && allCategoriesFilled && productValidationErrors.length === 0) { if (step === 3 && allCategoriesFilled && productValidationErrors.length === 0) {
const isDirtyNewDevice = deviceName.trim() !== '' || licenseNumber.trim() !== '' || Object.values(moduleQuantities).some(q => q > 0) const isDirtyNewDevice = (deviceName.trim() !== '' || licenseNumber.trim() !== '' || Object.values(moduleQuantities).some(q => q > 0)) && editingIdx === null
if (editingIdx !== null) { if (editingIdx !== null) {
addToBasket() addToBasket()
} else if (basketItems.length === 0 && hasActiveSelection) { } else if (basketItems.length === 0 && hasActiveSelection) {
@@ -743,35 +747,35 @@ export function OrderWizard({
setIsSubmitting(true) setIsSubmitting(true)
try { try {
let finalItems = [...basketItems] let finalItems = [...basketItems]
const isDirtyNewDevice = deviceName.trim() !== '' || licenseNumber.trim() !== '' || Object.values(moduleQuantities).some(q => q > 0) const isDirtyNewDevice = (deviceName.trim() !== '' || licenseNumber.trim() !== '' || Object.values(moduleQuantities).some(q => q > 0)) && editingIdx === null
if ((editingIdx !== null || basketItems.length === 0 || isDirtyNewDevice) && hasActiveSelection && allCategoriesFilled && productValidationErrors.length === 0) { if (editingIdx !== null) {
const normLicense = licenseNumber.trim().toUpperCase() const normLicense = licenseNumber.trim().toUpperCase()
if (normLicense) {
const isDuplicateInBasket = basketItems.some(
(item, idx) => idx !== editingIdx && item.licenseNumber?.toUpperCase().trim() === normLicense
)
if (isDuplicateInBasket) {
throw new Error(`Lizenznummer "${licenseNumber}" ist bereits im aktuellen Warenkorb vorhanden.`)
}
const isTaken = await isLicenseNumberTaken(normLicense, initialOrder?.id)
if (isTaken) {
throw new Error(`Lizenznummer "${licenseNumber}" wird bereits von einer anderen Bestellung verwendet.`)
}
}
const currentItem = { const currentItem = {
deviceName: deviceName || (editingIdx !== null ? basketItems[editingIdx]?.deviceName : `Kasse ${basketItems.length + 1}`), deviceName: deviceName || basketItems[editingIdx]?.deviceName || `Kasse ${editingIdx + 1}`,
licenseNumber: normLicense, licenseNumber: normLicense,
selections: JSON.parse(JSON.stringify(selections)), selections: JSON.parse(JSON.stringify(selections)),
moduleQuantities: { ...moduleQuantities }, moduleQuantities: { ...moduleQuantities },
billingInterval: selectedBillingInterval, billingInterval: selectedBillingInterval,
} }
if (editingIdx !== null) { finalItems[editingIdx] = currentItem
finalItems[editingIdx] = currentItem } else if (basketItems.length === 0 && hasActiveSelection && allCategoriesFilled && productValidationErrors.length === 0) {
} else { const normLicense = licenseNumber.trim().toUpperCase()
finalItems.push(currentItem) finalItems.push({
} deviceName: deviceName || 'Kasse 1',
licenseNumber: normLicense,
selections: JSON.parse(JSON.stringify(selections)),
moduleQuantities: { ...moduleQuantities },
billingInterval: selectedBillingInterval,
})
} else if (isDirtyNewDevice && hasActiveSelection && allCategoriesFilled && productValidationErrors.length === 0) {
const normLicense = licenseNumber.trim().toUpperCase()
finalItems.push({
deviceName: deviceName || `Kasse ${basketItems.length + 1}`,
licenseNumber: normLicense,
selections: JSON.parse(JSON.stringify(selections)),
moduleQuantities: { ...moduleQuantities },
billingInterval: selectedBillingInterval,
})
} }
if (finalItems.length === 0) { if (finalItems.length === 0) {