refactor(FE): Reset deliveries when products change

This commit is contained in:
rstubryan
2026-01-23 16:26:25 +07:00
parent 5c286128e4
commit 6f5540eb91
@@ -1115,6 +1115,44 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => {
formik.errors.destination_warehouse_id, formik.errors.destination_warehouse_id,
]); ]);
useEffect(() => {
if (formik.values.products && formik.values.deliveries) {
const productIds = formik.values.products.map((p) => p.product_id);
const updatedDeliveries = formik.values.deliveries.map((delivery) => {
const deliveryProduct = delivery.products[0];
if (deliveryProduct && deliveryProduct.product_id !== 0) {
if (!productIds.includes(deliveryProduct.product_id)) {
return {
...delivery,
products: [
{
product: null,
product_id: 0,
product_qty: '',
},
],
delivery_cost: '',
delivery_cost_per_item: '',
};
}
}
return delivery;
});
const hasChanges = formik.values.deliveries.some(
(delivery, idx) =>
delivery.products[0]?.product_id !==
updatedDeliveries[idx]?.products[0]?.product_id ||
delivery.delivery_cost !== updatedDeliveries[idx]?.delivery_cost
);
if (hasChanges) {
formik.setFieldValue('deliveries', updatedDeliveries);
}
}
}, [formik.values.products]);
useEffect(() => { useEffect(() => {
if (productQtyErrorShown) { if (productQtyErrorShown) {
toast.dismiss(); toast.dismiss();