Skip to content

Commit

Permalink
fix: get fresh branch data upon load
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianAndersen committed Nov 19, 2024
1 parent b0c893d commit 8fa40f3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/app/info/branch/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ const BranchPage = async ({ params }: Params) => {
const { branch, openingHours } = await getBranchData(params.id);

return (
<LinkableBranchInfo branch={branch} openingHours={openingHours ?? []} />
<LinkableBranchInfo
cachedBranch={branch}
cachedOpeningHours={openingHours ?? []}
/>
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/app/info/branch/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const metadata: Metadata = {
};

const BranchPage = async () => {
return <LinkableBranchInfo branch={null} openingHours={[]} />;
return <LinkableBranchInfo cachedBranch={null} cachedOpeningHours={[]} />;
};

export default BranchPage;
52 changes: 47 additions & 5 deletions src/components/LinkableBranchInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,71 @@
import { Branch, OpeningHour } from "@boklisten/bl-model";
import { Card, Typography } from "@mui/material";
import { Box } from "@mui/system";
import moment from "moment/moment";
import { useRouter } from "next/navigation";
import { useEffect } from "react";
import { useEffect, useState } from "react";

import BlFetcher from "@/api/blFetcher";
import BranchSelect from "@/components/BranchSelect";
import BranchInfo from "@/components/info/BranchInfo";
import DynamicNav from "@/components/info/DynamicNav";
import BL_CONFIG from "@/utils/bl-config";
import { infoPageTabs } from "@/utils/constants";
import { assertBlApiError } from "@/utils/types";
import { useGlobalState } from "@/utils/useGlobalState";

function LinkableBranchInfo({
branch,
openingHours,
}: {
type BranchData = {
branch: Branch | null;
openingHours: OpeningHour[];
};

async function getBranchData(branchId: string): Promise<BranchData> {
const branchUrl = `${BL_CONFIG.collection.branch}/${branchId}`;
const now = moment().startOf("day").format("DDMMYYYYHHmm");
const openingHoursUrl = `${BL_CONFIG.collection.openingHour}?branch=${branchId}&from=>${now}`;
const branchData: BranchData = { branch: null, openingHours: [] };
try {
const branches = await BlFetcher.get<Branch[]>(branchUrl);
branchData.branch = branches[0] ?? null;
} catch (error) {
assertBlApiError(error);
}
try {
const openingHoursResult =
await BlFetcher.get<OpeningHour[]>(openingHoursUrl);
branchData.openingHours = openingHoursResult ?? [];
} catch (error) {
assertBlApiError(error);
}

return branchData;
}

function LinkableBranchInfo({
cachedBranch,
cachedOpeningHours,
}: {
cachedBranch: Branch | null;
cachedOpeningHours: OpeningHour[];
}) {
const [branch, setBranch] = useState(cachedBranch);
const [openingHours, setOpeningHours] = useState(cachedOpeningHours);
const router = useRouter();
const { selectedBranchId } = useGlobalState();

useEffect(() => {
if (branch === null && selectedBranchId) {
return router.push(`/info/branch/${selectedBranchId}`);
}
async function getFreshBranchInfo() {
if (branch?.id) {
const branchData = await getBranchData(branch.id);
setBranch(branchData.branch);
setOpeningHours(branchData.openingHours);
}
}
getFreshBranchInfo();
console.log("use effekt lol");
}, [branch, router, selectedBranchId]);

return (
Expand Down

0 comments on commit 8fa40f3

Please sign in to comment.