diff --git a/frontend/src/page/ReceivedInvoice.jsx b/frontend/src/page/ReceivedInvoice.jsx index d4ca17fa..95f956d7 100644 --- a/frontend/src/page/ReceivedInvoice.jsx +++ b/frontend/src/page/ReceivedInvoice.jsx @@ -349,6 +349,22 @@ function ReceivedInvoice() { } }; + + + const getGroupedInvoices = (ids = selectedInvoices) => { + const grouped = new Map(); + receivedInvoices.forEach((invoice) => { + if (!ids.has(invoice.id)) return; + + const getGroupedInvoices = (selection = selectedInvoices) => { + const grouped = new Map(); + receivedInvoices.forEach((invoice) => { + if (!selection.has(invoice.id)) return; + + + const tokenAddress = invoice.paymentToken?.address || ethers.ZeroAddress; + const tokenKey = `${tokenAddress}_${invoice.paymentToken?.symbol || "ETH"}`; + const getGroupedInvoices = (invoices) => { const grouped = new Map(); invoices.forEach((invoice) => { @@ -356,6 +372,7 @@ function ReceivedInvoice() { invoice.paymentToken; const tokenKey = tokenAddress.toLowerCase(); + if (!grouped.has(tokenKey)) { grouped.set(tokenKey, { tokenAddress, @@ -445,14 +462,13 @@ function ReceivedInvoice() { return; } - setSelectedInvoices(new Set(batchInvoices.map((inv) => inv.id))); + const batchIds = new Set(batchInvoices.map((inv) => inv.id)); + setSelectedInvoices(batchIds); toast( `Selected ${batchInvoices.length} invoices from batch #${batchId}` ); - setTimeout(() => { - handleBatchPayment(); - }, 1000); + await handleBatchPayment(batchIds); }; // UNIFORM INDIVIDUAL PAYMENT @@ -579,6 +595,10 @@ function ReceivedInvoice() { }; // UNIFORM BATCH PAYMENT + + const handleBatchPayment = async (ids = selectedInvoices) => { + if (!walletClient || ids.size === 0) return; + const handleBatchPayment = async () => { if (!walletClient || payableSelected.length === 0) return; if (loading) { @@ -598,10 +618,13 @@ function ReceivedInvoice() { return; } + + // Paid, cancelled and mismatched invoices can stay selected for export; // the contract would revert the whole batch for any one of them. const grouped = getGroupedInvoices(payableSelected); + setBatchLoading(true); setPaymentError(""); setShowPaymentError(false); @@ -619,6 +642,11 @@ function ReceivedInvoice() { const contract = new Contract(contractAddress, ChainvoiceABI, signer); + + const grouped = getGroupedInvoices(ids); + + + // BALANCE CHECK (same as individual) toast("Checking balances..."); @@ -1426,7 +1454,7 @@ function ReceivedInvoice() { ))}