From 44a5c510230b8c656d32db30568315b8a4f3f4c7 Mon Sep 17 00:00:00 2001 From: rstubryan Date: Tue, 10 Mar 2026 14:02:07 +0700 Subject: [PATCH] refactor(FE): Refactor recording restriction logic for clarity and accuracy --- .../production/recording/RecordingTable.tsx | 12 +++++- .../recording/form/RecordingForm.tsx | 31 +++++++-------- .../production/recording/recording-utils.ts | 39 ++++++++++--------- 3 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/components/pages/production/recording/RecordingTable.tsx b/src/components/pages/production/recording/RecordingTable.tsx index 18fe6ae8..6ca8bb89 100644 --- a/src/components/pages/production/recording/RecordingTable.tsx +++ b/src/components/pages/production/recording/RecordingTable.tsx @@ -107,9 +107,13 @@ const RowOptionsMenu = ({ }; const isRecordingEditable = (recording: Recording) => { + const currentIsLaying = + recording.project_flock?.project_flock_category === 'LAYING'; + const restriction = getRecordingRestriction( recording.is_laying, - recording.is_transition + recording.is_transition, + currentIsLaying ); if (restriction.isLocked) { @@ -119,9 +123,13 @@ const RowOptionsMenu = ({ }; const getRecordingRestrictionInfo = (recording: Recording) => { + const currentIsLaying = + recording.project_flock?.project_flock_category === 'LAYING'; + return getRecordingRestriction( recording.is_laying, - recording.is_transition + recording.is_transition, + currentIsLaying ); }; diff --git a/src/components/pages/production/recording/form/RecordingForm.tsx b/src/components/pages/production/recording/form/RecordingForm.tsx index 2a2262b0..e25f0f67 100644 --- a/src/components/pages/production/recording/form/RecordingForm.tsx +++ b/src/components/pages/production/recording/form/RecordingForm.tsx @@ -545,27 +545,24 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { type, ]); - const isRecordingEditable = useCallback( - (recording?: Recording) => { - if (!recording) return true; + const isRecordingEditable = useCallback((recording?: Recording) => { + if (!recording) return true; - const currentIsLaying = - projectFlockKandangDetail?.project_flock?.category === 'LAYING'; + const currentIsLaying = + recording.project_flock?.project_flock_category === 'LAYING'; - const restriction = getRecordingRestriction( - recording.is_laying, - recording.is_transition, - currentIsLaying - ); + const restriction = getRecordingRestriction( + recording.is_laying, + recording.is_transition, + currentIsLaying + ); - if (restriction.isLocked) { - return false; - } + if (restriction.isLocked) { + return false; + } - return true; - }, - [projectFlockKandangDetail] - ); + return true; + }, []); const { options: stockProductOptions, diff --git a/src/components/pages/production/recording/recording-utils.ts b/src/components/pages/production/recording/recording-utils.ts index 53bd94ad..c1fc279d 100644 --- a/src/components/pages/production/recording/recording-utils.ts +++ b/src/components/pages/production/recording/recording-utils.ts @@ -22,16 +22,17 @@ export const getRecordingRestriction = ( }; } - if (!isLaying) { - if (isTransition) { - return { - canEditStock: true, - canEditDepletion: false, - canEditEgg: false, - isLocked: false, - lockReason: undefined, - }; - } + if (isTransition && !isLaying) { + return { + canEditStock: true, + canEditDepletion: false, + canEditEgg: false, + isLocked: false, + lockReason: undefined, + }; + } + + if (!isLaying && !isTransition) { return { canEditStock: true, canEditDepletion: true, @@ -40,21 +41,21 @@ export const getRecordingRestriction = ( lockReason: undefined, }; } - - if (isTransition) { + if (isLaying && !isTransition) { return { - canEditStock: false, + canEditStock: true, canEditDepletion: true, - canEditEgg: false, + canEditEgg: true, isLocked: false, lockReason: undefined, }; } + return { - canEditStock: true, - canEditDepletion: true, - canEditEgg: true, - isLocked: false, - lockReason: undefined, + canEditStock: false, + canEditDepletion: false, + canEditEgg: false, + isLocked: true, + lockReason: 'Kondisi transisi tidak valid', }; };