diff --git a/src/Pages/StockMaster/AddAllProductGrid.jsx b/src/Pages/StockMaster/AddAllProductGrid.jsx index a8f8da1..75d7d09 100644 --- a/src/Pages/StockMaster/AddAllProductGrid.jsx +++ b/src/Pages/StockMaster/AddAllProductGrid.jsx @@ -1,5 +1,7 @@ -import { Tooltip } from 'antd'; +import { Tooltip, message } from 'antd'; +import { useState, useEffect, useCallback } from 'react'; import { v4 as uuidv4 } from 'uuid'; +import { Messages } from '../../Components/Notifications/Messages'; const AddAllProductsButton = ({ productData = [], @@ -13,6 +15,9 @@ const AddAllProductsButton = ({ }) => { // This function just returns a prepared product object, no state changes + const [messageType, setMessageType] = useState(null); + const [messageData, setMessageData] = useState(null); + const ProductDropDownChange = async ( VariantName, ProdId, @@ -74,24 +79,34 @@ const AddAllProductsButton = ({ localId: uuidv4(), }; }; - const handleAddAll = async () => { + const existingProdIds = new Set( + purchaseData?.map((p) => `${p.ProdId}_${p.ProdVariantName}`) + ); + const productsToAdd = productData?.filter( + (p) => !existingProdIds?.has(`${p.ProdId}_${p.ProdVariantName}`) + ); + + if (productsToAdd.length === 0) { + setMessageData('All products are already added'); + setMessageType('warning'); + return; + } + try { setIsLoading(true); setLoadingText('Adding products...'); - // allow UI paint await new Promise((r) => requestAnimationFrame(r)); const allValidProducts = []; const allFormValues = {}; - for (let i = 0; i < productData.length; i += batchSize) { - const batch = productData.slice(i, i + batchSize); + for (let i = 0; i < productsToAdd.length; i += batchSize) { + const batch = productsToAdd.slice(i, i + batchSize); setLoadingText( - `Processing ${Math.min(i + batchSize, productData.length)} / ${ - productData.length + `Processing ${Math.min(i + batchSize, productsToAdd.length)} / ${productsToAdd.length }` ); @@ -101,9 +116,8 @@ const AddAllProductsButton = ({ product.ProdVariantName, product.ProdId, { - label: `${product.ProdName} (${product.Size} ${product.UomName})${ - product.BrandName ? ` - ${product.BrandName}` : '' - }`, + label: `${product.ProdName} (${product.Size} ${product.UomName})${product.BrandName ? ` - ${product.BrandName}` : '' + }`, }, productData ) @@ -128,19 +142,25 @@ const AddAllProductsButton = ({ await new Promise((r) => setTimeout(r, 0)); } - // ✅ ONE state update setPurchaseData((prev) => [...allValidProducts, ...prev]); - - // ✅ ONE form update form?.setFieldsValue(allFormValues); } finally { setIsLoading(false); setLoadingText(''); } }; + const onComplete = useCallback(() => { + setMessageData(null); + setMessageType(null); + }, []); return ( +