Skip to content

Commit

Permalink
Adds new result element code
Browse files Browse the repository at this point in the history
  • Loading branch information
fellmirr committed Nov 21, 2024
1 parent a608be6 commit fa84387
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 37 deletions.
73 changes: 67 additions & 6 deletions components/main/blocks/ResultContentRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { Suspense } from "react";
import links from "./Links/Links.module.scss";
import {
SectionContainerProps,
Expand All @@ -13,6 +13,41 @@ import { GiveBlock } from "./GiveBlock/GiveBlock";
import { ResultsGraphData } from "../../../pages/ResultsPage";
import dynamic from "next/dynamic";
import { stegaClean } from "@sanity/client/stega";
import { ResultsHeadline } from "../../shared/components/ResultsHeadline/ResultsHeadline";

const CumulativeDonationsSkeleton = () => {
return (
<div
style={{
width: "100%",
height: "670px",
backgroundColor: "#f3f4f6",
position: "relative",
overflow: "hidden",
}}
>
LOADING!
<div
style={{
position: "absolute",
top: 0,
left: "-100%",
width: "100%",
height: "100%",
background: "linear-gradient(90deg, transparent, rgba(255,255,255,0.8), transparent)",
animation: "shimmer 1s infinite",
}}
/>
<style>{`
@keyframes shimmer {
100% {
transform: translateX(200%);
}
}
`}</style>
</div>
);
};

/** Dynamic imports */
const CumulativeDonations = dynamic(() =>
Expand Down Expand Up @@ -67,6 +102,30 @@ export const ResultContentRenderer: React.FC<{ content: any; graphData: ResultsG
<Links links={block.links}></Links>
</div>
);
case "resultsheadline":
const totalOutputs = block.outputs.map((output: string) => {
const data = graphData.monthlyDonationsPerOutput.find(
(o) => o.output === mapSanityOutputToDataOutput(output),
);
if (!data) {
return "Did not find data for output type " + output;
}
return {
name:
data.output.indexOf("vitamin") > -1
? data.output
: data.output.toLowerCase(),
outputs: data.total.numberOfOutputs,
};
});

return (
<ResultsHeadline
key={block._key || block._id}
headlineNumbers={graphData.resultsHeadlineNumbers}
totalOutputs={totalOutputs}
/>
);
case "contactinfo":
return (
<ContactInfo
Expand Down Expand Up @@ -108,11 +167,13 @@ export const ResultContentRenderer: React.FC<{ content: any; graphData: ResultsG
}
case "cumulativedonationsgraph":
return (
<CumulativeDonations
key={block._key || block._id}
dailyDonations={graphData.dailyDonations}
graphContext={block.graphcontext}
/>
<Suspense fallback={<CumulativeDonationsSkeleton />}>
<CumulativeDonations
key={block._key || block._id}
dailyDonations={graphData.dailyDonations}
graphContext={block.graphcontext}
/>
</Suspense>
);
case "resultsoutput":
const data = graphData.monthlyDonationsPerOutput.find(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { useRef } from "react";
import { useEffect } from "react";
import { useState } from "react";
import * as Plot from "@observablehq/plot";
import AnimateHeight from "react-animate-height";
import { BlockTablesContent } from "../../../../../main/blocks/BlockTable/BlockTablesContent";
import styles from "./CumulativeDonations.module.scss";
import resultsStyle from "../Shared.module.scss";
import { useDebouncedCallback } from "use-debounce";
Expand All @@ -23,6 +21,9 @@ export const CumulativeDonations: React.FC<{
const resizeGraph = useCallback(() => {
if (graphRef.current) {
graphRef.current.innerHTML = "";
console.log(
`Resizing graph to ${graphRef.current.clientWidth}x${graphRef.current.clientHeight}`,
);
setSize({ width: graphRef.current!.clientWidth, height: graphRef.current!.clientHeight });
}
}, [graphRef]);
Expand All @@ -31,11 +32,16 @@ export const CumulativeDonations: React.FC<{
useEffect(() => {
if (graphRef.current) {
setSize({ width: graphRef.current.clientWidth, height: graphRef.current.clientHeight });
window.addEventListener("resize", () => {
if (graphRef.current) {
const resizeObserver = new ResizeObserver((entries) => {
const newWidth = entries[0].contentRect.width;
console.log(newWidth, size.width);
if (newWidth !== size.width) {
debouncedResizeGraph();
}
});

resizeObserver.observe(graphRef.current);
return () => resizeObserver.disconnect();
}
}, [graphRef]);

Expand Down Expand Up @@ -255,11 +261,6 @@ const dayCount = getDaysInMonth(true).reduce(
[0],
);

const isLeapYear = (year: number) => {
if ((year & 3) != 0) return false;
return year % 100 != 0 || year % 400 == 0;
};

const getDOY = (date: Date) => {
var mn = date.getMonth();
var dn = date.getDate();
Expand Down
2 changes: 0 additions & 2 deletions components/shared/components/ResultsOutput/ResultsOutput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ export const ResultsOutput: React.FC<{
return Math.ceil(Math.max(...Object.values(groupedByPeriod), 0));
}, [transformedMonthlyDonationsPerOutput]);

console.log(`maxY: ${maxY} for ${graphData.output}`);

const [normalizeYAxis, setNormalizeYAxis] = useState(false);

return (
Expand Down
10 changes: 9 additions & 1 deletion pages/ResultsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ResultContentRenderer } from "../components/main/blocks/ResultContentRe
import { MonthlyDonationsPerOutputResult } from "../components/shared/components/Graphs/Results/Outputs/Outputs";
import { token } from "../token";
import { stegaClean } from "@sanity/client/stega";
import { ResultsHadlineNumbers } from "../components/shared/components/ResultsHeadline/ResultsHeadline";

const fetchResultsPageSlug = groq`
{
Expand All @@ -33,6 +34,7 @@ export type ResultsGraphData = {
dailyDonations: DailyDonations;
referralSums: ReferralSumsResult[];
monthlyDonationsPerOutput: MonthlyDonationsPerOutputResult[];
resultsHeadlineNumbers: ResultsHadlineNumbers;
};

export const ResultsPage = withStaticProps(
Expand All @@ -56,6 +58,11 @@ export const ResultsPage = withStaticProps(
);
const monthlyDonationsPerOutput = await monthlyDonationsPerOutputResult.json();

const resultsHeadlineNumbersResult = await fetch(
`${process.env.NEXT_PUBLIC_EFFEKT_API}/results/headline`,
);
const resultsHeadlineNumbers = await resultsHeadlineNumbersResult.json();

return {
appStaticProps,
draftMode,
Expand All @@ -69,6 +76,7 @@ export const ResultsPage = withStaticProps(
referralSums: referralSums.content as ReferralSumsResult[],
monthlyDonationsPerOutput:
monthlyDonationsPerOutput.content as MonthlyDonationsPerOutputResult[],
resultsHeadlineNumbers: resultsHeadlineNumbers.content as ResultsHadlineNumbers,
},
},
}; // satisfies GeneralPageProps (requires next@13);;
Expand Down Expand Up @@ -105,7 +113,7 @@ export const ResultsPage = withStaticProps(
title={header.title}
inngress={header.inngress}
links={header.links}
layout={header.centered ? "centered" : "default"}
layout={header.layout}
/>
</div>

Expand Down
2 changes: 2 additions & 0 deletions studio/schemas/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import { pages } from "./pages/_pages";
import { dashboardpages } from "./dashboard/_dashboardPages";
import plausiblerevenuetracker from "./types/plausiblerevenuetracker";
import opendistributionbutton from "./types/opendistributionbutton";
import resultsheadline from "./types/results/resultsheadline";

const paymentMethods = [vipps, bank, swish, autogiro, avtalegiro] as const;

Expand Down Expand Up @@ -146,6 +147,7 @@ export const types = [
graphcontext,
cumulativedonationsgraph,
resultsoutput,
resultsheadline,
resultssection,
referralgraph,
latex,
Expand Down
20 changes: 2 additions & 18 deletions studio/schemas/types/results/resultsoutput.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
import { BarChart } from "react-feather";
import { outputType } from "./_outputtype";

export default {
type: "object",
name: "resultsoutput",
icon: BarChart,
fields: [
{
type: "string",
name: "outputType",
title: "Output Type",
options: {
list: [
"Bednets",
"Deworming",
"Cash",
"Cash zakat",
"Cash climate fund",
"Vitamin A",
"Malaria treatment",
"Vaccinations",
"Years of food fortification",
],
},
},
outputType,
{
type: "array",
name: "description",
Expand Down
1 change: 1 addition & 0 deletions studio/schemas/types/results/resultssection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default {
{ type: "links" },
{ type: "normalimage" },
{ type: "fullimage" },
{ type: "resultsheadline" },
{ type: "cumulativedonationsgraph" },
{ type: "resultsoutput" },
{ type: "referralgraph" },
Expand Down
2 changes: 1 addition & 1 deletion util/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const longDate = (isoDate: string): string => {
};

export const thousandize = (number: number | null) =>
number !== null ? number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ") : "-";
number !== null ? number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "\u00A0") : "-";

export const thousandizeString = (number: string | null) =>
number !== null ? number.replace(/\B(?=(\d{3})+(?!\d))/g, " ") : "-";

0 comments on commit fa84387

Please sign in to comment.