diff --git a/src/app/info/branch/[id]/page.tsx b/src/app/info/branch/[id]/page.tsx index a0e08d8..3646c21 100644 --- a/src/app/info/branch/[id]/page.tsx +++ b/src/app/info/branch/[id]/page.tsx @@ -63,7 +63,10 @@ const BranchPage = async ({ params }: Params) => { const { branch, openingHours } = await getBranchData(params.id); return ( - + ); }; diff --git a/src/app/info/branch/page.tsx b/src/app/info/branch/page.tsx index 45df848..ecc92aa 100644 --- a/src/app/info/branch/page.tsx +++ b/src/app/info/branch/page.tsx @@ -9,7 +9,7 @@ export const metadata: Metadata = { }; const BranchPage = async () => { - return ; + return ; }; export default BranchPage; diff --git a/src/components/LinkableBranchInfo.tsx b/src/components/LinkableBranchInfo.tsx index 494b471..ecd68bd 100644 --- a/src/components/LinkableBranchInfo.tsx +++ b/src/components/LinkableBranchInfo.tsx @@ -2,22 +2,55 @@ 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 { + 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(branchUrl); + branchData.branch = branches[0] ?? null; + } catch (error) { + assertBlApiError(error); + } + try { + const openingHoursResult = + await BlFetcher.get(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(); @@ -25,6 +58,15 @@ function LinkableBranchInfo({ 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 (