Skip to content

Commit

Permalink
Fixed toast multiplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
brelieu05 committed Feb 5, 2025
1 parent 807db79 commit d36f4c5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
6 changes: 0 additions & 6 deletions client/src/components/invoices/InvoiceComponents.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ import personIcon from "../../assets/person.svg"

function InvoiceFilter({invoices, filter, setFilter}) {


useEffect(() => {
console.log(filter);
console.log(invoices);
}, [invoices, filter])

return (
<>
<Popover placement='bottom-start'>
Expand Down
15 changes: 5 additions & 10 deletions client/src/components/invoices/InvoicesDashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ function InvoicesDashboard(){
if (invoices.length === 0 || hasShownToast.current) return;

const getUnpaidInvoices = () => {
const pastDueInvoices = invoices.filter(invoice => isPaid(invoice) === "Past Due");
const notifCounter = pastDueInvoices.length;
const filteredPastDueInvoices = filteredInvoices.filter(invoice => isPaid(invoice) === "Past Due");
const notifCounter = filteredPastDueInvoices.length;

if (notifCounter > 0) {
hasShownToast.current = true; // Set ref to true to prevent multiple toasts
toast({
title: "Unpaid Invoices",
description: notifCounter > 1
? `You have ${notifCounter} past due invoices`
: `${pastDueInvoices[0].name} - ${pastDueInvoices[0].endDate.split("T")[0]}`,
: `${filteredPastDueInvoices[0].name} - ${filteredPastDueInvoices[0].endDate.split("T")[0]}`,
status: "error",
duration: 9000,
position: "bottom-right",
Expand All @@ -80,8 +80,8 @@ function InvoicesDashboard(){
<Heading size="sm">Unpaid Invoices</Heading>
{notifCounter > 1
? `You have ${notifCounter} past due invoices`
: `${pastDueInvoices[0].name} -
${new Date(pastDueInvoices[0].endDate).toLocaleDateString("en-US", {month: "2-digit", day: "2-digit", year: "2-digit",})}`
: `${filteredPastDueInvoices[0].name} -
${new Date(filteredPastDueInvoices[0].endDate).toLocaleDateString("en-US", {month: "2-digit", day: "2-digit", year: "2-digit",})}`
}
</Box>
),
Expand All @@ -90,7 +90,6 @@ function InvoicesDashboard(){
};

getUnpaidInvoices();
console.log(filter);
}, [invoices]);

const filteredInvoices = invoices
Expand All @@ -102,23 +101,19 @@ function InvoicesDashboard(){

// Status filter
if (filter.status !== 'all' && isPaid(invoice).toLowerCase() !== filter.status.toLowerCase()) return false;


// Instructor filter
// Filters for events that have an instructor, and gets the event while ensuring only showing a single events even if they have both instructor and payee
if (filter.instructor.toLowerCase() !== 'all' && invoice.role !== "instructor" && !invoices.some(inv => inv.eventName === invoice.eventName && inv.role === "instructor" && inv.name.toLowerCase() === filter.instructor.toLowerCase()))
return false;


//Payee filter
if (filter.payee.toLowerCase() !== 'all' && invoice.role === "payee" && invoice.name.toLowerCase() !== filter.payee.toLowerCase()) return false;

// Date range filters
if (filter.startDate && new Date(invoice.endDate) < new Date(filter.startDate)) return false;
if (filter.endDate && new Date(invoice.endDate) > new Date(new Date(filter.endDate).setDate(new Date(filter.endDate).getDate() + 1))) return false; //to make date range inclusive



return true;
});

Expand Down

0 comments on commit d36f4c5

Please sign in to comment.