From bac6766fa2bf68453a2026cfb7faa3a9a24d1797 Mon Sep 17 00:00:00 2001 From: rstubryan Date: Sun, 8 Mar 2026 16:06:13 +0700 Subject: [PATCH 1/8] refactor(FE): Refactor transfer logic to use maxSourceQuantity state --- .../TransferToLayingFormModal.tsx | 70 ++++++++----------- 1 file changed, 31 insertions(+), 39 deletions(-) diff --git a/src/components/pages/production/transfer-to-laying/TransferToLayingFormModal.tsx b/src/components/pages/production/transfer-to-laying/TransferToLayingFormModal.tsx index 2487f10b..b01a479d 100644 --- a/src/components/pages/production/transfer-to-laying/TransferToLayingFormModal.tsx +++ b/src/components/pages/production/transfer-to-laying/TransferToLayingFormModal.tsx @@ -229,6 +229,8 @@ const TransferToLayingFormModal = () => { ProjectFlock | undefined >(undefined); + const [maxSourceQuantity, setMaxSourceQuantity] = useState(0); + const selectedFlockDestinationRawData = isResponseSuccess( flockDestinationRawData ) @@ -353,19 +355,14 @@ const TransferToLayingFormModal = () => { return { available: countAvailable, unavailable: countUnavailable }; }, [mappedFlockDestinationKandangsMaxTargetQty]); - const totalEnteredChickenForTransfer = - formik.values.flockSourceKandangs.reduce( - (acc, item) => acc + Number(item.quantity), - 0 - ); - const totalTransferedChicken = formik.values.flockDestinationKandangs.reduce( (acc, item) => acc + Number(item.quantity), 0 ); + // Sisa transfer = Max available dari kandang asal - Total yang sudah diisi di kandang tujuan const totalAvailableChickenForTransfer = - totalEnteredChickenForTransfer - totalTransferedChicken; + maxSourceQuantity - totalTransferedChicken; const isNextButtonDisabled = useMemo(() => { if (step === 1) { @@ -397,6 +394,7 @@ const TransferToLayingFormModal = () => { formik.setFieldValue('maxTotalQuantity', ''); formik.setFieldValue('reason', ''); formik.setFieldTouched('reason', false); + setMaxSourceQuantity(0); setStep(2); }; @@ -404,6 +402,7 @@ const TransferToLayingFormModal = () => { const flockSourceChangeHandler = (val: OptionType | OptionType[] | null) => { formik.setFieldValue('flockSource', val); formik.setFieldValue('flockSourceKandangs', []); + setMaxSourceQuantity(0); }; const flockDestinationChangeHandler = ( @@ -469,6 +468,16 @@ const TransferToLayingFormModal = () => { formik.setFieldValue('maxTotalQuantity', totalTransferedChicken); }, [totalTransferedChicken, formik.values.flockDestinationKandangs]); + // Auto-fill source kandang quantity from total destination quantity + useEffect(() => { + if (formik.values.flockSourceKandangs.length > 0) { + formik.setFieldValue( + 'flockSourceKandangs.0.quantity', + totalTransferedChicken + ); + } + }, [totalTransferedChicken]); + return ( <> { k.kandang.value === item.project_flock_kandang_id ); - const flockSourceKandangCheckboxChangeHandler: FormEventHandler< - HTMLInputElement - > = (e) => { - const checked = (e.target as HTMLInputElement) - .checked; - if (checked) { + const flockSourceKandangRadioChangeHandler = () => { + if (isAvailable) { formik.setFieldValue('flockSourceKandangs', [ - ...formik.values.flockSourceKandangs, { kandang: { value: item.project_flock_kandang_id, @@ -600,15 +604,7 @@ const TransferToLayingFormModal = () => { maxQuantity: item.available_qty, }, ]); - } else { - formik.setFieldValue( - 'flockSourceKandangs', - formik.values.flockSourceKandangs.filter( - (k) => - k.kandang.value !== - item.project_flock_kandang_id - ) - ); + setMaxSourceQuantity(item.available_qty); } }; @@ -618,28 +614,22 @@ const TransferToLayingFormModal = () => { className='w-full p-3 flex flex-row items-center justify-between' >
-