diff --git a/src/components/pages/inventory/movement/form/MovementForm.tsx b/src/components/pages/inventory/movement/form/MovementForm.tsx index 8137260f..77934af5 100644 --- a/src/components/pages/inventory/movement/form/MovementForm.tsx +++ b/src/components/pages/inventory/movement/form/MovementForm.tsx @@ -263,6 +263,25 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { }, }); + // ===== WAREHOUSE VALIDATION TOAST ===== + useEffect(() => { + const destinationWarehouseError = formik.errors + .destination_warehouse_id as string; + + if ( + destinationWarehouseError === + 'Gudang tujuan tidak boleh sama dengan gudang asal!' + ) { + const sourceWarehouseName = + (formik.values.source_warehouse as WarehouseOptionType)?.label || + 'gudang asal'; + + toast.error( + `Tidak bisa memilih gudang yang sama. Gudang tujuan tidak boleh sama dengan ${sourceWarehouseName}.` + ); + } + }, [formik.errors.destination_warehouse_id, formik.values.source_warehouse]); + // ===== PRODUCT WAREHOUSE FETCHING (after form initialization) ===== const { setInputValue: setProductWarehouseSelectInputValue, @@ -361,59 +380,10 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { (val: OptionType | OptionType[] | null) => { const newSourceWarehouseId = (val as WarehouseOptionType)?.value; - if (newSourceWarehouseId) { - if (newSourceWarehouseId === formik.values.destination_warehouse_id) { - const destinationWarehouseName = - (formik.values.destination_warehouse as WarehouseOptionType) - ?.label || 'gudang tujuan'; - - toast.error( - `Tidak bisa memilih gudang yang sama. Gudang asal tidak boleh sama dengan ${destinationWarehouseName}.` - ); - return; - } - } - formik.setFieldTouched('source_warehouse', true); formik.setFieldValue('source_warehouse', val); formik.setFieldTouched('source_warehouse_id', true); formik.setFieldValue('source_warehouse_id', newSourceWarehouseId); - - if ( - formik.errors.destination_warehouse_id === - 'Gudang tujuan tidak boleh sama dengan gudang asal!' - ) { - formik.setFieldError('destination_warehouse_id', undefined); - } - - if ( - newSourceWarehouseId && - newSourceWarehouseId !== formik.values.source_warehouse_id - ) { - formik.setFieldValue('products', [ - { - product: null, - product_id: 0, - product_qty: '', - }, - ]); - formik.setFieldTouched('products', false); - - const updatedDeliveries = formik.values.deliveries.map( - (delivery: DeliverySchema) => ({ - ...delivery, - products: [ - { - product: null, - product_id: 0, - product_qty: '', - }, - ], - }) - ); - formik.setFieldValue('deliveries', updatedDeliveries); - formik.setFieldTouched('deliveries', false); - } }, [] ); @@ -422,19 +392,6 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { (val: OptionType | OptionType[] | null) => { const newDestinationWarehouseId = (val as WarehouseOptionType)?.value; - if (newDestinationWarehouseId) { - if (newDestinationWarehouseId === formik.values.source_warehouse_id) { - const sourceWarehouseName = - (formik.values.source_warehouse as WarehouseOptionType)?.label || - 'gudang asal'; - - toast.error( - `Tidak bisa memilih gudang yang sama. Gudang tujuan tidak boleh sama dengan ${sourceWarehouseName}.` - ); - return; - } - } - formik.setFieldTouched('destination_warehouse', true); formik.setFieldValue('destination_warehouse', val); formik.setFieldTouched('destination_warehouse_id', true); @@ -442,13 +399,6 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { 'destination_warehouse_id', newDestinationWarehouseId ); - - if ( - formik.errors.destination_warehouse_id === - 'Gudang tujuan tidak boleh sama dengan gudang asal!' - ) { - formik.setFieldError('destination_warehouse_id', undefined); - } }, [] );