Skip to content

Commit

Permalink
wip(hall-of-fame): add dynamic data import
Browse files Browse the repository at this point in the history
  • Loading branch information
ThulinaWickramasinghe committed Aug 17, 2024
1 parent b3e4754 commit 88e269d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
23 changes: 14 additions & 9 deletions src/pages/hall-of-fame.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ScoreCardSkeleton } from "@/components";
import { ScoreCard } from "@/components/home";
import { filters as filterData, sorts as sortData } from "@/filters";
import { useTitle } from "@/hooks";
import { computePastLeaderboardDataPath } from "@/utils/compute-data-path";
import {
AnimatedSwitcher,
Filters,
Expand All @@ -16,22 +17,26 @@ import { useGhostLegion, useRound } from "@sliit-foss/bashaway-ui/hooks";
import { Ghost } from "@sliit-foss/bashaway-ui/icons";
import { Footnote, Title } from "@sliit-foss/bashaway-ui/typography";
import { computeFilterQuery, computeSortQuery } from "@sliit-foss/bashaway-ui/utils";
import { fetchPastLeaderboardData } from "@/utils/fetch-old-data";

const HallOfFame = () => {
const [page, setPage] = useState(1);
const [filters, setFilters] = useState(computeFilterQuery(filterData));
const [sorts, setSorts] = useState(computeSortQuery(sortData));

const [scores, setScores] = useState({});

const { rounds, round, roundKey, onRoundChange } = useRound();

const { ghostLegion, toggleGhostLegion } = useGhostLegion();

const { data: scores, isFetching } = fetchPastLeaderboardData({ page, filters, sorts, round, ghostLegion, year: 2023 });
const dataPath = computePastLeaderboardDataPath({ page, filters, sorts, round, ghostLegion, year: 2023 });

useEffect(() => {
const fetchPastLeaderboadData = async () => {
const data = (await import(`../../data/annual-leaderboard/${dataPath}.json`)).default;
setScores(data);
};

fetchPastLeaderboadData();
}, [dataPath]);

}, [])
useEffect(() => {
if (page !== 1) setPage(1);
}, [filters, sorts, round, ghostLegion]);
Expand Down Expand Up @@ -75,13 +80,13 @@ const HallOfFame = () => {
</div>
</div>
<AnimatedSwitcher
show={isFetching}
show={false}
className={`w-full flex flex-col gap-5 min-h-[150px] xl:min-h-[250px] 2xl:min-h-[350px]`}
component={<ScoreCardSkeleton />}
alternateComponent={
scores?.data?.docs?.length ? (
scores?.length ? (
<>
{scores.data.docs.map((item, index) => (
{scores.map((item, index) => (
<ScoreCard item={item} round={round} key={`score-card-${index}`} />
))}
</>
Expand Down
5 changes: 5 additions & 0 deletions src/utils/compute-data-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const computePastLeaderboardDataPath = ({ page, filters, sorts, round, ghostLegion, year }) => {

Check failure on line 1 in src/utils/compute-data-path.js

View workflow job for this annotation

GitHub Actions / Enforce ESLint rules

'page' is defined but never used

Check failure on line 1 in src/utils/compute-data-path.js

View workflow job for this annotation

GitHub Actions / Enforce ESLint rules

'filters' is defined but never used

Check failure on line 1 in src/utils/compute-data-path.js

View workflow job for this annotation

GitHub Actions / Enforce ESLint rules

'sorts' is defined but never used
const round2Path = ghostLegion == true ? "/ghost-legion" : "/final";
const path = `${year}/round${round}${round == 1 ? "" : round2Path}`;
return path;
};
7 changes: 0 additions & 7 deletions src/utils/fetch-old-data.js

This file was deleted.

0 comments on commit 88e269d

Please sign in to comment.