Skip to content

Commit

Permalink
add donations info to project card
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlanglen committed Nov 3, 2023
1 parent ee68786 commit 26f3d38
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 16 deletions.
1 change: 1 addition & 0 deletions apps/potlock/widget/Index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ State.init({

if (state.nearToUsd === null) {
const res = fetch("https://api.coingecko.com/api/v3/simple/price?ids=near&vs_currencies=usd");
console.log("coingecko res: ", res.body);
State.update({ nearToUsd: res.body.near.usd });
}

Expand Down
59 changes: 53 additions & 6 deletions apps/potlock/widget/Project/Card.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ownerId = "potlock.near";
const donationContractId = "donation.tests.potlock.near"; // TODO: update to donate.potlock.near after testing

const Card = styled.a`
display: flex;
Expand Down Expand Up @@ -45,16 +46,16 @@ const Info = styled.div`
margin-top: 160px;
padding: 16px 24px;
gap: 16px;
flex: 1;
`;

const ProjectName = styled.h2`
const Title = styled.div`
font-size: 16px;
font-weight: 600;
font-family: mona-sans;
color: #2e2e2e;
`;

const ProjectDescription = styled.p`
const SubTitle = styled.div`
font-size: 16px;
font-weight: 400;
color: #2e2e2e;
Expand All @@ -74,10 +75,46 @@ const Tag = styled.span`
color: #2e2e2e;
`;

const DonationsInfoContainer = styled.div`
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
padding: 16px 24px;
width: 100%;
border-top: 1px #f0f0f0 solid;
`;

const DonationsInfoItem = styled.div`
display: flex;
flex-direction: row;
gap: 8px;
align-items: center;
`;

const MAX_DESCRIPTION_LENGTH = 120;

const { id, bannerImageUrl, profileImageUrl, name, description, tags } = props.project;

const donationsForProject = Near.view(donationContractId, "get_donations_for_recipient", {
recipient_id: id,
});

const [totalAmount, totalDonors] = useMemo(() => {
if (!donationsForProject) {
return ["-", "-"];
}
const donors = [];
let totalDonationAmount = new Big(0);
for (const donation of donationsForProject) {
if (!donors.includes(donation.donor_id)) {
donors.push(donation.donor_id);
}
totalDonationAmount = totalDonationAmount.plus(new Big(donation.total_amount));
}
return [(props.nearToUsd * totalDonationAmount.div(1e24).toNumber()).toFixed(2), donors.length];
}, [donationsForProject]);

return (
<Card href={`?tab=project&projectId=${id}`} key={id}>
<Widget
Expand Down Expand Up @@ -107,12 +144,12 @@ return (
}}
/>
<Info>
<ProjectName>{name}</ProjectName>
<ProjectDescription>
<Title>{name}</Title>
<SubTitle>
{description.length > MAX_DESCRIPTION_LENGTH
? description.slice(0, MAX_DESCRIPTION_LENGTH) + "..."
: description}
</ProjectDescription>
</SubTitle>
<Widget
src={`${ownerId}/widget/Project.Tags`}
props={{
Expand All @@ -121,5 +158,15 @@ return (
}}
/>
</Info>
<DonationsInfoContainer>
<DonationsInfoItem>
<Title>{totalDonors}</Title>
<SubTitle>{totalDonors === 1 ? "Donor" : "Donors"}</SubTitle>
</DonationsInfoItem>
<DonationsInfoItem>
<Title>${totalAmount}</Title>
<SubTitle>Raised</SubTitle>
</DonationsInfoItem>
</DonationsInfoContainer>
</Card>
);
5 changes: 3 additions & 2 deletions apps/potlock/widget/Project/DonationsInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const InfoCard = styled.div`
background: #fef6ee;
box-shadow: 0px -2px 0px rgba(219, 82, 27, 0.36) inset;
gap: 8px;
min-width: 260px;
`;

const InfoTextPrimary = styled.div`
Expand Down Expand Up @@ -57,13 +58,13 @@ const [totalDonations, totalDonors] = useMemo(() => {
}
totalDonationAmount = totalDonationAmount.plus(new Big(donation.total_amount));
}
return [totalDonationAmount.div(1e24).toString(), donors.length];
return [totalDonationAmount.div(1e24).toNumber().toFixed(2), donors.length];
}, [donationsForProject]);

return (
<Container>
<InfoCard>
<InfoTextPrimary>{totalDonations} NEAR</InfoTextPrimary>
<InfoTextPrimary>${totalDonations}</InfoTextPrimary>
<InfoTextSecondary>Contributed</InfoTextSecondary>
</InfoCard>
<InfoCard>
Expand Down
1 change: 1 addition & 0 deletions apps/potlock/widget/Project/ListPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ return (
src={`${ownerId}/widget/Project.Card`}
props={{
project,
...props,
}}
/>
),
Expand Down
1 change: 1 addition & 0 deletions build/potlock/src/Index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ State.init({

if (state.nearToUsd === null) {
const res = fetch("https://api.coingecko.com/api/v3/simple/price?ids=near&vs_currencies=usd");
console.log("coingecko res: ", res.body);
State.update({ nearToUsd: res.body.near.usd });
}

Expand Down
59 changes: 53 additions & 6 deletions build/potlock/src/Project/Card.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ownerId = "potlock.near";
const donationContractId = "donation.tests.potlock.near"; // TODO: update to donate.potlock.near after testing

const Card = styled.a`
display: flex;
Expand Down Expand Up @@ -45,16 +46,16 @@ const Info = styled.div`
margin-top: 160px;
padding: 16px 24px;
gap: 16px;
flex: 1;
`;

const ProjectName = styled.h2`
const Title = styled.div`
font-size: 16px;
font-weight: 600;
font-family: mona-sans;
color: #2e2e2e;
`;

const ProjectDescription = styled.p`
const SubTitle = styled.div`
font-size: 16px;
font-weight: 400;
color: #2e2e2e;
Expand All @@ -74,10 +75,46 @@ const Tag = styled.span`
color: #2e2e2e;
`;

const DonationsInfoContainer = styled.div`
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
padding: 16px 24px;
width: 100%;
border-top: 1px #f0f0f0 solid;
`;

const DonationsInfoItem = styled.div`
display: flex;
flex-direction: row;
gap: 8px;
align-items: center;
`;

const MAX_DESCRIPTION_LENGTH = 120;

const { id, bannerImageUrl, profileImageUrl, name, description, tags } = props.project;

const donationsForProject = Near.view(donationContractId, "get_donations_for_recipient", {
recipient_id: id,
});

const [totalAmount, totalDonors] = useMemo(() => {
if (!donationsForProject) {
return ["-", "-"];
}
const donors = [];
let totalDonationAmount = new Big(0);
for (const donation of donationsForProject) {
if (!donors.includes(donation.donor_id)) {
donors.push(donation.donor_id);
}
totalDonationAmount = totalDonationAmount.plus(new Big(donation.total_amount));
}
return [(props.nearToUsd * totalDonationAmount.div(1e24).toNumber()).toFixed(2), donors.length];
}, [donationsForProject]);

return (
<Card href={`?tab=project&projectId=${id}`} key={id}>
<Widget
Expand Down Expand Up @@ -107,12 +144,12 @@ return (
}}
/>
<Info>
<ProjectName>{name}</ProjectName>
<ProjectDescription>
<Title>{name}</Title>
<SubTitle>
{description.length > MAX_DESCRIPTION_LENGTH
? description.slice(0, MAX_DESCRIPTION_LENGTH) + "..."
: description}
</ProjectDescription>
</SubTitle>
<Widget
src={`${ownerId}/widget/Project.Tags`}
props={{
Expand All @@ -121,5 +158,15 @@ return (
}}
/>
</Info>
<DonationsInfoContainer>
<DonationsInfoItem>
<Title>{totalDonors}</Title>
<SubTitle>{totalDonors === 1 ? "Donor" : "Donors"}</SubTitle>
</DonationsInfoItem>
<DonationsInfoItem>
<Title>${totalAmount}</Title>
<SubTitle>Raised</SubTitle>
</DonationsInfoItem>
</DonationsInfoContainer>
</Card>
);
5 changes: 3 additions & 2 deletions build/potlock/src/Project/DonationsInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const InfoCard = styled.div`
background: #fef6ee;
box-shadow: 0px -2px 0px rgba(219, 82, 27, 0.36) inset;
gap: 8px;
min-width: 260px;
`;

const InfoTextPrimary = styled.div`
Expand Down Expand Up @@ -57,13 +58,13 @@ const [totalDonations, totalDonors] = useMemo(() => {
}
totalDonationAmount = totalDonationAmount.plus(new Big(donation.total_amount));
}
return [totalDonationAmount.div(1e24).toString(), donors.length];
return [totalDonationAmount.div(1e24).toNumber().toFixed(2), donors.length];
}, [donationsForProject]);

return (
<Container>
<InfoCard>
<InfoTextPrimary>{totalDonations} NEAR</InfoTextPrimary>
<InfoTextPrimary>${totalDonations}</InfoTextPrimary>
<InfoTextSecondary>Contributed</InfoTextSecondary>
</InfoCard>
<InfoCard>
Expand Down
1 change: 1 addition & 0 deletions build/potlock/src/Project/ListPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ return (
src={`${ownerId}/widget/Project.Card`}
props={{
project,
...props,
}}
/>
),
Expand Down

0 comments on commit 26f3d38

Please sign in to comment.