Skeleton: Fetch static data (teams/drivers/races/substitutions) in global layout asynchronously
All checks were successful
Build Formula11 Docker Image / pocketbase-docker (push) Successful in 29s
All checks were successful
Build Formula11 Docker Image / pocketbase-docker (push) Successful in 29s
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
import type { Driver, Graphic, Race, Substitution, Team } from "$lib/schema";
|
||||
import type { LayoutServerLoad } from "./$types";
|
||||
|
||||
// On each page load (every route), this function runs serverside.
|
||||
@ -6,14 +7,82 @@ import type { LayoutServerLoad } from "./$types";
|
||||
// It will populate the "user" attribute of each page's "data" object,
|
||||
// so each page has access to the current user (or knows if no one is signed in).
|
||||
export const load: LayoutServerLoad = ({ locals }) => {
|
||||
if (locals.user) {
|
||||
return {
|
||||
user: locals.user,
|
||||
admin: locals.user.admin,
|
||||
};
|
||||
}
|
||||
const fetch_graphics = async (): Promise<Graphic[]> => {
|
||||
const graphics: Graphic[] = await locals.pb
|
||||
.collection("graphics")
|
||||
.getFullList({ fetch: fetch });
|
||||
|
||||
graphics.map((graphic: Graphic) => {
|
||||
graphic.file_url = locals.pb.files.getURL(graphic, graphic.file);
|
||||
});
|
||||
|
||||
return graphics;
|
||||
};
|
||||
|
||||
const fetch_teams = async (): Promise<Team[]> => {
|
||||
const teams: Team[] = await locals.pb.collection("teams").getFullList({
|
||||
sort: "+name",
|
||||
fetch: fetch,
|
||||
});
|
||||
|
||||
teams.map((team: Team) => {
|
||||
team.banner_url = locals.pb.files.getURL(team, team.banner);
|
||||
team.logo_url = locals.pb.files.getURL(team, team.logo);
|
||||
});
|
||||
|
||||
return teams;
|
||||
};
|
||||
|
||||
const fetch_drivers = async (): Promise<Driver[]> => {
|
||||
const drivers: Driver[] = await locals.pb.collection("drivers").getFullList({
|
||||
sort: "+code",
|
||||
fetch: fetch,
|
||||
});
|
||||
|
||||
drivers.map((driver: Driver) => {
|
||||
driver.headshot_url = locals.pb.files.getURL(driver, driver.headshot);
|
||||
});
|
||||
|
||||
return drivers;
|
||||
};
|
||||
|
||||
const fetch_races = async (): Promise<Race[]> => {
|
||||
const races: Race[] = await locals.pb.collection("races").getFullList({
|
||||
sort: "+step",
|
||||
fetch: fetch,
|
||||
});
|
||||
|
||||
races.map((race: Race) => {
|
||||
race.pictogram_url = locals.pb.files.getURL(race, race.pictogram);
|
||||
});
|
||||
|
||||
return races;
|
||||
};
|
||||
|
||||
const fetch_substitutions = async (): Promise<Substitution[]> => {
|
||||
const substitutions: Substitution[] = await locals.pb.collection("substitutions").getFullList({
|
||||
expand: "race",
|
||||
fetch: fetch,
|
||||
});
|
||||
|
||||
// Sort by race step (ascending)
|
||||
substitutions.sort(
|
||||
(a: Substitution, b: Substitution) => a.expand.race.step - b.expand.race.step,
|
||||
);
|
||||
|
||||
return substitutions;
|
||||
};
|
||||
|
||||
return {
|
||||
user: undefined,
|
||||
// User information
|
||||
user: locals.user,
|
||||
admin: locals.user?.admin ?? false,
|
||||
|
||||
// Return static data asynchronously
|
||||
graphics: fetch_graphics(),
|
||||
teams: fetch_teams(),
|
||||
drivers: fetch_drivers(),
|
||||
races: fetch_races(),
|
||||
substitutions: fetch_substitutions(),
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user