diff --git a/src/components/pages/inventory/movement/form/MovementForm.tsx b/src/components/pages/inventory/movement/form/MovementForm.tsx index 1907d498..51d35857 100644 --- a/src/components/pages/inventory/movement/form/MovementForm.tsx +++ b/src/components/pages/inventory/movement/form/MovementForm.tsx @@ -56,6 +56,9 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { const [productQtyErrorShown, setProductQtyErrorShown] = useState(false); const [deliveryQtyErrorShown, setDeliveryQtyErrorShown] = useState(false); const [isInitialized, setIsInitialized] = useState(false); + const productStockCacheRef = useRef< + Map + >(new Map()); // ===== FORM HANDLERS ===== const createMovementHandler = useCallback( @@ -337,6 +340,7 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { prevSourceWarehouseId !== currentSourceWarehouseId && prevSourceWarehouseId !== null ) { + productStockCacheRef.current = new Map(); formik.setFieldValue('products', [ { product: null, @@ -399,6 +403,15 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { : []; }, [productWarehouses]); + useEffect(() => { + productWarehouseOptions.forEach((pw) => { + productStockCacheRef.current.set(pw.product_id, { + quantity: pw.quantity, + transfer_available_qty: pw.transfer_available_qty, + }); + }); + }, [productWarehouseOptions]); + // ===== HELPER FUNCTIONS ===== const isRepeaterInputError = ( arrayName: T, @@ -840,15 +853,12 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { const getAvailableStock = useCallback( (productId: number) => { if (type === 'detail') return 0; - const productWarehouse = productWarehouseOptions.find( + const live = productWarehouseOptions.find( (pw) => pw.product_id === productId ); - - return ( - productWarehouse?.transfer_available_qty ?? - productWarehouse?.quantity ?? - 0 - ); + if (live) return live.transfer_available_qty ?? live.quantity ?? 0; + const cached = productStockCacheRef.current.get(productId); + return cached?.transfer_available_qty ?? cached?.quantity ?? 0; }, [productWarehouseOptions, type] ); @@ -856,20 +866,25 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { const getTotalStock = useCallback( (productId: number) => { if (type === 'detail') return 0; - const productWarehouse = productWarehouseOptions.find( + const live = productWarehouseOptions.find( (pw) => pw.product_id === productId ); - return productWarehouse?.quantity ?? 0; + if (live) return live.quantity ?? 0; + return productStockCacheRef.current.get(productId)?.quantity ?? 0; }, [productWarehouseOptions, type] ); const hasAvailableQty = useCallback( (productId: number) => { - const productWarehouse = productWarehouseOptions.find( + const live = productWarehouseOptions.find( (pw) => pw.product_id === productId ); - return productWarehouse?.transfer_available_qty !== undefined; + if (live) return live.transfer_available_qty !== undefined; + return ( + productStockCacheRef.current.get(productId)?.transfer_available_qty !== + undefined + ); }, [productWarehouseOptions] );