Skip to content

Commit

Permalink
fixed summary
Browse files Browse the repository at this point in the history
  • Loading branch information
Su Yeon Lee authored and Su Yeon Lee committed Jun 13, 2024
1 parent 6fdfa30 commit 410eb12
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions client/src/Popup/PopupPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ interface PopupPageProps {

function PopupPage({ open, onClose, donorID }: PopupPageProps) {
const donations = useData(`donation/donor/${donorID}`);
// const donor = useData(`donor/${donorID}`);
const [donorData, setDonorData] = useState<IDonor | null>(null);
const [donationsData, setDonationsData] = useState<any>([]);
const [donationsStats, setDonationsStats] = useState<DonationStats>();
Expand All @@ -48,7 +47,6 @@ function PopupPage({ open, onClose, donorID }: PopupPageProps) {
const purposes = useData('purpose');
const [purposesData, setPurposesData] = useState<PurposeType[]>([]);


useEffect(() => {
const data = purposes?.data || [];
setPurposesData(data);
Expand Down Expand Up @@ -77,69 +75,58 @@ function PopupPage({ open, onClose, donorID }: PopupPageProps) {
useEffect(() => {
const calculateDonationStats = (data: any) => {
if (data.length) {
// Calculate Total Donation Amount
const totalDonationAmount = data.reduce(
(total: number, donation: any) => total + donation.amount,
0,
);

// Get the current date
let fiscalYearStart: Date;
let fiscalYearEnd: Date;

const currentDate = new Date();

if (currentDate.getMonth() < 6) {
// If the current date is before July 1, the fiscal year starts on July 1 of the previous year
fiscalYearStart = new Date(currentDate.getFullYear() - 1, 6, 1);
fiscalYearEnd = new Date(currentDate.getFullYear(), 5, 30);
} else {
// If the current date is on or after July 1, the fiscal year starts on July 1 of the current year
fiscalYearStart = new Date(currentDate.getFullYear(), 6, 1);
fiscalYearEnd = new Date(currentDate.getFullYear() + 1, 5, 30);
}

// Filter out the donations that were made during the current fiscal year
const fiscalYearDonations = data.filter((donation: any) => {
const donationDate = new Date(donation.date);
return (
donationDate >= fiscalYearStart && donationDate <= fiscalYearEnd
);
});

// Calculate the total amount of donations made during the current fiscal year
const totalFiscalYearDonationAmount = fiscalYearDonations.reduce(
(total: number, donation: any) => total + donation.amount,
0,
);

// Calculate the average donation per fiscal year
const avDonationPerFiscal: BasicDonationStat = {
amount: fiscalYearDonations.length
? totalFiscalYearDonationAmount / fiscalYearDonations.length
? (totalFiscalYearDonationAmount / fiscalYearDonations.length).toFixed(2)
: 0,
count: fiscalYearDonations.length,
};

// Define the start and end dates of the current calendar year
const calendarYearStart = new Date(currentDate.getFullYear(), 0, 1);
const calendarYearEnd = new Date(currentDate.getFullYear(), 11, 31);

// Filter out the donations that were made during the current calendar year
const calendarYearDonations = donationsData.filter((donation: any) => {
const donationDate = new Date(donation.date);
return (
donationDate >= calendarYearStart && donationDate <= calendarYearEnd
);
});

// Calculate the total amount of donations made during the current calendar year
const totalCalendarYearDonationAmount = calendarYearDonations.reduce(
(total: number, donation: any) => total + donation.amount,
0,
);

// Calculate the average donation per calendar year
const avDonationPerCalendar: BasicDonationStat = {
amount: calendarYearDonations.length
? +(
Expand All @@ -149,7 +136,6 @@ function PopupPage({ open, onClose, donorID }: PopupPageProps) {
count: calendarYearDonations.length,
};

// Calculate Donation (in past 30 days)
const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
const donationsLast30Days = donationsData.filter(
Expand All @@ -166,7 +152,6 @@ function PopupPage({ open, onClose, donorID }: PopupPageProps) {
count: donationCountLast30Days,
};

// Find the most recent donation
const mostRecentDonation = donationsData.reduce(
(mostRecent: any, donation: any) => {
const donationDate = new Date(donation.date);
Expand Down Expand Up @@ -206,6 +191,15 @@ function PopupPage({ open, onClose, donorID }: PopupPageProps) {

if (donationsData.length > 0) {
calculateDonationStats(donationsData);
} else {
setDonationsStats({
totalDonationAmount: { amount: 0, count: 0 },
avDonationPerFiscal: { amount: 0, count: 0 },
avDonationPerCalendar: { amount: 0, count: 0 },
donationThirtyDays: { amount: 0, count: 0 },
recentDonation: { amount: 0, date: 'no history' },
});
setPurpose('no history');
}
}, [donationsData, purposesData]);

Expand All @@ -220,31 +214,41 @@ function PopupPage({ open, onClose, donorID }: PopupPageProps) {
<TableRow>
<TableCell>Total Donation Amount</TableCell>
<TableCell>
${donationsStats?.totalDonationAmount.amount}
{donationsStats?.totalDonationAmount.count > 0
? `$${donationsStats.totalDonationAmount.amount}`
: 'no history'}
</TableCell>
</TableRow>
<TableRow>
<TableCell>Average Donation (Fiscal)</TableCell>
<TableCell>
${donationsStats?.avDonationPerFiscal.amount}
{donationsStats?.avDonationPerFiscal.count > 0
? `$${donationsStats.avDonationPerFiscal.amount}`
: 'no history'}
</TableCell>
</TableRow>
<TableRow>
<TableCell>Average Donation (Calendar)</TableCell>
<TableCell>
${donationsStats?.avDonationPerCalendar.amount}
{donationsStats?.avDonationPerCalendar.count > 0
? `$${donationsStats.avDonationPerCalendar.amount}`
: 'no history'}
</TableCell>
</TableRow>
<TableRow>
<TableCell>Average Donation (Past 30 Days)</TableCell>
<TableCell>
${donationsStats?.donationThirtyDays.amount}
{donationsStats?.donationThirtyDays.count > 0
? `$${donationsStats.donationThirtyDays.amount}`
: 'no history'}
</TableCell>
</TableRow>
<TableRow>
<TableCell>Recent Donation</TableCell>
<TableCell>
${donationsStats?.recentDonation.amount}
{donationsStats?.recentDonation.amount > 0
? `$${donationsStats.recentDonation.amount}`
: 'no history'}
</TableCell>
</TableRow>
<TableRow>
Expand Down

0 comments on commit 410eb12

Please sign in to comment.