Lib: Move data fetching functions to library
This commit is contained in:
@ -1,4 +1,12 @@
|
||||
import { pb, pbUser } from "$lib/pocketbase";
|
||||
import {
|
||||
fetch_drivers,
|
||||
fetch_graphics,
|
||||
fetch_raceresults,
|
||||
fetch_races,
|
||||
fetch_substitutions,
|
||||
fetch_teams,
|
||||
} from "$lib/fetch";
|
||||
import { pbUser } from "$lib/pocketbase";
|
||||
|
||||
// This makes the page client-side rendered
|
||||
export const ssr = false;
|
||||
@ -12,91 +20,19 @@ import type { LayoutLoad } 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: LayoutLoad = () => {
|
||||
const fetch_graphics = async (): Promise<Graphic[]> => {
|
||||
const graphics: Graphic[] = await pb.collection("graphics").getFullList({ fetch: fetch });
|
||||
|
||||
graphics.map((graphic: Graphic) => {
|
||||
graphic.file_url = pb.files.getURL(graphic, graphic.file);
|
||||
});
|
||||
|
||||
return graphics;
|
||||
};
|
||||
|
||||
const fetch_teams = async (): Promise<Team[]> => {
|
||||
const teams: Team[] = await pb.collection("teams").getFullList({
|
||||
sort: "+name",
|
||||
fetch: fetch,
|
||||
});
|
||||
|
||||
teams.map((team: Team) => {
|
||||
team.banner_url = pb.files.getURL(team, team.banner);
|
||||
team.logo_url = pb.files.getURL(team, team.logo);
|
||||
});
|
||||
|
||||
return teams;
|
||||
};
|
||||
|
||||
const fetch_drivers = async (): Promise<Driver[]> => {
|
||||
const drivers: Driver[] = await pb.collection("drivers").getFullList({
|
||||
sort: "+code",
|
||||
fetch: fetch,
|
||||
});
|
||||
|
||||
drivers.map((driver: Driver) => {
|
||||
driver.headshot_url = pb.files.getURL(driver, driver.headshot);
|
||||
});
|
||||
|
||||
return drivers;
|
||||
};
|
||||
|
||||
const fetch_races = async (): Promise<Race[]> => {
|
||||
const races: Race[] = await pb.collection("races").getFullList({
|
||||
sort: "+step",
|
||||
fetch: fetch,
|
||||
});
|
||||
|
||||
races.map((race: Race) => {
|
||||
race.pictogram_url = pb.files.getURL(race, race.pictogram);
|
||||
});
|
||||
|
||||
return races;
|
||||
};
|
||||
|
||||
const fetch_substitutions = async (): Promise<Substitution[]> => {
|
||||
const substitutions: Substitution[] = await 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;
|
||||
};
|
||||
|
||||
const fetch_raceresults = async (): Promise<RaceResult[]> => {
|
||||
const raceresults: RaceResult[] = await pb
|
||||
.collection("raceresultsdesc")
|
||||
.getFullList({ fetch: fetch });
|
||||
|
||||
return raceresults;
|
||||
};
|
||||
|
||||
return {
|
||||
// User information (synchronous)
|
||||
user: pbUser,
|
||||
admin: pbUser?.admin ?? false,
|
||||
|
||||
// Return static data
|
||||
graphics: fetch_graphics(),
|
||||
teams: fetch_teams(),
|
||||
drivers: fetch_drivers(),
|
||||
races: fetch_races(),
|
||||
substitutions: fetch_substitutions(),
|
||||
graphics: fetch_graphics(fetch),
|
||||
teams: fetch_teams(fetch),
|
||||
drivers: fetch_drivers(fetch),
|
||||
races: fetch_races(fetch),
|
||||
substitutions: fetch_substitutions(fetch),
|
||||
|
||||
// Return other data
|
||||
raceresults: fetch_raceresults(),
|
||||
raceresults: fetch_raceresults(fetch),
|
||||
};
|
||||
};
|
||||
|
||||
@ -1,21 +1,8 @@
|
||||
import { pb } from "$lib/pocketbase";
|
||||
import type { User } from "$lib/schema";
|
||||
import { fetch_users } from "$lib/fetch";
|
||||
import type { PageLoad } from "../../$types";
|
||||
|
||||
export const load: PageLoad = async ({ fetch }) => {
|
||||
const fetch_users = async (): Promise<User[]> => {
|
||||
const users: User[] = await pb
|
||||
.collection("users")
|
||||
.getFullList({ fetch: fetch, sort: "+username" });
|
||||
|
||||
users.map((user: User) => {
|
||||
user.avatar_url = pb.files.getURL(user, user.avatar);
|
||||
});
|
||||
|
||||
return users;
|
||||
};
|
||||
|
||||
return {
|
||||
users: await fetch_users(),
|
||||
users: await fetch_users(fetch),
|
||||
};
|
||||
};
|
||||
|
||||
@ -1,45 +1,11 @@
|
||||
import { pb } from "$lib/pocketbase";
|
||||
import type { CurrentPickedUser, Race, RacePick } from "$lib/schema";
|
||||
import { fetch_currentpickedusers, fetch_currentrace, fetch_racepicks } from "$lib/fetch";
|
||||
import type { PageLoad } from "../$types";
|
||||
|
||||
export const load: PageLoad = async ({ fetch }) => {
|
||||
const fetch_currentrace = async (): Promise<Race | null> => {
|
||||
const currentrace: Race[] = await pb.collection("currentrace").getFullList({ fetch: fetch });
|
||||
|
||||
// The currentrace collection either has a single or no entries
|
||||
if (currentrace.length == 0) return null;
|
||||
|
||||
currentrace[0].pictogram_url = pb.files.getURL(currentrace[0], currentrace[0].pictogram);
|
||||
|
||||
return currentrace[0];
|
||||
};
|
||||
|
||||
const fetch_racepicks = async (): Promise<RacePick[]> => {
|
||||
// Don't expand race/pxx/dnf since we already fetched those
|
||||
const racepicks: RacePick[] = await pb
|
||||
.collection("racepicks")
|
||||
.getFullList({ fetch: fetch, expand: "user" });
|
||||
|
||||
return racepicks;
|
||||
};
|
||||
|
||||
const fetch_currentpickedusers = async (): Promise<CurrentPickedUser[]> => {
|
||||
const currentpickedusers: CurrentPickedUser[] = await pb
|
||||
.collection("currentpickedusers")
|
||||
.getFullList({ fetch: fetch });
|
||||
|
||||
currentpickedusers.map((currentpickeduser: CurrentPickedUser) => {
|
||||
if (currentpickeduser.avatar) {
|
||||
currentpickeduser.avatar_url = pb.files.getURL(currentpickeduser, currentpickeduser.avatar);
|
||||
}
|
||||
});
|
||||
|
||||
return currentpickedusers;
|
||||
};
|
||||
return {
|
||||
racepicks: fetch_racepicks(),
|
||||
currentpickedusers: fetch_currentpickedusers(),
|
||||
racepicks: fetch_racepicks(fetch),
|
||||
currentpickedusers: fetch_currentpickedusers(fetch),
|
||||
|
||||
currentrace: await fetch_currentrace(),
|
||||
currentrace: await fetch_currentrace(fetch),
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user