mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 21:41:57 +00:00
refactor(FE): Exclude already selected products from options
This commit is contained in:
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user