diff --git a/client/src/components/CustomerOrderForm.tsx b/client/src/components/CustomerOrderForm.tsx index a008779..9bffb40 100644 --- a/client/src/components/CustomerOrderForm.tsx +++ b/client/src/components/CustomerOrderForm.tsx @@ -158,19 +158,22 @@ export function CustomerOrderForm({ // Lookup API ffnancy par gencode ou référence const [articleLookupLoading, setArticleLookupLoading] = useState(false); + const [articleNotFound, setArticleNotFound] = useState(false); const lookupDebounceRef = useRef | null>(null); const gencodeValue = form.watch("gencode"); const referenceValue = form.watch("productReference"); const fetchAndFillArticle = async (params: URLSearchParams) => { setArticleLookupLoading(true); + setArticleNotFound(false); try { const res = await fetch(`/api/ffnancy/articles?${params}&limit=1`, { credentials: 'include' }); - if (!res.ok) return; + if (!res.ok) { setArticleNotFound(true); return; } const data = await res.json(); const article = data.articles?.[0]; - if (!article) return; + if (!article) { setArticleNotFound(true); return; } + setArticleNotFound(false); form.setValue("productName", article.libelle1, { shouldValidate: true }); if (article.gtin) form.setValue("gencode", article.gtin, { shouldValidate: true }); if (article.codein) form.setValue("productReference", article.codein, { shouldValidate: true }); @@ -198,27 +201,25 @@ export function CustomerOrderForm({ ); if (matched) form.setValue("supplierId", matched.id, { shouldValidate: true }); } - } catch { /* best-effort */ } finally { + } catch { setArticleNotFound(true); } finally { setArticleLookupLoading(false); } }; useEffect(() => { - if (!gencodeValue || gencodeValue.length < 8) return; + if (!gencodeValue || gencodeValue.length < 8) { setArticleNotFound(false); return; } if (lookupDebounceRef.current) clearTimeout(lookupDebounceRef.current); lookupDebounceRef.current = setTimeout(() => { - const p = new URLSearchParams({ ean: gencodeValue }); - fetchAndFillArticle(p); + fetchAndFillArticle(new URLSearchParams({ ean: gencodeValue })); }, 600); return () => { if (lookupDebounceRef.current) clearTimeout(lookupDebounceRef.current); }; }, [gencodeValue]); useEffect(() => { - if (!referenceValue || referenceValue.length < 3) return; + if (!referenceValue || referenceValue.length < 3) { setArticleNotFound(false); return; } if (lookupDebounceRef.current) clearTimeout(lookupDebounceRef.current); lookupDebounceRef.current = setTimeout(() => { - const p = new URLSearchParams({ codein: referenceValue }); - fetchAndFillArticle(p); + fetchAndFillArticle(new URLSearchParams({ codein: referenceValue })); }, 600); return () => { if (lookupDebounceRef.current) clearTimeout(lookupDebounceRef.current); }; }, [referenceValue]); @@ -425,26 +426,26 @@ export function CustomerOrderForm({ {/* Case "Client appelé" cachée pour nouvelles commandes */} + {/* Produit non référencé */} + {articleNotFound && ( +
+ Produit non référencé — ce produit n'existe pas dans la base. La commande ne peut pas être créée. +
+ )} + {/* Actions */}
- - diff --git a/client/src/pages/mobile/CustomerOrdersPage.tsx b/client/src/pages/mobile/CustomerOrdersPage.tsx index 37bf532..4ec112f 100644 --- a/client/src/pages/mobile/CustomerOrdersPage.tsx +++ b/client/src/pages/mobile/CustomerOrdersPage.tsx @@ -160,19 +160,22 @@ export default function MobileCustomerOrdersPage() { // Lookup API ffnancy par gencode ou référence const [articleLookupLoading, setArticleLookupLoading] = useState(false); + const [articleNotFound, setArticleNotFound] = useState(false); const lookupDebounceRef = useRef | null>(null); const gencodeValue = form.watch("gencode"); const referenceValue = form.watch("productReference"); const fetchAndFillArticle = async (params: URLSearchParams) => { setArticleLookupLoading(true); + setArticleNotFound(false); try { const res = await fetch(`/api/ffnancy/articles?${params}&limit=1`, { credentials: 'include' }); - if (!res.ok) return; + if (!res.ok) { setArticleNotFound(true); return; } const data = await res.json(); const article = data.articles?.[0]; - if (!article) return; + if (!article) { setArticleNotFound(true); return; } + setArticleNotFound(false); form.setValue("productName", article.libelle1, { shouldValidate: true }); if (article.gtin) form.setValue("gencode", article.gtin, { shouldValidate: true }); if (article.codein) form.setValue("productReference", article.codein, { shouldValidate: true }); @@ -199,13 +202,13 @@ export default function MobileCustomerOrdersPage() { ); if (matched) form.setValue("supplierId", matched.id, { shouldValidate: true }); } - } catch { /* best-effort */ } finally { + } catch { setArticleNotFound(true); } finally { setArticleLookupLoading(false); } }; useEffect(() => { - if (!gencodeValue || gencodeValue.length < 8) return; + if (!gencodeValue || gencodeValue.length < 8) { setArticleNotFound(false); return; } if (lookupDebounceRef.current) clearTimeout(lookupDebounceRef.current); lookupDebounceRef.current = setTimeout(() => { fetchAndFillArticle(new URLSearchParams({ ean: gencodeValue })); @@ -214,7 +217,7 @@ export default function MobileCustomerOrdersPage() { }, [gencodeValue]); useEffect(() => { - if (!referenceValue || referenceValue.length < 3) return; + if (!referenceValue || referenceValue.length < 3) { setArticleNotFound(false); return; } if (lookupDebounceRef.current) clearTimeout(lookupDebounceRef.current); lookupDebounceRef.current = setTimeout(() => { fetchAndFillArticle(new URLSearchParams({ codein: referenceValue })); @@ -575,7 +578,12 @@ export default function MobileCustomerOrdersPage() { )} /> -