From ba9e3f6c17108c9e34264985821998ea27b410b8 Mon Sep 17 00:00:00 2001 From: beatz174-bit Date: Tue, 2 Dec 2025 17:31:03 +1000 Subject: [PATCH] Fix barcode lookup initialization and unit test script --- package.json | 1 + src/components/AddProductDialog.tsx | 52 ++++++++++++++--------------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index 46bd052..14299b8 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "preview": "vite preview", "lint": "eslint .", "test": "vitest", + "test:unit": "vitest run", "test:coverage": "vitest run --coverage", "test:e2e": "playwright test", "coverage:unit": "npm run test:coverage", diff --git a/src/components/AddProductDialog.tsx b/src/components/AddProductDialog.tsx index 9c43fbe..84b4a95 100644 --- a/src/components/AddProductDialog.tsx +++ b/src/components/AddProductDialog.tsx @@ -59,32 +59,6 @@ export const AddProductDialog = ({ setScannerOpen(false); }, []); - useEffect(() => { - if (open) { - if (categoryOptions.length > 0 && !categoryOptions.includes(category)) { - setCategory(categoryOptions[0]); - } - if (initialBarcode) { - setBarcode(initialBarcode); - void lookupBarcode(initialBarcode); - } - } else { - resetForm(); - } - }, [category, categoryOptions, initialBarcode, lookupBarcode, open, resetForm]); - - useEffect(() => { - if (!open || !barcode) return; - void lookupBarcode(barcode); - }, [barcode, lookupBarcode, open]); - - useEffect(() => { - if (!barcode) { - setLookupStatus('idle'); - } - setBarcodeError(''); - }, [barcode]); - const categoryMap = useMemo(() => new Map(categoryOptions.map((c) => [c, c])), [categoryOptions]); const findBarcodeConflict = useCallback( @@ -198,6 +172,32 @@ export const AddProductDialog = ({ } }, []); + useEffect(() => { + if (open) { + if (categoryOptions.length > 0 && !categoryOptions.includes(category)) { + setCategory(categoryOptions[0]); + } + if (initialBarcode) { + setBarcode(initialBarcode); + void lookupBarcode(initialBarcode); + } + } else { + resetForm(); + } + }, [category, categoryOptions, initialBarcode, lookupBarcode, open, resetForm]); + + useEffect(() => { + if (!open || !barcode) return; + void lookupBarcode(barcode); + }, [barcode, lookupBarcode, open]); + + useEffect(() => { + if (!barcode) { + setLookupStatus('idle'); + } + setBarcodeError(''); + }, [barcode]); + const handleSubmit = async () => { setNameError(''); setBarcodeError('');