diff --git a/src/components/pages/production/transfer-to-laying/TransferToLayingFormModal.tsx b/src/components/pages/production/transfer-to-laying/TransferToLayingFormModal.tsx index 9e2e61ad..96c172a2 100644 --- a/src/components/pages/production/transfer-to-laying/TransferToLayingFormModal.tsx +++ b/src/components/pages/production/transfer-to-laying/TransferToLayingFormModal.tsx @@ -205,11 +205,9 @@ const TransferToLayingFormModal = () => { const { formErrorList, close, handleFormSubmit } = useFormikErrorList(formik); - const selectedFlockSourceRawData = isResponseSuccess(flockSourceRawData) - ? flockSourceRawData.data.find( - (item) => item.id === formik.values.flockSource?.value - ) - : undefined; + const [selectedFlockSourceRawData, setSelectedFlockSourceRawData] = useState< + ProjectFlock | undefined + >(undefined); const selectedFlockDestinationRawData = isResponseSuccess( flockDestinationRawData @@ -375,467 +373,418 @@ const TransferToLayingFormModal = () => { } }; + const getFlockSourceData = async () => { + if (transferToLayingId && isResponseSuccess(transferToLaying)) { + const singleFlockSourceRawData = await ProjectFlockApi.getSingle( + transferToLaying.data.from_project_flock.id + ); + + if (isResponseSuccess(singleFlockSourceRawData)) { + setSelectedFlockSourceRawData(singleFlockSourceRawData.data); + } + } + }; + getFilledInitialValues(); + getFlockSourceData(); }, [transferToLayingId, transferToLaying]); + useEffect(() => { + if (isResponseSuccess(flockSourceRawData)) { + const selectedFlockSourceRawData = flockSourceRawData.data.find( + (item) => item.id === formik.values.flockSource?.value + ); + + setSelectedFlockSourceRawData(selectedFlockSourceRawData); + } + }, [flockSourceRawData]); + useEffect(() => { formik.setFieldValue('totalQuantity', totalTransferedChicken); formik.setFieldValue('maxTotalQuantity', totalTransferedChicken); }, [totalTransferedChicken]); return ( - -
+ - {/* 1st Section */} -
-
- - -
- -

- Add Transfer to Laying -

-
- -
-

- Informasi Umum -

- - 2} - /> - - 2} - /> - - 2} - /> -
- - {step >= 2 && ( - <> -
-

- Pilih Kandang Asal -

- -
- - -
- - -
- -
- {mappedFlockSourceKandangsAvailability.map( - (item, itemIdx) => { - const isAvailable = item.available_qty > 0; - const isChecked = formik.values.flockSourceKandangs.some( - (k) => k.kandang.value === item.project_flock_kandang_id - ); - - const flockSourceKandangCheckboxChangeHandler: FormEventHandler< - HTMLInputElement - > = (e) => { - const checked = (e.target as HTMLInputElement).checked; - if (checked) { - formik.setFieldValue('flockSourceKandangs', [ - ...formik.values.flockSourceKandangs, - { - kandang: { - value: item.project_flock_kandang_id, - label: item.kandang_name, - }, - quantity: '', - maxQuantity: item.available_qty, - }, - ]); - } else { - formik.setFieldValue( - 'flockSourceKandangs', - formik.values.flockSourceKandangs.filter( - (k) => - k.kandang.value !== - item.project_flock_kandang_id - ) - ); - } - }; - - return ( -
-
- - - -
- - -
- ); - } - )} -
-
- -
-
-

- Pilih Kandang Tujuan -

- {formik.touched.flockDestinationKandangs && - formik.errors.flockDestinationKandangs && - typeof formik.errors.flockDestinationKandangs === - 'string' && ( - - {formik.errors.flockDestinationKandangs} - - )} -
- -
- - -
- - -
- -
- {selectedFlockDestinationRawData?.kandangs.map( - (item, itemIdx) => { - // TODO: change this to real available quota later - const isAvailable = item.capacity > 0; - const isChecked = - formik.values.flockDestinationKandangs.some( - (k) => - k.kandang.value === item.project_flock_kandang_id - ); - - const flockDestinationKandangCheckboxChangeHandler: FormEventHandler< - HTMLInputElement - > = (e) => { - const checked = (e.target as HTMLInputElement).checked; - if (checked) { - formik.setFieldValue('flockDestinationKandangs', [ - ...formik.values.flockDestinationKandangs, - { - kandang: { - value: item.project_flock_kandang_id, - label: item.name, - }, - quantity: '', - // TODO: change this to real available quota later - maxQuantity: item.capacity, - }, - ]); - } else { - formik.setFieldValue( - 'flockDestinationKandangs', - formik.values.flockDestinationKandangs.filter( - (k) => - k.kandang.value !== - item.project_flock_kandang_id - ) - ); - } - }; - - return ( -
-
- - - -
- - -
- ); - } - )} -
-
- - )} - -
- {step < 3 && ( - - )} -
-
- - {/* 2nd Section */} - {step === 3 && ( -
-
-

- Tambah Kuantitas -

- + + {/* 1st Section */} +
+
+ +
+ +

+ Add Transfer to Laying +

- Informasi Kandang + Informasi Umum

- {/* Source Kandang */} -
- - Kandang Asal - + 2} + /> - {formik.values.flockSourceKandangs.length === 0 && ( - - Belum ada kandang asal yang dipilih - - )} + 2} + /> - {formik.values.flockSourceKandangs.length > 0 && ( -
- {formik.values.flockSourceKandangs.map((item, index) => { - const isInvalid = - item.quantity === '' - ? false - : Boolean( - getIn( - formik.errors, - `flockSourceKandangs[${index}].quantity` + 2} + /> +
+ + {step >= 2 && ( + <> +
+

+ Pilih Kandang Asal +

+ +
+ + +
+ + +
+ +
+ {mappedFlockSourceKandangsAvailability.map( + (item, itemIdx) => { + const isAvailable = item.available_qty > 0; + const isChecked = + formik.values.flockSourceKandangs.some( + (k) => + k.kandang.value === item.project_flock_kandang_id + ); + + const flockSourceKandangCheckboxChangeHandler: FormEventHandler< + HTMLInputElement + > = (e) => { + const checked = (e.target as HTMLInputElement) + .checked; + if (checked) { + formik.setFieldValue('flockSourceKandangs', [ + ...formik.values.flockSourceKandangs, + { + kandang: { + value: item.project_flock_kandang_id, + label: item.kandang_name, + }, + quantity: '', + maxQuantity: item.available_qty, + }, + ]); + } else { + formik.setFieldValue( + 'flockSourceKandangs', + formik.values.flockSourceKandangs.filter( + (k) => + k.kandang.value !== + item.project_flock_kandang_id ) ); - - const errorMessage = getIn( - formik.errors, - `flockSourceKandangs[${index}].quantity` - ); - - return ( - - - {item.kandang.label} - - -
-
} - className={{ - inputPrefix: - 'py-0 px-0 pl-3 text-base-content/50 bg-transparent border-r-0', - inputPrefixSuffixWrapper: 'grid grid-cols-2', - inputWrapper: 'border-l-0 pl-5', - }} - /> - ); - })} + }; + + return ( +
+
+ + + +
+ + +
+ ); + } + )}
- )} +
+ +
+
+

+ Pilih Kandang Tujuan +

+ {formik.touched.flockDestinationKandangs && + formik.errors.flockDestinationKandangs && + typeof formik.errors.flockDestinationKandangs === + 'string' && ( + + {formik.errors.flockDestinationKandangs} + + )} +
+ +
+ + +
+ + +
+ +
+ {selectedFlockDestinationRawData?.kandangs.map( + (item, itemIdx) => { + // TODO: change this to real available quota later + const isAvailable = item.capacity > 0; + const isChecked = + formik.values.flockDestinationKandangs.some( + (k) => + k.kandang.value === item.project_flock_kandang_id + ); + + const flockDestinationKandangCheckboxChangeHandler: FormEventHandler< + HTMLInputElement + > = (e) => { + const checked = (e.target as HTMLInputElement) + .checked; + if (checked) { + formik.setFieldValue('flockDestinationKandangs', [ + ...formik.values.flockDestinationKandangs, + { + kandang: { + value: item.project_flock_kandang_id, + label: item.name, + }, + quantity: '', + // TODO: change this to real available quota later + maxQuantity: item.capacity, + }, + ]); + } else { + formik.setFieldValue( + 'flockDestinationKandangs', + formik.values.flockDestinationKandangs.filter( + (k) => + k.kandang.value !== + item.project_flock_kandang_id + ) + ); + } + }; + + return ( +
+
+ + + +
+ + +
+ ); + } + )} +
+
+ + )} + +
+ {step < 3 && ( + + )} +
+
+ + {/* 2nd Section */} + {step === 3 && ( +
+
+

+ Tambah Kuantitas +

+ +
- {/* Destination Kandang */} -
- - Kandang Tujuan +
+

+ Informasi Kandang +

-
- - - - - {formik.values.flockDestinationKandangs.length === 0 && ( - - Belum ada kandang tujuan yang dipilih + {/* Source Kandang */} +
+ + Kandang Asal - )} - {formik.values.flockDestinationKandangs.length > 0 && ( -
- {formik.values.flockDestinationKandangs.map( - (item, index) => { + {formik.values.flockSourceKandangs.length === 0 && ( + + Belum ada kandang asal yang dipilih + + )} + + {formik.values.flockSourceKandangs.length > 0 && ( +
+ {formik.values.flockSourceKandangs.map((item, index) => { const isInvalid = item.quantity === '' ? false : Boolean( getIn( formik.errors, - `flockDestinationKandangs[${index}].quantity` + `flockSourceKandangs[${index}].quantity` ) ); const errorMessage = getIn( formik.errors, - `flockDestinationKandangs[${index}].quantity` + `flockSourceKandangs[${index}].quantity` ); return ( { }} /> ); + })} +
+ )} +
+ + {/* Destination Kandang */} +
+ + Kandang Tujuan + +
+ + + + + {formik.values.flockDestinationKandangs.length === 0 && ( + + Belum ada kandang tujuan yang dipilih + + )} + + {formik.values.flockDestinationKandangs.length > 0 && ( +
+ {formik.values.flockDestinationKandangs.map( + (item, index) => { + const isInvalid = + item.quantity === '' + ? false + : Boolean( + getIn( + formik.errors, + `flockDestinationKandangs[${index}].quantity` + ) + ); + + const errorMessage = getIn( + formik.errors, + `flockDestinationKandangs[${index}].quantity` + ); + + return ( + + + {item.kandang.label} + + +
+
+ } + className={{ + inputPrefix: + 'py-0 px-0 pl-3 text-base-content/50 bg-transparent border-r-0', + inputPrefixSuffixWrapper: 'grid grid-cols-2', + inputWrapper: 'border-l-0 pl-5', + }} + /> + ); + } + )} +
+ )} +
+
+ +
+

+ Informasi Umum +

+ + + +