Here Addd Add All Product Condition Added
This commit is contained in:
parent
582b1381bc
commit
eff8119813
|
|
@ -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 { v4 as uuidv4 } from 'uuid';
|
||||||
|
import { Messages } from '../../Components/Notifications/Messages';
|
||||||
|
|
||||||
const AddAllProductsButton = ({
|
const AddAllProductsButton = ({
|
||||||
productData = [],
|
productData = [],
|
||||||
|
|
@ -13,6 +15,9 @@ const AddAllProductsButton = ({
|
||||||
}) => {
|
}) => {
|
||||||
// This function just returns a prepared product object, no state changes
|
// This function just returns a prepared product object, no state changes
|
||||||
|
|
||||||
|
const [messageType, setMessageType] = useState(null);
|
||||||
|
const [messageData, setMessageData] = useState(null);
|
||||||
|
|
||||||
const ProductDropDownChange = async (
|
const ProductDropDownChange = async (
|
||||||
VariantName,
|
VariantName,
|
||||||
ProdId,
|
ProdId,
|
||||||
|
|
@ -74,24 +79,34 @@ const AddAllProductsButton = ({
|
||||||
localId: uuidv4(),
|
localId: uuidv4(),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleAddAll = async () => {
|
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 {
|
try {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
setLoadingText('Adding products...');
|
setLoadingText('Adding products...');
|
||||||
|
|
||||||
// allow UI paint
|
|
||||||
await new Promise((r) => requestAnimationFrame(r));
|
await new Promise((r) => requestAnimationFrame(r));
|
||||||
|
|
||||||
const allValidProducts = [];
|
const allValidProducts = [];
|
||||||
const allFormValues = {};
|
const allFormValues = {};
|
||||||
|
|
||||||
for (let i = 0; i < productData.length; i += batchSize) {
|
for (let i = 0; i < productsToAdd.length; i += batchSize) {
|
||||||
const batch = productData.slice(i, i + batchSize);
|
const batch = productsToAdd.slice(i, i + batchSize);
|
||||||
|
|
||||||
setLoadingText(
|
setLoadingText(
|
||||||
`Processing ${Math.min(i + batchSize, productData.length)} / ${
|
`Processing ${Math.min(i + batchSize, productsToAdd.length)} / ${productsToAdd.length
|
||||||
productData.length
|
|
||||||
}`
|
}`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -101,9 +116,8 @@ const AddAllProductsButton = ({
|
||||||
product.ProdVariantName,
|
product.ProdVariantName,
|
||||||
product.ProdId,
|
product.ProdId,
|
||||||
{
|
{
|
||||||
label: `${product.ProdName} (${product.Size} ${product.UomName})${
|
label: `${product.ProdName} (${product.Size} ${product.UomName})${product.BrandName ? ` - ${product.BrandName}` : ''
|
||||||
product.BrandName ? ` - ${product.BrandName}` : ''
|
}`,
|
||||||
}`,
|
|
||||||
},
|
},
|
||||||
productData
|
productData
|
||||||
)
|
)
|
||||||
|
|
@ -128,19 +142,25 @@ const AddAllProductsButton = ({
|
||||||
await new Promise((r) => setTimeout(r, 0));
|
await new Promise((r) => setTimeout(r, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ ONE state update
|
|
||||||
setPurchaseData((prev) => [...allValidProducts, ...prev]);
|
setPurchaseData((prev) => [...allValidProducts, ...prev]);
|
||||||
|
|
||||||
// ✅ ONE form update
|
|
||||||
form?.setFieldsValue(allFormValues);
|
form?.setFieldsValue(allFormValues);
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
setLoadingText('');
|
setLoadingText('');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
const onComplete = useCallback(() => {
|
||||||
|
setMessageData(null);
|
||||||
|
setMessageType(null);
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Tooltip title="Add All Products" placement="top">
|
<Tooltip title="Add All Products" placement="top">
|
||||||
|
<Messages
|
||||||
|
messageType={messageType}
|
||||||
|
messageData={messageData}
|
||||||
|
onComplete={onComplete}
|
||||||
|
/>
|
||||||
<button type="button" className="addallProductTBN" onClick={handleAddAll}>
|
<button type="button" className="addallProductTBN" onClick={handleAddAll}>
|
||||||
Add All
|
Add All
|
||||||
</button>
|
</button>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue