Skip to content

Commit

Permalink
Merge pull request #1782 from thebiggive/BG2-2775-sort-cc-highlight-t…
Browse files Browse the repository at this point in the history
…o-top

Bg2 2775 sort cc highlight to top and link to metacampaign page
  • Loading branch information
bdsl authored Nov 28, 2024
2 parents 3112028 + 149a6f5 commit 306556d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 19 deletions.
4 changes: 3 additions & 1 deletion src/app/campaign.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HttpTestingController, provideHttpClientTesting } from '@angular/common
import { TestBed } from '@angular/core/testing';

import { Campaign } from './campaign.model';
import { CampaignService } from './campaign.service';
import {CampaignService} from './campaign.service';
import { environment } from '../environments/environment';
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';

Expand Down Expand Up @@ -182,3 +182,5 @@ describe('CampaignService', () => {
expect(CampaignService.percentRaisedOfCampaignOrParent(campaign)).toBe(50);
});
});


26 changes: 11 additions & 15 deletions src/app/campaign.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {HttpClient, HttpParams} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';

import { Campaign } from './campaign.model';
import { CampaignStats } from './campaign-stats.model';
import { CampaignSummary } from './campaign-summary.model';
import { environment } from '../environments/environment';
import { SelectedType } from './search.service';
import {HighlightCard, SfApiHighlightCard, SFAPIHighlightCardToHighlightCard} from "./highlight-cards/HighlightCard";
import {Campaign} from './campaign.model';
import {CampaignStats} from './campaign-stats.model';
import {CampaignSummary} from './campaign-summary.model';
import {environment} from '../environments/environment';
import {SelectedType} from './search.service';
import {HighlightCard, SfApiHighlightCard, SFHighlightCardsToFEHighlightCards} from "./highlight-cards/HighlightCard";
import {map} from "rxjs/operators";

@Injectable({
providedIn: 'root',
})
Expand Down Expand Up @@ -206,13 +207,7 @@ export class CampaignService {

getHomePageHighlightCards(): Observable<HighlightCard[]> {
return this.http.get<SfApiHighlightCard[]>(`${environment.apiUriPrefix}${this.apiPath}/highlight-service`).pipe(
map((apiHighlightCards => apiHighlightCards.map(
card => SFAPIHighlightCardToHighlightCard(
environment.experienceUriPrefix,
environment.blogUriPrefix,
environment.donateUriPrefix,
card
))))
map(SFHighlightCardsToFEHighlightCards)
);
}
}
Expand All @@ -234,3 +229,4 @@ export class SearchQuery implements SearchQueryInterface {
public sortField?: string;
public term?: string;
}

55 changes: 54 additions & 1 deletion src/app/highlight-cards/HighlightCard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {campaignFamilyName, SfApiHighlightCard, SFAPIHighlightCardToHighlightCard} from "./HighlightCard";
import {
campaignFamilyName,
SfApiHighlightCard,
SFAPIHighlightCardToHighlightCard,
SFHighlightCardsToFEHighlightCards
} from "./HighlightCard";

describe('highlightCard', () => {
it('should convert a highlight card from the SF API to one we can display', () => {
Expand Down Expand Up @@ -107,6 +112,18 @@ describe('highlightCard', () => {
expect(highlightCardForHomepage.button.href.href).toBe('https://example-blog.com/some-path');
});

it('should replace CC24 link with metacampaign page', () => {
const cardFromApi = cardLinkingTo("https://example-blog.com/christmas-challenge/");

const highlightCardForHomepage = SFAPIHighlightCardToHighlightCard(
'https://example-experience.com',
'https://example-blog.com',
'https://example-donate.com',
cardFromApi
);

expect(highlightCardForHomepage.button.href.href).toBe('https://example-donate.com/christmas-challenge-2024');
});

it('should replace experience origins with origin for relevant environment', () => {
const cardFromApi = cardLinkingTo("https://community.biggive.org/some-path");
Expand Down Expand Up @@ -143,3 +160,39 @@ describe('highlightCard', () => {
expect(highlightCardForHomepage.iconColor).toBe('primary');
});
});

describe('SFHighlightCardsToFEHighlightCards', () => {
it('Sorts Christmas to the top', () => {
const exampleSFCardList: SfApiHighlightCard[] = [
{
campaignFamily: 'emergencyMatch',
cardStyle: 'DONATE_NOW',
headerText: "header",
bodyText: "Emergency card body",
button: {text: "button text", href: 'https://biggive.org/some-path'}
},
{
campaignFamily: 'christmasChallenge',
cardStyle: 'DONATE_NOW',
headerText: "header",
bodyText: "Christmas card body",
button: {text: "button text", href: 'https://biggive.org/some-path'}
},
{
campaignFamily: 'greenMatchFund',
cardStyle: 'DONATE_NOW',
headerText: "header",
bodyText: "Green card body",
button: {text: "button text", href: 'https://biggive.org/some-path'}
},
] as const;

const FECards = SFHighlightCardsToFEHighlightCards(exampleSFCardList);

expect(FECards.map((card) => card.bodyText)).toEqual([
'Christmas card body',
'Emergency card body',
'Green card body',
]);
})
});
31 changes: 29 additions & 2 deletions src/app/highlight-cards/HighlightCard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { brandColour } from "@biggive/components/dist/types/globals/brand-colour"
import {brandColour} from "@biggive/components/dist/types/globals/brand-colour"
import {environment} from "../../environments/environment";

export type HighlightCard = {
backgroundImageUrl: URL,
Expand Down Expand Up @@ -58,14 +59,21 @@ export const SFAPIHighlightCardToHighlightCard = (experienceUriPrefix: string, b

const backgroundImageUrl = backgroundImage(sfApiHighlightCard, donateUriPrefix);

let href = replaceURLOrigin(experienceUriPrefix, blogUriPrefix, donateUriPrefix, sfApiHighlightCard.button.href);

// temp fix for 2024. Will need to think of something better and probably move this logic to SF for next year.
if (href.pathname.includes('christmas-challenge')) {
href = new URL(donateUriPrefix + '/christmas-challenge-2024');
}

return {
headerText: sfApiHighlightCard.headerText,
bodyText: sfApiHighlightCard.bodyText,
iconColor: iconColor(sfApiHighlightCard, campaignFamilyColours),
backgroundImageUrl,
button: {
text: sfApiHighlightCard.button.text,
href: replaceURLOrigin(experienceUriPrefix, blogUriPrefix, donateUriPrefix, sfApiHighlightCard.button.href),
href: href,
}
};
};
Expand Down Expand Up @@ -105,3 +113,22 @@ function backgroundImage(sfApiHighlightCard: SfApiHighlightCard, donateUriPrefix
return campaignFamilyBackgroundImages[sfApiHighlightCard.campaignFamily] || defaultBackground;
}

export function SFHighlightCardsToFEHighlightCards(apiHighlightCards: SfApiHighlightCard[]): HighlightCard[] {
function isChristmasChallenge(card: SfApiHighlightCard) {
return card.campaignFamily === "christmasChallenge";
}

// Array.prototype.sort is specified as being stable since (or ECMAScript 2019). I don't think we support any browsers
// too old to have that, and we can cope with the possibility of none CC cards being in the wrong order in very old
const cardsSortedCCFirst = apiHighlightCards.sort((a, b) => {
return +isChristmasChallenge(b) - +isChristmasChallenge(a);
});

return cardsSortedCCFirst.map(
card => SFAPIHighlightCardToHighlightCard(
environment.experienceUriPrefix,
environment.blogUriPrefix,
environment.donateUriPrefix,
card
))
}

0 comments on commit 306556d

Please sign in to comment.