Here Addd Add All Product Condition Added

This commit is contained in:
Srinath 2026-02-11 15:51:24 +05:30
parent 582b1381bc
commit eff8119813
1 changed files with 33 additions and 13 deletions

View File

@ -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,8 +116,7 @@ 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 (
<Tooltip title="Add All Products" placement="top">
<Messages
messageType={messageType}
messageData={messageData}
onComplete={onComplete}
/>
<button type="button" className="addallProductTBN" onClick={handleAddAll}>
Add All
</button>