From 811850857d02df1d0add6705271f50478f5d2f61 Mon Sep 17 00:00:00 2001 From: rstubryan Date: Wed, 11 Mar 2026 10:18:19 +0700 Subject: [PATCH] refactor(FE): Restrict stock and depletion edits based on permissions --- .../recording/form/RecordingForm.tsx | 467 +++++++++--------- 1 file changed, 241 insertions(+), 226 deletions(-) diff --git a/src/components/pages/production/recording/form/RecordingForm.tsx b/src/components/pages/production/recording/form/RecordingForm.tsx index 1f1475e7..9d90cdf2 100644 --- a/src/components/pages/production/recording/form/RecordingForm.tsx +++ b/src/components/pages/production/recording/form/RecordingForm.tsx @@ -1043,6 +1043,14 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { }; } + if (!recordingRestriction.canEditStock) { + baseValues.stocks = []; + } + + if (!recordingRestriction.canEditDepletion) { + baseValues.depletions = []; + } + return baseValues; }, [ initialValues, @@ -1053,6 +1061,8 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { selectedLocation, selectedProjectFlock, selectedKandang, + recordingRestriction.canEditStock, + recordingRestriction.canEditDepletion, ]); const formik = useFormik< @@ -2443,86 +2453,39 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { )} - {/* Stocks Table */} - -
- - - - {(type as 'add' | 'edit' | 'detail') !== 'detail' && ( - - )} - - - {(type as 'add' | 'edit' | 'detail') !== 'detail' && ( - - )} - - - - {formik.values.stocks?.map((stock, idx) => ( - + {/* Stocks Table - Only show if can edit stock or has data */} + {(recordingRestriction.canEditStock || + (type === 'detail' && formik.values.stocks?.length > 0)) && ( + +
+
- 0 - } - onChange={( - e: React.ChangeEvent - ) => { - if (e.target.checked) { - setSelectedStocks( - formik.values.stocks?.map((_, idx) => idx) ?? [] - ); - } else { - setSelectedStocks([]); - } - }} - disabled={!recordingRestriction.canEditStock} - classNames={{ - wrapper: 'flex justify-center', - checkbox: 'checkbox checkbox-sm', - }} - /> - - Persediaan - - * - - - Jumlah Pakai - - * - - Action
+ + {(type as 'add' | 'edit' | 'detail') !== 'detail' && ( - )} - - + + {(type as 'add' | 'edit' | 'detail') !== 'detail' && ( - + )} - ))} - -
+ 0 + } onChange={( e: React.ChangeEvent ) => { if (e.target.checked) { - setSelectedStocks([...selectedStocks, idx]); - } else { setSelectedStocks( - selectedStocks.filter((i) => i !== idx) + formik.values.stocks?.map((_, idx) => idx) ?? + [] ); + } else { + setSelectedStocks([]); } }} disabled={!recordingRestriction.canEditStock} @@ -2531,169 +2494,221 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { checkbox: 'checkbox checkbox-sm', }} /> - + - - product.value === stock.product_warehouse_id - ) || null - } - onChange={(selectedOption) => { - const option = selectedOption as OptionType | null; - formik.setFieldValue( - `stocks.${idx}.product_warehouse_id`, - option?.value || 0 - ); - }} - options={getAvailableStockProductOptions(idx)} - placeholder={ - !formik.values.project_flock_kandang_id - ? 'Pilih kandang terlebih dahulu' - : 'Pilih Produk' - } - isLoading={isLoadingStockProducts} - onMenuScrollToBottom={loadMoreStockProducts} - isError={ - isRepeaterInputError( - 'stocks', - 'product_warehouse_id', - idx - ).isError - } - errorMessage={ - isRepeaterInputError( - 'stocks', - 'product_warehouse_id', - idx - ).errorMessage - } - className={{ - wrapper: 'w-full min-w-48', - }} - isSearchable - isDisabled={ - type === 'detail' || - !formik.values.project_flock_kandang_id || - !recordingRestriction.canEditStock - } - isClearable={type !== 'detail'} - inputPrefix={ - stock.product_warehouse_id - ? getProductFlagBadgeAdornment( - stock.product_warehouse_id - ) - : undefined - } - /> - -
- - {getStockUsageAdornment(idx)} -
-
+ Persediaan + + * + + + Jumlah Pakai + + * + + -
- -
-
Action
-
- {(type as 'add' | 'edit' | 'detail') !== 'detail' && ( -
- {selectedStocks.length > 0 && - recordingRestriction.canEditStock && ( + + + {formik.values.stocks?.map((stock, idx) => ( + + {(type as 'add' | 'edit' | 'detail') !== 'detail' && ( + + + ) => { + if (e.target.checked) { + setSelectedStocks([...selectedStocks, idx]); + } else { + setSelectedStocks( + selectedStocks.filter((i) => i !== idx) + ); + } + }} + disabled={!recordingRestriction.canEditStock} + classNames={{ + wrapper: 'flex justify-center', + checkbox: 'checkbox checkbox-sm', + }} + /> + + )} + + + product.value === stock.product_warehouse_id + ) || null + } + onChange={(selectedOption) => { + const option = + selectedOption as OptionType | null; + formik.setFieldValue( + `stocks.${idx}.product_warehouse_id`, + option?.value || 0 + ); + }} + options={getAvailableStockProductOptions(idx)} + placeholder={ + !formik.values.project_flock_kandang_id + ? 'Pilih kandang terlebih dahulu' + : 'Pilih Produk' + } + isLoading={isLoadingStockProducts} + onMenuScrollToBottom={loadMoreStockProducts} + isError={ + isRepeaterInputError( + 'stocks', + 'product_warehouse_id', + idx + ).isError + } + errorMessage={ + isRepeaterInputError( + 'stocks', + 'product_warehouse_id', + idx + ).errorMessage + } + className={{ + wrapper: 'w-full min-w-48', + }} + isSearchable + isDisabled={ + type === 'detail' || + !formik.values.project_flock_kandang_id || + !recordingRestriction.canEditStock + } + isClearable={type !== 'detail'} + inputPrefix={ + stock.product_warehouse_id + ? getProductFlagBadgeAdornment( + stock.product_warehouse_id + ) + : undefined + } + /> + + +
+ + {getStockUsageAdornment(idx)} +
+ + {(type as 'add' | 'edit' | 'detail') !== 'detail' && ( + +
+ +
+ + )} + + ))} + + +
+ {(type as 'add' | 'edit' | 'detail') !== 'detail' && ( +
+ {selectedStocks.length > 0 && + recordingRestriction.canEditStock && ( + + )} + - )} - - - -
- )} -
+ + + )} + + )} {/* Transition Warning Banner -- MOVED UP -- */} {isTransitionPeriod && (