Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load total number of donated trees dynamically #2056

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions pages/sites/[slug]/[locale]/all.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { handleError, APIError } from '@planet-sdk/common';
import {
LeaderBoardList,
TenantScore,
TreesDonated,
} from '../../../../src/features/common/types/leaderboard';
import { useTenant } from '../../../../src/features/common/Layout/TenantContext';
import { Tenant } from '@planet-sdk/common/build/types/tenant';
Expand All @@ -32,7 +33,7 @@ export default function Home({ pageProps }: Props) {
const [leaderboard, setLeaderboard] = React.useState<LeaderBoardList | null>(
null
);
const { redirect, setErrors } = React.useContext(ErrorHandlingContext);
const { setErrors } = React.useContext(ErrorHandlingContext);

const router = useRouter();
const { setTenantConfig } = useTenant();
Expand Down Expand Up @@ -77,17 +78,37 @@ export default function Home({ pageProps }: Props) {
loadTenantScore();
}, []);


const [treesDonated, setTreesDonated] = React.useState<TreesDonated | null>(
null
);

React.useEffect(() => {
async function loadTreesDonated() {
try {
const newTreesDonated = await getRequest<TreesDonated>(
pageProps.tenantConfig.id,
'https://automate.plant-for-the-planet.org/webhook/platform/total-tree-count'
);
setTreesDonated(newTreesDonated);
} catch (err) {
setErrors(handleError(err as APIError));
}
}
loadTreesDonated();
}, []);

let AllPage;
function getAllPage() {
switch (pageProps.tenantConfig.config.slug) {
case 'planet':
AllPage = (
<LeaderBoard leaderboard={leaderboard} tenantScore={tenantScore} />
<LeaderBoard leaderboard={leaderboard} tenantScore={tenantScore} treesDonated={treesDonated} />
);
return AllPage;
case 'ttc':
AllPage = (
<LeaderBoard leaderboard={leaderboard} tenantScore={tenantScore} />
<LeaderBoard leaderboard={leaderboard} tenantScore={tenantScore} treesDonated={treesDonated}/>
);
return AllPage;
default:
Expand Down
5 changes: 5 additions & 0 deletions src/features/common/types/leaderboard.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ export type LeaderBoardList = {
mostRecent: LeaderBoardItem[];
mostDonated: LeaderBoardItem[];
};

export type TreesDonated = {
trees_since_2019: number;
updated_on: string;
};
13 changes: 9 additions & 4 deletions src/tenants/planet/LeaderBoard/components/Stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import StatsInfoModal from './StatsInfoModal';
import { useLocale, useTranslations } from 'next-intl';
import { localizedAbbreviatedNumber } from '../../../../utils/getFormattedNumber';
import { ThemeContext } from '../../../../theme/themeContext';
import {
TenantScore,
TreesDonated,
} from '../../../../features/common/types/leaderboard';

interface Props {
tenantScore: any;
tenantScore: TenantScore;
treesDonated: TreesDonated;
}
export default function Stats({ tenantScore }: Props): ReactElement {
const [infoExpanded, setInfoExpanded] = React.useState(null);
export default function Stats({ tenantScore, treesDonated }: Props): ReactElement {
const [infoExpanded, setInfoExpanded] = React.useState<String | null>(null);
const tPlanet = useTranslations('Planet');
const locale = useLocale();
const [openModal, setModalOpen] = React.useState(false);
Expand All @@ -25,7 +30,7 @@ export default function Stats({ tenantScore }: Props): ReactElement {
<div className={styles.container}>
<div className={styles.statCard}>
<h2 className={styles.statNumber}>
{localizedAbbreviatedNumber(locale, Number(79586370), 2)}
{localizedAbbreviatedNumber(locale, Number(treesDonated.trees_since_2019), 2)}
</h2>
<h3 className={styles.statText}>{tPlanet('treesDonated')}</h3>
<button
Expand Down
5 changes: 4 additions & 1 deletion src/tenants/planet/LeaderBoard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ import Stories from './components/Stories';
import {
LeaderBoardList,
TenantScore,
TreesDonated,
} from '../../../features/common/types/leaderboard';

interface Props {
leaderboard: LeaderBoardList | null;
tenantScore: TenantScore | null;
treesDonated: TreesDonated | null;
}

export default function index({
leaderboard,
tenantScore,
treesDonated,
}: Props): ReactElement {
return (
<div>
<Score leaderboard={leaderboard} />
{tenantScore && <Stats tenantScore={tenantScore} />}
{(tenantScore && treesDonated) && <Stats tenantScore={tenantScore} treesDonated={treesDonated}/>}
<Stories />
<Footer />
</div>
Expand Down
Loading