mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 07:45:47 +00:00
refactor(FE-62): remove unused product fetching logic from MovementForm
This commit is contained in:
@@ -28,7 +28,6 @@ import { useMovementFormHandlers } from './useMovementFormHandlers';
|
|||||||
import {
|
import {
|
||||||
SupplierApi,
|
SupplierApi,
|
||||||
WarehouseApi,
|
WarehouseApi,
|
||||||
ProductApi,
|
|
||||||
} from '@/services/api/master-data';
|
} from '@/services/api/master-data';
|
||||||
import { ProductWarehouseApi } from '@/services/api/inventory';
|
import { ProductWarehouseApi } from '@/services/api/inventory';
|
||||||
import { toast } from 'react-hot-toast';
|
import { toast } from 'react-hot-toast';
|
||||||
@@ -47,9 +46,6 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => {
|
|||||||
] = useState('');
|
] = useState('');
|
||||||
const [selectedProducts, setSelectedProducts] = useState<number[]>([]);
|
const [selectedProducts, setSelectedProducts] = useState<number[]>([]);
|
||||||
const [selectedDeliveries, setSelectedDeliveries] = useState<number[]>([]);
|
const [selectedDeliveries, setSelectedDeliveries] = useState<number[]>([]);
|
||||||
const [fetchedProductIds, setFetchedProductIds] = useState<Set<number>>(
|
|
||||||
new Set()
|
|
||||||
);
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
deleteModal,
|
deleteModal,
|
||||||
@@ -355,173 +351,6 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => {
|
|||||||
}
|
}
|
||||||
}, [formik.values.source_warehouse_id]);
|
}, [formik.values.source_warehouse_id]);
|
||||||
|
|
||||||
// Effect to populate product labels from ProductWarehouse data
|
|
||||||
useEffect(() => {
|
|
||||||
if (!productWarehouses || !isResponseSuccess(productWarehouses)) return;
|
|
||||||
if (type !== 'edit' && type !== 'detail') return;
|
|
||||||
|
|
||||||
let hasUpdates = false;
|
|
||||||
const updatedProducts = formik.values.products?.map((product) => {
|
|
||||||
if (product.product && product.product.label.startsWith('Product ID:')) {
|
|
||||||
const productWarehouse = productWarehouses.data.find(
|
|
||||||
(pw) => pw.product.id === product.product_id
|
|
||||||
);
|
|
||||||
if (productWarehouse) {
|
|
||||||
hasUpdates = true;
|
|
||||||
return {
|
|
||||||
...product,
|
|
||||||
product: {
|
|
||||||
value: productWarehouse.product.id,
|
|
||||||
label: productWarehouse.product.name,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return product;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (hasUpdates && updatedProducts) {
|
|
||||||
formik.setFieldValue('products', updatedProducts);
|
|
||||||
|
|
||||||
const updatedDeliveries = formik.values.deliveries?.map((delivery) => {
|
|
||||||
const updatedDeliveryProducts = delivery.products.map(
|
|
||||||
(deliveryProduct) => {
|
|
||||||
if (
|
|
||||||
deliveryProduct.product &&
|
|
||||||
deliveryProduct.product.label.startsWith('Product ID:')
|
|
||||||
) {
|
|
||||||
const productWarehouse = productWarehouses.data.find(
|
|
||||||
(pw) => pw.product.id === deliveryProduct.product_id
|
|
||||||
);
|
|
||||||
if (productWarehouse) {
|
|
||||||
return {
|
|
||||||
...deliveryProduct,
|
|
||||||
product: {
|
|
||||||
value: productWarehouse.product.id,
|
|
||||||
label: productWarehouse.product.name,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return deliveryProduct;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
return {
|
|
||||||
...delivery,
|
|
||||||
products: updatedDeliveryProducts,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
formik.setFieldValue('deliveries', updatedDeliveries);
|
|
||||||
}
|
|
||||||
}, [productWarehouses, type]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (type !== 'edit' && type !== 'detail') return;
|
|
||||||
|
|
||||||
const productIdsToFetch: number[] = [];
|
|
||||||
|
|
||||||
formik.values.products?.forEach((product) => {
|
|
||||||
if (
|
|
||||||
product.product &&
|
|
||||||
product.product.label.startsWith('Product ID:') &&
|
|
||||||
product.product_id > 0 &&
|
|
||||||
!fetchedProductIds.has(product.product_id)
|
|
||||||
) {
|
|
||||||
productIdsToFetch.push(product.product_id);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
formik.values.deliveries?.forEach((delivery) => {
|
|
||||||
delivery.products.forEach((deliveryProduct) => {
|
|
||||||
if (
|
|
||||||
deliveryProduct.product &&
|
|
||||||
deliveryProduct.product.label.startsWith('Product ID:') &&
|
|
||||||
deliveryProduct.product_id > 0 &&
|
|
||||||
!fetchedProductIds.has(deliveryProduct.product_id)
|
|
||||||
) {
|
|
||||||
if (!productIdsToFetch.includes(deliveryProduct.product_id)) {
|
|
||||||
productIdsToFetch.push(deliveryProduct.product_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
if (productIdsToFetch.length === 0) return;
|
|
||||||
|
|
||||||
const fetchProducts = async () => {
|
|
||||||
const productMap = new Map<number, { id: number; name: string }>();
|
|
||||||
const newFetchedIds = new Set(fetchedProductIds);
|
|
||||||
|
|
||||||
for (const productId of productIdsToFetch) {
|
|
||||||
try {
|
|
||||||
const response = await ProductApi.getSingle(productId);
|
|
||||||
if (isResponseSuccess(response)) {
|
|
||||||
const product = response.data;
|
|
||||||
productMap.set(product.id, { id: product.id, name: product.name });
|
|
||||||
newFetchedIds.add(productId);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Failed to fetch product ${productId}:`, error);
|
|
||||||
newFetchedIds.add(productId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (productMap.size > 0) {
|
|
||||||
const updatedProducts = formik.values.products?.map((p) => {
|
|
||||||
const productData = productMap.get(p.product_id);
|
|
||||||
if (productData) {
|
|
||||||
return {
|
|
||||||
...p,
|
|
||||||
product: {
|
|
||||||
value: productData.id,
|
|
||||||
label: productData.name,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return p;
|
|
||||||
});
|
|
||||||
|
|
||||||
const updatedDeliveries = formik.values.deliveries?.map((delivery) => {
|
|
||||||
const updatedDeliveryProducts = delivery.products.map(
|
|
||||||
(deliveryProduct) => {
|
|
||||||
const productData = productMap.get(deliveryProduct.product_id);
|
|
||||||
if (productData) {
|
|
||||||
return {
|
|
||||||
...deliveryProduct,
|
|
||||||
product: {
|
|
||||||
value: productData.id,
|
|
||||||
label: productData.name,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return deliveryProduct;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
return {
|
|
||||||
...delivery,
|
|
||||||
products: updatedDeliveryProducts,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
if (updatedProducts) {
|
|
||||||
formik.setFieldValue('products', updatedProducts);
|
|
||||||
}
|
|
||||||
if (updatedDeliveries) {
|
|
||||||
formik.setFieldValue('deliveries', updatedDeliveries);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
setFetchedProductIds(newFetchedIds);
|
|
||||||
};
|
|
||||||
|
|
||||||
fetchProducts();
|
|
||||||
}, [
|
|
||||||
formik.values.products,
|
|
||||||
formik.values.deliveries,
|
|
||||||
type,
|
|
||||||
fetchedProductIds,
|
|
||||||
]);
|
|
||||||
|
|
||||||
const getFilteredProductWarehouseOptions = useCallback(() => {
|
const getFilteredProductWarehouseOptions = useCallback(() => {
|
||||||
return (
|
return (
|
||||||
formik.values.products
|
formik.values.products
|
||||||
|
|||||||
Reference in New Issue
Block a user