From df875eda1d19888cfa53504d177e416529a4d16e Mon Sep 17 00:00:00 2001 From: rstubryan Date: Fri, 23 Jan 2026 16:05:59 +0700 Subject: [PATCH] refactor(FE): Exclude selected products from product options --- .../inventory/movement/form/MovementForm.tsx | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/components/pages/inventory/movement/form/MovementForm.tsx b/src/components/pages/inventory/movement/form/MovementForm.tsx index 817facf1..e07dcd78 100644 --- a/src/components/pages/inventory/movement/form/MovementForm.tsx +++ b/src/components/pages/inventory/movement/form/MovementForm.tsx @@ -467,7 +467,7 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { }, ]; formik.setFieldValue('products', newProducts); - }, []); + }, [formik.values.products]); const removeProduct = useCallback( (i: number) => { @@ -476,12 +476,14 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { ); formik.setFieldValue('products', updatedProducts); + setSelectedProducts([]); + if (productQtyErrorShown) { toast.dismiss(); setProductQtyErrorShown(false); } }, - [formik.values.products, productQtyErrorShown] + [formik.values.products, productQtyErrorShown, setSelectedProducts] ); const bulkRemoveProduct = useCallback(() => { @@ -725,7 +727,20 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { [handleDeliveryCostPerItemChange] ); - // UTILITY FUNCTIONS + const getAvailableProductOptions = useCallback( + (currentIdx: number) => { + const selectedProductIds = + formik.values.products + ?.filter((p, idx) => idx !== currentIdx && p.product_id !== 0) + .map((p) => p.product_id) || []; + + return productWarehouseOptions.filter( + (pw) => !selectedProductIds.includes(pw.product_id) + ); + }, + [formik.values.products, productWarehouseOptions] + ); + const getFilteredProductWarehouseOptions = useCallback(() => { return ( formik.values.products @@ -1468,7 +1483,7 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { required value={product.product ?? undefined} onChange={(val) => handleProductChange(idx, val)} - options={productWarehouseOptions} + options={getAvailableProductOptions(idx)} onInputChange={setProductWarehouseSelectInputValue} onMenuScrollToBottom={loadMoreProductWarehouses} isLoading={isLoadingProductWarehouses}