From 9e39547936db4c1c2625cfd58599feeb8ab566e6 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Mon, 17 Feb 2025 15:58:07 +0100 Subject: [PATCH] Skeleton: Fetch RaceResults asynchronously in root layout --- src/routes/+layout.ts | 17 ++++++++++++++--- src/routes/data/raceresults/+page.svelte | 10 ++++++++-- src/routes/data/raceresults/+page.ts | 18 ------------------ src/routes/racepicks/+page.ts | 14 +------------- 4 files changed, 23 insertions(+), 36 deletions(-) delete mode 100644 src/routes/data/raceresults/+page.ts diff --git a/src/routes/+layout.ts b/src/routes/+layout.ts index feb09ee..04d8b43 100644 --- a/src/routes/+layout.ts +++ b/src/routes/+layout.ts @@ -3,7 +3,7 @@ import { pb, pbUser } from "$lib/pocketbase"; // This makes the page client-side rendered export const ssr = false; -import type { Driver, Graphic, Race, Substitution, Team } from "$lib/schema"; +import type { Driver, Graphic, Race, RaceResult, Substitution, Team } from "$lib/schema"; import type { LayoutLoad } from "./$types"; // On each page load (every route), this function runs serverside. @@ -76,16 +76,27 @@ export const load: LayoutLoad = () => { return substitutions; }; + const fetch_raceresults = async (): Promise => { + const raceresults: RaceResult[] = await pb + .collection("raceresultsdesc") + .getFullList({ fetch: fetch }); + + return raceresults; + }; + return { - // User information + // User information (synchronous) user: pbUser, admin: pbUser?.admin ?? false, - // Return static data asynchronously + // Return static data graphics: fetch_graphics(), teams: fetch_teams(), drivers: fetch_drivers(), races: fetch_races(), substitutions: fetch_substitutions(), + + // Return other data + raceresults: fetch_raceresults(), }; }; diff --git a/src/routes/data/raceresults/+page.svelte b/src/routes/data/raceresults/+page.svelte index 7ec9061..8ad61ae 100644 --- a/src/routes/data/raceresults/+page.svelte +++ b/src/routes/data/raceresults/+page.svelte @@ -11,7 +11,11 @@ const modalStore: ModalStore = getModalStore(); const result_handler = async (event: Event, id?: string) => { - const result: RaceResult | undefined = get_by_value(data.results, "id", id ?? "Invalid"); + const result: RaceResult | undefined = get_by_value( + await data.raceresults, + "id", + id ?? "Invalid", + ); if (id && !result) return; @@ -90,4 +94,6 @@ Create Race Result - +{#await data.raceresults then results} +
+{/await} diff --git a/src/routes/data/raceresults/+page.ts b/src/routes/data/raceresults/+page.ts deleted file mode 100644 index 91c36fb..0000000 --- a/src/routes/data/raceresults/+page.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { pb } from "$lib/pocketbase"; -import type { RaceResult } from "$lib/schema"; -import type { PageLoad } from "../../$types"; - -export const load: PageLoad = async ({ fetch }) => { - // TODO: Duplicated code from racepicks/+page.server.ts - const fetch_raceresults = async (): Promise => { - const raceresults: RaceResult[] = await pb - .collection("raceresultsdesc") - .getFullList({ fetch: fetch }); - - return raceresults; - }; - - return { - results: await fetch_raceresults(), - }; -}; diff --git a/src/routes/racepicks/+page.ts b/src/routes/racepicks/+page.ts index c7cc16d..20ca9eb 100644 --- a/src/routes/racepicks/+page.ts +++ b/src/routes/racepicks/+page.ts @@ -1,5 +1,5 @@ import { pb } from "$lib/pocketbase"; -import type { CurrentPickedUser, Race, RacePick, RaceResult } from "$lib/schema"; +import type { CurrentPickedUser, Race, RacePick } from "$lib/schema"; import type { PageLoad } from "../$types"; export const load: PageLoad = async ({ fetch }) => { @@ -36,21 +36,9 @@ export const load: PageLoad = async ({ fetch }) => { return currentpickedusers; }; - - // TODO: Duplicated code from data/raceresults/+page.server.ts - const fetch_raceresults = async (): Promise => { - // Don't expand races/pxxs/dnfs since we already fetched those - const raceresults: RaceResult[] = await pb - .collection("raceresultsdesc") - .getFullList({ fetch: fetch }); - - return raceresults; - }; - return { racepicks: fetch_racepicks(), currentpickedusers: fetch_currentpickedusers(), - raceresults: fetch_raceresults(), currentrace: await fetch_currentrace(), };