From 2f9221359433592181d914f42e89647078a74268 Mon Sep 17 00:00:00 2001 From: DanielS Date: Fri, 10 Jul 2026 01:10:39 +0200 Subject: [PATCH] feat: reconstruct multiple devices from initialOrder items into basketItems for editing --- shop/components/order-wizard.tsx | 62 +++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/shop/components/order-wizard.tsx b/shop/components/order-wizard.tsx index 5d2f933..1aad854 100644 --- a/shop/components/order-wizard.tsx +++ b/shop/components/order-wizard.tsx @@ -98,7 +98,55 @@ export function OrderWizard({ const [selectedCompanyId, setSelectedCompanyId] = useState( initialOrder?.company_id ?? null ) - const [basketItems, setBasketItems] = useState([]) + const [basketItems, setBasketItems] = useState(() => { + if (!initialOrder || !initialOrder.order_data?.items) return [] + + const groups: Record = {} + initialOrder.order_data.items.forEach((item: any) => { + const devName = item.device_name || 'Kasse 1' + if (!groups[devName]) { + groups[devName] = [] + } + groups[devName].push(item) + }) + + return Object.entries(groups).map(([deviceName, devItems]) => { + const selections: Record = {} + const moduleQuantities: Record = {} + + categories.forEach(cat => { + selections[cat.id] = { productId: null, productIds: [], moduleIds: [] } + }) + + devItems.forEach((item: any) => { + const catId = item.category_id + if (!selections[catId]) { + selections[catId] = { productId: null, productIds: [], moduleIds: [] } + } + + const cat = categories.find(c => c.id === catId) + if (cat?.allow_multiselect) { + selections[catId].productIds = [...(selections[catId].productIds || []), item.product_id] + selections[catId].productId = selections[catId].productIds[0] || null + } else { + selections[catId].productId = item.product_id + selections[catId].productIds = [item.product_id] + } + + item.selected_modules?.forEach((mod: any) => { + selections[catId].moduleIds.push(mod.module_id) + moduleQuantities[mod.module_id] = mod.quantity || 1 + }) + }) + + return { + deviceName, + selections, + moduleQuantities, + billingInterval: devItems[0]?.billing_interval || 'monthly' + } + }) + }) const [deviceName, setDeviceName] = useState('') // Partner-Profil (Fallback) @@ -223,18 +271,6 @@ export function OrderWizard({ const initInterval = initialOrder?.order_data?.billing_cycle ?? 'monthly' const init: Record = {} categories.forEach(cat => { - if (initialOrder) { - // Multi-select might have multiple items matching this category - const matchingItems = initialOrder.order_data?.items?.filter(i => i.category_id === cat.id) || [] - if (matchingItems.length > 0) { - init[cat.id] = { - productId: matchingItems[0].product_id, - productIds: matchingItems.map(i => i.product_id), - moduleIds: matchingItems.flatMap(i => i.selected_modules?.map(m => m.module_id) ?? []) - } - return - } - } const isVisible = initInterval === 'one_time' ? cat.show_in_kauf !== false : cat.show_in_abo !== false if (!isVisible || cat.allow_multiselect || cat.preselect === false) { init[cat.id] = { productId: null, productIds: [], moduleIds: [] }