Skip to content

Commit

Permalink
Merge pull request #1334 from thebiggive/DON-906-shared-fund-raised-p…
Browse files Browse the repository at this point in the history
…ercent

DON-906 – fix some Campaign Info progressbars
  • Loading branch information
NoelLH authored Oct 6, 2023
2 parents 20515d6 + 6130621 commit 2d07e50
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/app/campaign-info/campaign-info.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class CampaignInfoComponent implements OnInit {
}

getPercentageRaised(campaign: Campaign): number | undefined {
return CampaignService.percentRaised(campaign);
return CampaignService.percentRaised(campaign, true);
}

getBeneficiaryIcon(beneficiary: string) {
Expand Down
20 changes: 20 additions & 0 deletions src/app/campaign.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,24 @@ describe('CampaignService', () => {

expect(CampaignService.isOpenForDonations(campaign)).toBe(false);
});

it ('should return the % raised for itself when its parent does not use shared funds', () => {
const campaign = getDummyCampaign();
campaign.parentUsesSharedFunds = false;
campaign.amountRaised = 98;
campaign.target = 200;

expect(CampaignService.percentRaised(campaign, true)).toBe(49);
});

it ('should return the % raised for the parent campaign when its parent does use shared funds', () => {
const campaign = getDummyCampaign();
campaign.parentUsesSharedFunds = true;
campaign.amountRaised = 98;
campaign.target = 200;
campaign.parentAmountRaised = 1000;
campaign.parentTarget = 2000;

expect(CampaignService.percentRaised(campaign, true)).toBe(50);
});
});
17 changes: 16 additions & 1 deletion src/app/campaign.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,22 @@ export class CampaignService {
return dateToUse;
}

static percentRaised(campaign: (Campaign | CampaignSummary)): number | undefined {
/**
* @param useParentIfApplicable Whether to get a percentage for the parent/meta-campaign if possible. This
* is possible when `campaign` is a detailed Campaign, e.g. on /campaign/..., and
* it takes effect when the `parentUsesSharedFunds`.
*/
static percentRaised(
campaign: (Campaign | CampaignSummary),
useParentIfApplicable = false,
): number | undefined {
if (useParentIfApplicable) {
campaign = campaign as Campaign;
if (campaign.parentUsesSharedFunds && campaign.parentTarget && campaign.parentAmountRaised) {
return Math.round((campaign.parentAmountRaised / campaign.parentTarget) * 100);
}
}

if (!campaign.target) {
return undefined;
}
Expand Down

0 comments on commit 2d07e50

Please sign in to comment.