mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 07:45:47 +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]
|
[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(() => {
|
const hasExceededStock = useMemo(() => {
|
||||||
if ((type as 'add' | 'edit' | 'detail') === 'detail') return false;
|
if ((type as 'add' | 'edit' | 'detail') === 'detail') return false;
|
||||||
return (
|
return (
|
||||||
@@ -2398,7 +2458,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
|||||||
option?.value || 0
|
option?.value || 0
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
options={unifiedStockProducts}
|
options={getAvailableStockProductOptions(idx)}
|
||||||
placeholder={
|
placeholder={
|
||||||
!formik.values.project_flock_kandang_id
|
!formik.values.project_flock_kandang_id
|
||||||
? 'Pilih kandang terlebih dahulu'
|
? 'Pilih kandang terlebih dahulu'
|
||||||
@@ -2619,7 +2679,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
|||||||
option?.value || 0
|
option?.value || 0
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
options={depletionProducts}
|
options={getAvailableDepletionProductOptions(idx)}
|
||||||
placeholder='Pilih Kondisi'
|
placeholder='Pilih Kondisi'
|
||||||
isLoading={isLoadingDepletionProducts}
|
isLoading={isLoadingDepletionProducts}
|
||||||
onMenuScrollToBottom={loadMoreDepletionProducts}
|
onMenuScrollToBottom={loadMoreDepletionProducts}
|
||||||
@@ -2837,7 +2897,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
|||||||
option?.value || 0
|
option?.value || 0
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
options={eggProducts}
|
options={getAvailableEggProductOptions(idx)}
|
||||||
placeholder='Pilih Kondisi Telur'
|
placeholder='Pilih Kondisi Telur'
|
||||||
isLoading={isLoadingEggProducts}
|
isLoading={isLoadingEggProducts}
|
||||||
onMenuScrollToBottom={loadMoreEggProducts}
|
onMenuScrollToBottom={loadMoreEggProducts}
|
||||||
|
|||||||
Reference in New Issue
Block a user