Skeleton: Fetch RaceResults asynchronously in root layout

This commit is contained in:
2025-02-17 15:58:07 +01:00
parent e58b94022a
commit 9e39547936
4 changed files with 23 additions and 36 deletions

View File

@ -3,7 +3,7 @@ import { pb, pbUser } from "$lib/pocketbase";
// This makes the page client-side rendered // This makes the page client-side rendered
export const ssr = false; 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"; import type { LayoutLoad } from "./$types";
// On each page load (every route), this function runs serverside. // On each page load (every route), this function runs serverside.
@ -76,16 +76,27 @@ export const load: LayoutLoad = () => {
return substitutions; return substitutions;
}; };
const fetch_raceresults = async (): Promise<RaceResult[]> => {
const raceresults: RaceResult[] = await pb
.collection("raceresultsdesc")
.getFullList({ fetch: fetch });
return raceresults;
};
return { return {
// User information // User information (synchronous)
user: pbUser, user: pbUser,
admin: pbUser?.admin ?? false, admin: pbUser?.admin ?? false,
// Return static data asynchronously // Return static data
graphics: fetch_graphics(), graphics: fetch_graphics(),
teams: fetch_teams(), teams: fetch_teams(),
drivers: fetch_drivers(), drivers: fetch_drivers(),
races: fetch_races(), races: fetch_races(),
substitutions: fetch_substitutions(), substitutions: fetch_substitutions(),
// Return other data
raceresults: fetch_raceresults(),
}; };
}; };

View File

@ -11,7 +11,11 @@
const modalStore: ModalStore = getModalStore(); const modalStore: ModalStore = getModalStore();
const result_handler = async (event: Event, id?: string) => { 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; if (id && !result) return;
@ -90,4 +94,6 @@
<span class="font-bold">Create Race Result</span> <span class="font-bold">Create Race Result</span>
</Button> </Button>
</div> </div>
<Table data={data.results} columns={results_columns} handler={result_handler} /> {#await data.raceresults then results}
<Table data={results} columns={results_columns} handler={result_handler} />
{/await}

View File

@ -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<RaceResult[]> => {
const raceresults: RaceResult[] = await pb
.collection("raceresultsdesc")
.getFullList({ fetch: fetch });
return raceresults;
};
return {
results: await fetch_raceresults(),
};
};

View File

@ -1,5 +1,5 @@
import { pb } from "$lib/pocketbase"; 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"; import type { PageLoad } from "../$types";
export const load: PageLoad = async ({ fetch }) => { export const load: PageLoad = async ({ fetch }) => {
@ -36,21 +36,9 @@ export const load: PageLoad = async ({ fetch }) => {
return currentpickedusers; return currentpickedusers;
}; };
// TODO: Duplicated code from data/raceresults/+page.server.ts
const fetch_raceresults = async (): Promise<RaceResult[]> => {
// Don't expand races/pxxs/dnfs since we already fetched those
const raceresults: RaceResult[] = await pb
.collection("raceresultsdesc")
.getFullList({ fetch: fetch });
return raceresults;
};
return { return {
racepicks: fetch_racepicks(), racepicks: fetch_racepicks(),
currentpickedusers: fetch_currentpickedusers(), currentpickedusers: fetch_currentpickedusers(),
raceresults: fetch_raceresults(),
currentrace: await fetch_currentrace(), currentrace: await fetch_currentrace(),
}; };