fix(work-orders): sync draft WO# ref in effect for duplicate guard [SH-189]

This commit is contained in:
arthur.bassi 2026-08-06 12:31:45 -03:00
parent f31a301dbf
commit 3cd8298c62

View file

@ -47,7 +47,10 @@ export function useNewWoWizardState({
const [duplicateRow, setDuplicateRow] = useState<WorkOrderTableRow | null>(null);
const [isCheckingDuplicate, setIsCheckingDuplicate] = useState(false);
const draftWoNumberRef = useRef(draft.woNumber);
draftWoNumberRef.current = draft.woNumber;
useEffect(() => {
draftWoNumberRef.current = draft.woNumber;
}, [draft.woNumber]);
const { data: apiLocations = [] } = useWorkOrderLocations();
const { data: apiProblems = [] } = useDropdownOptionsByCategory("Problem");
@ -135,10 +138,15 @@ export function useNewWoWizardState({
};
const handleCreate = () => {
const woNumberAtSubmit = draft.woNumber;
void (async () => {
setIsCheckingDuplicate(true);
try {
const duplicate = await resolveDuplicate(draft.woNumber);
const duplicate = await resolveDuplicate(woNumberAtSubmit);
// Ignore stale lookups if the WO# changed while the request was in flight.
if (woNumberAtSubmit !== draftWoNumberRef.current) {
return;
}
if (duplicate) {
setDuplicateRow(duplicate);
return;