From c5731a120634daef15f1637015807df7f7ee9973 Mon Sep 17 00:00:00 2001 From: SJCoetz Date: Sat, 11 Jul 2026 00:04:27 +0200 Subject: [PATCH] feat(collection-pool): sequential bulk-assign PATCH --- src/app/(app)/collection-pool/page.tsx | 53 ++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/src/app/(app)/collection-pool/page.tsx b/src/app/(app)/collection-pool/page.tsx index 5561469..a427f4f 100644 --- a/src/app/(app)/collection-pool/page.tsx +++ b/src/app/(app)/collection-pool/page.tsx @@ -355,10 +355,57 @@ export default function CollectionPoolPage() { const [bulkBusy, setBulkBusy] = useState(false); const handleBulkAssignSubmit = useCallback( - async (_payload: AssignDriverSubmitPayload) => { - // Full implementation in Task 9. + async (payload: AssignDriverSubmitPayload) => { + if (!bulkAssignDialog) return; + const { driverId, date } = payload; + const { rowIds, supplierName } = bulkAssignDialog; + + setBulkBusy(true); + try { + const pb = getPocketBase(); + let successCount = 0; + let firstError: string | null = null; + + // Sequential, not parallel: any failure leaves earlier + // successes committed (matches the single-row PATCH semantics). + for (const id of rowIds) { + try { + await pb.collection("order_items").update(id, { + item_status: "assigned", + drv_id: driverId, + drv_status: "assigned", + assigned_at: `${date}T00:00:00.000Z`, + } as Partial); + successCount++; + } catch (err) { + if (!firstError) { + firstError = err instanceof Error ? err.message : String(err); + } + } + } + + // Clear selections for this supplier on success (partial or full). + setSelectedIds((prev) => { + const next = new Set(prev); + for (const id of rowIds) next.delete(id); + return next; + }); + closeBulkAssign(); + + if (firstError) { + toast.error( + `Assigned ${successCount} of ${rowIds.length} stops at ${supplierName || "supplier"}. First error: ${firstError}`, + ); + } else { + toast.success(`Assigned ${successCount} stops at ${supplierName || "supplier"}.`); + } + + await load(); + } finally { + setBulkBusy(false); + } }, - [], + [bulkAssignDialog, closeBulkAssign, load], ); const handleToggleReady = useCallback(