Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added recurring donation sum in project card of QF active round #4538

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/apollo/gql/gqlProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,3 +754,32 @@ export const FETCH_RECURRING_DONATIONS_BY_PROJECTID = gql`
}
}
`;
export const FETCH_RECURRING_DONATIONS_BY_DATE = gql`
query ($projectId: Int!, $startDate: String, $endDate: String) {
recurringDonationsByDate(
projectId: $projectId
startDate: $startDate
endDate: $endDate
) {
recurringDonations {
id
txHash
networkId
flowRate
currency
anonymous
isArchived
status
totalUsdStreamed
donor {
id
walletAddress
firstName
email
}
createdAt
}
totalCount
}
}
`;
64 changes: 60 additions & 4 deletions src/components/project-card/ProjectCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import styled from 'styled-components';
import {
P,
Expand All @@ -21,7 +21,7 @@ import { useRouter } from 'next/router';
import { Shadow } from '@/components/styled-components/Shadow';
import ProjectCardBadges from './ProjectCardBadgeButtons';
import ProjectCardOrgBadge from './ProjectCardOrgBadge';
import { IProject } from '@/apollo/types/types';
import { IAdminUser, IProject } from '@/apollo/types/types';
import { timeFromNow } from '@/lib/helpers';
import ProjectCardImage from './ProjectCardImage';
import { slugToProjectDonate, slugToProjectView } from '@/lib/routeCreators';
Expand All @@ -33,6 +33,8 @@ import { formatDonation } from '@/helpers/number';
import { RoundNotStartedModal } from './RoundNotStartedModal';
import { TooltipContent } from '@/components/modals/HarvestAll.sc';
import { IconWithTooltip } from '@/components/IconWithToolTip';
import { FETCH_RECURRING_DONATIONS_BY_DATE } from '@/apollo/gql/gqlProjects';
import { client } from '@/apollo/apolloClient';

const cardRadius = '12px';
const imgHeight = '226px';
Expand All @@ -43,10 +45,24 @@ interface IProjectCard {
className?: string;
order?: number;
}

interface IRecurringDonation {
id: string;
txHash: string;
networkId: number;
currency: string;
anonymous: boolean;
status: string;
amountStreamed: number;
totalUsdStreamed: number;
flowRate: string;
donor: IAdminUser;
createdAt: string;
finished: boolean;
}
const ProjectCard = (props: IProjectCard) => {
const { project, className } = props;
const {
id,
title,
descriptionSummary,
image,
Expand All @@ -61,6 +77,7 @@ const ProjectCard = (props: IProjectCard) => {
qfRounds,
estimatedMatching,
} = project;
const [recurringDonationSumInQF, setRecurringDonationSumInQF] = useState(0);
const [isHover, setIsHover] = useState(false);
const [showHintModal, setShowHintModal] = useState(false);
const [destination, setDestination] = useState('');
Expand Down Expand Up @@ -96,6 +113,44 @@ const ProjectCard = (props: IProjectCard) => {
}
};

const fetchProjectRecurringDonationsByDate = async () => {
const startDate = activeStartedRound?.beginDate;
const endDate = activeStartedRound?.endDate;
if (startDate && endDate) {
const { data: projectRecurringDonations } = await client.query({
query: FETCH_RECURRING_DONATIONS_BY_DATE,
variables: {
projectId: parseInt(id),
startDate,
endDate,
},
});
const { recurringDonationsByDate } = projectRecurringDonations;
return recurringDonationsByDate;
}
};
useEffect(() => {
const calculateTotalAmountStreamed = async () => {
if (activeStartedRound?.isActive) {
const donations = await fetchProjectRecurringDonationsByDate();
let totalAmountStreamed;
if (donations.totalCount != 0) {
console.log(id, donations.recurringDonations);
totalAmountStreamed = donations.recurringDonations.reduce(
(sum: number, donation: IRecurringDonation) => {
return sum + donation.totalUsdStreamed;
},
0,
);
setRecurringDonationSumInQF(totalAmountStreamed);
}
console.log(id, totalAmountStreamed);
}
};

calculateTotalAmountStreamed();
}, [props]);

Comment on lines +132 to +152
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimize useEffect dependencies.

The useEffect hook and the calculateTotalAmountStreamed function are well-implemented. However, consider optimizing the dependencies to avoid unnecessary re-renders.

- }, [props]);
+ }, [project, activeStartedRound]);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
useEffect(() => {
const calculateTotalAmountStreamed = async () => {
if (activeStartedRound?.isActive) {
const donations = await fetchProjectRecurringDonationsByDate();
let totalAmountStreamed;
if (donations.totalCount != 0) {
console.log(id, donations.recurringDonations);
totalAmountStreamed = donations.recurringDonations.reduce(
(sum: number, donation: IRecurringDonation) => {
return sum + donation.totalUsdStreamed;
},
0,
);
setRecurringDonationSumInQF(totalAmountStreamed);
}
console.log(id, totalAmountStreamed);
}
};
calculateTotalAmountStreamed();
}, [props]);
useEffect(() => {
const calculateTotalAmountStreamed = async () => {
if (activeStartedRound?.isActive) {
const donations = await fetchProjectRecurringDonationsByDate();
let totalAmountStreamed;
if (donations.totalCount != 0) {
console.log(id, donations.recurringDonations);
totalAmountStreamed = donations.recurringDonations.reduce(
(sum: number, donation: IRecurringDonation) => {
return sum + donation.totalUsdStreamed;
},
0,
);
setRecurringDonationSumInQF(totalAmountStreamed);
}
console.log(id, totalAmountStreamed);
}
};
calculateTotalAmountStreamed();
}, [project, activeStartedRound]);

return (
// </Link>
<Wrapper
Expand Down Expand Up @@ -170,7 +225,8 @@ const ProjectCard = (props: IProjectCard) => {
<PriceText>
{formatDonation(
(activeStartedRound
? sumDonationValueUsdForActiveQfRound
? sumDonationValueUsdForActiveQfRound! +
recurringDonationSumInQF
: totalDonations) || 0,
'$',
locale,
Expand Down
Loading