mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 07:45:47 +00:00
refactor(FE-Issue): add validation to prevent selecting the same warehouse for source and destination in MovementForm
This commit is contained in:
@@ -176,6 +176,22 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => {
|
|||||||
enableReinitialize: true,
|
enableReinitialize: true,
|
||||||
onSubmit: async (values) => {
|
onSubmit: async (values) => {
|
||||||
setMovementFormErrorMessage('');
|
setMovementFormErrorMessage('');
|
||||||
|
if (values.source_warehouse_id === values.destination_warehouse_id) {
|
||||||
|
const sourceWarehouseName =
|
||||||
|
(values.source_warehouse as WarehouseOptionType)?.label ||
|
||||||
|
'Gudang asal';
|
||||||
|
const destinationWarehouseName =
|
||||||
|
(values.destination_warehouse as WarehouseOptionType)?.label ||
|
||||||
|
'gudang tujuan';
|
||||||
|
|
||||||
|
setMovementFormErrorMessage(
|
||||||
|
`Tidak bisa submit form. ${sourceWarehouseName} tidak boleh sama dengan ${destinationWarehouseName}.`
|
||||||
|
);
|
||||||
|
toast.error(
|
||||||
|
`Tidak bisa submit form. Gudang asal dan tujuan tidak boleh sama!`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const documents: File[] = [];
|
const documents: File[] = [];
|
||||||
const deliveriesPayload = values.deliveries.map((d) => {
|
const deliveriesPayload = values.deliveries.map((d) => {
|
||||||
let documentIndex = 0;
|
let documentIndex = 0;
|
||||||
@@ -858,35 +874,40 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => {
|
|||||||
placeholder='Pilih gudang asal...'
|
placeholder='Pilih gudang asal...'
|
||||||
value={formik.values.source_warehouse}
|
value={formik.values.source_warehouse}
|
||||||
onChange={(val) => {
|
onChange={(val) => {
|
||||||
|
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; // Prevent setting the value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
formik.setFieldTouched('source_warehouse', true);
|
formik.setFieldTouched('source_warehouse', true);
|
||||||
formik.setFieldValue('source_warehouse', val);
|
formik.setFieldValue('source_warehouse', val);
|
||||||
formik.setFieldTouched('source_warehouse_id', true);
|
formik.setFieldTouched('source_warehouse_id', true);
|
||||||
const newSourceWarehouseId = (val as WarehouseOptionType)
|
|
||||||
?.value;
|
|
||||||
formik.setFieldValue(
|
formik.setFieldValue(
|
||||||
'source_warehouse_id',
|
'source_warehouse_id',
|
||||||
newSourceWarehouseId
|
newSourceWarehouseId
|
||||||
);
|
);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
newSourceWarehouseId &&
|
formik.errors.destination_warehouse_id ===
|
||||||
formik.values.destination_warehouse_id &&
|
'Gudang tujuan tidak boleh sama dengan gudang asal!'
|
||||||
newSourceWarehouseId ===
|
|
||||||
formik.values.destination_warehouse_id
|
|
||||||
) {
|
) {
|
||||||
formik.setFieldError(
|
formik.setFieldError('destination_warehouse_id', undefined);
|
||||||
'destination_warehouse_id',
|
|
||||||
'Gudang tujuan tidak boleh sama dengan gudang asal!'
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
if (
|
|
||||||
formik.errors.destination_warehouse_id ===
|
|
||||||
'Gudang tujuan tidak boleh sama dengan gudang asal!'
|
|
||||||
) {
|
|
||||||
formik.setFieldError(
|
|
||||||
'destination_warehouse_id',
|
|
||||||
undefined
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
options={warehouseOptions}
|
options={warehouseOptions}
|
||||||
@@ -952,35 +973,38 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => {
|
|||||||
placeholder='Pilih gudang tujuan...'
|
placeholder='Pilih gudang tujuan...'
|
||||||
value={formik.values.destination_warehouse}
|
value={formik.values.destination_warehouse}
|
||||||
onChange={(val) => {
|
onChange={(val) => {
|
||||||
|
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.setFieldTouched('destination_warehouse', true);
|
||||||
formik.setFieldValue('destination_warehouse', val);
|
formik.setFieldValue('destination_warehouse', val);
|
||||||
formik.setFieldTouched('destination_warehouse_id', true);
|
formik.setFieldTouched('destination_warehouse_id', true);
|
||||||
const newDestinationWarehouseId = (val as WarehouseOptionType)
|
|
||||||
?.value;
|
|
||||||
formik.setFieldValue(
|
formik.setFieldValue(
|
||||||
'destination_warehouse_id',
|
'destination_warehouse_id',
|
||||||
newDestinationWarehouseId
|
newDestinationWarehouseId
|
||||||
);
|
);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
newDestinationWarehouseId &&
|
formik.errors.destination_warehouse_id ===
|
||||||
formik.values.source_warehouse_id &&
|
'Gudang tujuan tidak boleh sama dengan gudang asal!'
|
||||||
newDestinationWarehouseId ===
|
|
||||||
formik.values.source_warehouse_id
|
|
||||||
) {
|
) {
|
||||||
formik.setFieldError(
|
formik.setFieldError('destination_warehouse_id', undefined);
|
||||||
'destination_warehouse_id',
|
|
||||||
'Gudang tujuan tidak boleh sama dengan gudang asal!'
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
if (
|
|
||||||
formik.errors.destination_warehouse_id ===
|
|
||||||
'Gudang tujuan tidak boleh sama dengan gudang asal!'
|
|
||||||
) {
|
|
||||||
formik.setFieldError(
|
|
||||||
'destination_warehouse_id',
|
|
||||||
undefined
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
options={warehouseOptions}
|
options={warehouseOptions}
|
||||||
@@ -1701,7 +1725,11 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => {
|
|||||||
hasInvalidQty ||
|
hasInvalidQty ||
|
||||||
hasExceededStock ||
|
hasExceededStock ||
|
||||||
!formik.isValid ||
|
!formik.isValid ||
|
||||||
formik.isSubmitting
|
formik.isSubmitting ||
|
||||||
|
(formik.values.source_warehouse_id ===
|
||||||
|
formik.values.destination_warehouse_id &&
|
||||||
|
formik.values.source_warehouse_id !== 0 &&
|
||||||
|
formik.values.destination_warehouse_id !== 0)
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
Submit
|
Submit
|
||||||
|
|||||||
Reference in New Issue
Block a user