From fcbb34624d333dfc475d3be42b78df6346ef92f3 Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Tue, 27 Jan 2026 18:17:13 +0700 Subject: [PATCH] fix: get real max quantity in target project flock kandang --- .../TransferToLayingFormModal.tsx | 64 +++++++++++++++---- 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/src/components/pages/production/transfer-to-laying/TransferToLayingFormModal.tsx b/src/components/pages/production/transfer-to-laying/TransferToLayingFormModal.tsx index 9f71fdc5..3ade0946 100644 --- a/src/components/pages/production/transfer-to-laying/TransferToLayingFormModal.tsx +++ b/src/components/pages/production/transfer-to-laying/TransferToLayingFormModal.tsx @@ -288,6 +288,48 @@ const TransferToLayingFormModal = () => { return { available: countAvailable, unavailable: countUnavailable }; }, [mappedFlockSourceKandangsAvailability]); + const { + data: flockDestinationKandangsMaxTargetQty, + isLoading: isLoadingFlockDestinationKandangsMaxTargetQty, + } = useSWR( + formik.values.flockDestination + ? [ + 'transfer-to-laying', + 'max-target-qty', + String(formik.values.flockDestination.value), + ] + : undefined, + ([, , id]: string[]) => + TransferToLayingApi.getMappedFlockKandangsMaxTargetQty(Number(id)) + ); + + const mappedFlockDestinationKandangsMaxTargetQty: { + kandang_name: string; + max_target_qty: number; + project_flock_kandang_id: number; + }[] = useMemo(() => { + if ( + !flockDestinationKandangsMaxTargetQty || + !selectedFlockDestinationRawData + ) + return []; + + return selectedFlockDestinationRawData + ? selectedFlockDestinationRawData.kandangs.map((kandang) => { + const maxQty = + flockDestinationKandangsMaxTargetQty[ + kandang.project_flock_kandang_id + ]?.max_target_qty; + + return { + kandang_name: kandang.name, + max_target_qty: maxQty, + project_flock_kandang_id: kandang.project_flock_kandang_id, + }; + }) + : []; + }, [flockDestinationKandangsMaxTargetQty, selectedFlockDestinationRawData]); + const mappedFlockDestinationKandangsAvailabilityInfo: { available: number; unavailable: number; @@ -298,9 +340,8 @@ const TransferToLayingFormModal = () => { let countAvailable = 0; let countUnavailable = 0; - selectedFlockDestinationRawData?.kandangs.forEach((item) => { - // TODO: change this to real available quota later - if (item.capacity > 0) { + mappedFlockDestinationKandangsMaxTargetQty.forEach((item) => { + if (item.max_target_qty > 0) { countAvailable += 1; } else { countUnavailable += 1; @@ -308,7 +349,7 @@ const TransferToLayingFormModal = () => { }); return { available: countAvailable, unavailable: countUnavailable }; - }, [selectedFlockDestinationRawData]); + }, [mappedFlockDestinationKandangsMaxTargetQty]); const totalEnteredChickenForTransfer = formik.values.flockSourceKandangs.reduce( @@ -648,10 +689,9 @@ const TransferToLayingFormModal = () => {
- {selectedFlockDestinationRawData?.kandangs.map( + {mappedFlockDestinationKandangsMaxTargetQty.map( (item, itemIdx) => { - // TODO: change this to real available quota later - const isAvailable = item.capacity > 0; + const isAvailable = item.max_target_qty > 0; const isChecked = formik.values.flockDestinationKandangs.some( (k) => @@ -669,11 +709,10 @@ const TransferToLayingFormModal = () => { { kandang: { value: item.project_flock_kandang_id, - label: item.name, + label: item.kandang_name, }, quantity: '', - // TODO: change this to real available quota later - maxQuantity: item.capacity, + maxQuantity: item.max_target_qty, }, ]); } else { @@ -718,9 +757,8 @@ const TransferToLayingFormModal = () => { 'cursor-not-allowed': !isAvailable, })} > - {item.name}{' '} - {/* TODO: change this to real available quota later */} - {`(Max: ${item.capacity})`} + {item.kandang_name}{' '} + {`(Max: ${item.max_target_qty})`}