Skip to content

Commit

Permalink
Merge pull request #22 from johnsyweb/paj/loading-message
Browse files Browse the repository at this point in the history
Loading message
  • Loading branch information
johnsyweb authored Oct 20, 2024
2 parents e3ccb64 + 89d2b97 commit c835f01
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
10 changes: 10 additions & 0 deletions src/dom/upsertParagraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ export function upsertParagraph(
div.appendChild(paragraph);
return paragraph;
}

export function deleteParagraph(div: HTMLElement, id: string) {
const existingParagraph = Array.from(div.children).find(
(element) => element.id === id,
);

if (existingParagraph) {
existingParagraph.remove();
}
}
32 changes: 22 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,47 @@ import { fiveKFinishersToMilestones } from "./transformers/fiveKFinishersToMiles
import { fiveKVolunteersToMilestones } from "./transformers/fiveKVolunteersToMilestones";
import { MilestonePresenter } from "./presenters/MilestonePresenter";
import { ResultsPageExtractor } from "./extractors/ResultsPageExtractor";
import { upsertParagraph } from "./dom/upsertParagraph";
import { deleteParagraph, upsertParagraph } from "./dom/upsertParagraph";
import { VolunteerWithCount } from "./types/Volunteer";

function populate(
rpe: ResultsPageExtractor,
volunteerWithCountList: VolunteerWithCount[],
message?: string
): void {
const introduction = `On parkrunday, ${rpe.finishers.length} parkrunners joined us for event ${rpe.eventNumber} and completed the ${rpe.courseLength}km ${rpe.eventName} course`;

const newestParkrunnersTitle = `Congratulations to our ${pluralize(
"newest parkrunner",
"newest parkrunners",
rpe.newestParkrunners.length,
rpe.newestParkrunners.length
)}: `;

const firstTimersTitle = `Welcome to the ${pluralize(
"parkrunner",
"parkrunners",
rpe.firstTimers.length,
rpe.firstTimers.length
)} who joined us at ${rpe.eventName ?? "parkrun"} for the first time: `;

const finishersWithNewPBsTitle = `Very well done to the ${pluralize(
"parkrunner",
"parkrunners",
rpe.finishersWithNewPBs.length,
rpe.finishersWithNewPBs.length
)} who improved their personal best this week: `;

const runningWalkingGroupsTitle = `We were pleased to see ${pluralize(
"active group",
"walking and running groups",
rpe.runningWalkingGroups.length,
rpe.runningWalkingGroups.length
)} represented at this event: `;

const volunteersTitle = `${rpe.eventName} are very grateful to the ${volunteerWithCountList.length} amazing volunteers who made this event happen: `;
const volunteerOccasions = volunteerWithCountList
.map((v) => v.vols)
.reduce((c, p) => c + p, 0);

const volunteersTitle = `The following ${volunteerWithCountList.length.toLocaleString()} superstars have volunteered a total of ${volunteerOccasions.toLocaleString()} times between them, and helped us host ${
rpe.eventName
} this weekend. Our deep thanks to: `;

const milestoneCelebrations = [
...fiveKVolunteersToMilestones(volunteerWithCountList),
Expand All @@ -61,6 +68,7 @@ function populate(
eventuateDiv.id = "eventuate";

const reportDetails = {
message: { title: "⏳︎", details: message },
introduction: { title: "", details: introduction },

milestoneCelebrations: {
Expand Down Expand Up @@ -109,6 +117,8 @@ function populate(
if (content.details) {
const paragraphText = `${content.title} ${content.details}.`;
upsertParagraph(eventuateDiv, section, paragraphText);
} else {
deleteParagraph(eventuateDiv, section);
}
}
}
Expand All @@ -119,12 +129,14 @@ function eventuate() {
const volunteerWithCountList = rpe
.volunteersList()
.map((vol) => new VolunteerWithCount(vol));
const waitingOn = volunteerWithCountList
.map((v) => v.promisedVols)
.filter((v) => !!v);
const loadingMessage = `Loading volunteer data for ${waitingOn.length} parkrunners. Please wait`;

populate(rpe, volunteerWithCountList); // Initial draw
populate(rpe, volunteerWithCountList, loadingMessage);

Promise.all(
volunteerWithCountList.map((v) => v.promisedVols).filter((v) => !!v),
).then(() => populate(rpe, volunteerWithCountList)); // Refresh with volunteer counts
Promise.all(waitingOn).then(() => populate(rpe, volunteerWithCountList));
}

eventuate();
2 changes: 1 addition & 1 deletion test/transformers/fiveKFinishersToMilestones.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe(fiveKFinishersToMilestones, () => {
"agegrade",
"achievement",
"59:59",
9999999
9999999,
);
});

Expand Down

0 comments on commit c835f01

Please sign in to comment.