From 2425316fea3a029666a86e3b44f021101a8a398d Mon Sep 17 00:00:00 2001 From: rstubryan Date: Mon, 2 Feb 2026 11:54:19 +0700 Subject: [PATCH] refactor(FE): Exclude already selected products from options --- .../recording/form/RecordingForm.tsx | 66 ++++++++++++++++++- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/src/components/pages/production/recording/form/RecordingForm.tsx b/src/components/pages/production/recording/form/RecordingForm.tsx index f3d6d2f9..3b3ce302 100644 --- a/src/components/pages/production/recording/form/RecordingForm.tsx +++ b/src/components/pages/production/recording/form/RecordingForm.tsx @@ -1196,6 +1196,66 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { [stockProducts, depletionProductsData, eggProductsData, initialValues, type] ); + const getAvailableStockProductOptions = useCallback( + (currentIdx: number) => { + const selectedProductIds = + formik.values.stocks + ?.filter((s, idx) => { + return ( + idx !== currentIdx && + s.product_warehouse_id && + s.product_warehouse_id !== 0 + ); + }) + .map((s) => s.product_warehouse_id) || []; + + return unifiedStockProducts.filter( + (opt) => !selectedProductIds.includes(Number(opt.value)) + ); + }, + [formik.values.stocks, unifiedStockProducts] + ); + + const getAvailableDepletionProductOptions = useCallback( + (currentIdx: number) => { + const selectedProductIds = + formik.values.depletions + ?.filter((d, idx) => { + return ( + idx !== currentIdx && + d.product_warehouse_id && + d.product_warehouse_id !== 0 + ); + }) + .map((d) => d.product_warehouse_id) || []; + + return depletionProducts.filter( + (opt) => !selectedProductIds.includes(Number(opt.value)) + ); + }, + [formik.values.depletions, depletionProducts] + ); + + const getAvailableEggProductOptions = useCallback( + (currentIdx: number) => { + const selectedProductIds = + (formik.values as RecordingLayingFormValues).eggs + ?.filter((e, idx) => { + return ( + idx !== currentIdx && + e.product_warehouse_id && + e.product_warehouse_id !== 0 + ); + }) + .map((e) => e.product_warehouse_id) || []; + + return eggProducts.filter( + (opt) => !selectedProductIds.includes(Number(opt.value)) + ); + }, + [formik.values, eggProducts] + ); + const hasExceededStock = useMemo(() => { if ((type as 'add' | 'edit' | 'detail') === 'detail') return false; return ( @@ -2398,7 +2458,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { option?.value || 0 ); }} - options={unifiedStockProducts} + options={getAvailableStockProductOptions(idx)} placeholder={ !formik.values.project_flock_kandang_id ? 'Pilih kandang terlebih dahulu' @@ -2619,7 +2679,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { option?.value || 0 ); }} - options={depletionProducts} + options={getAvailableDepletionProductOptions(idx)} placeholder='Pilih Kondisi' isLoading={isLoadingDepletionProducts} onMenuScrollToBottom={loadMoreDepletionProducts} @@ -2837,7 +2897,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { option?.value || 0 ); }} - options={eggProducts} + options={getAvailableEggProductOptions(idx)} placeholder='Pilih Kondisi Telur' isLoading={isLoadingEggProducts} onMenuScrollToBottom={loadMoreEggProducts}