Racepicks: Stream loaded data and resolve promises in markup
This commit is contained in:
@ -2,65 +2,6 @@ import { form_data_clean, form_data_ensure_keys, form_data_get_and_remove_id } f
|
||||
import type { CurrentPickedUser, Race, RacePick, RaceResult } from "$lib/schema";
|
||||
import type { Actions, PageServerLoad } from "./$types";
|
||||
|
||||
export const load: PageServerLoad = async ({ fetch, locals }) => {
|
||||
const fetch_racepicks = async (): Promise<RacePick[]> => {
|
||||
// Don't expand race/pxx/dnf since we already fetched those
|
||||
const racepicks: RacePick[] = await locals.pb
|
||||
.collection("racepicks")
|
||||
.getFullList({ fetch: fetch, expand: "user" });
|
||||
|
||||
// TODO: Fill in the expanded race pictogram_url fields
|
||||
|
||||
return racepicks;
|
||||
};
|
||||
|
||||
const fetch_currentrace = async (): Promise<Race | null> => {
|
||||
const currentrace: Race[] = await locals.pb.collection("currentrace").getFullList();
|
||||
|
||||
// The currentrace collection either has a single or no entries
|
||||
if (currentrace.length == 0) return null;
|
||||
|
||||
currentrace[0].pictogram_url = await locals.pb.files.getURL(
|
||||
currentrace[0],
|
||||
currentrace[0].pictogram,
|
||||
);
|
||||
|
||||
return currentrace[0];
|
||||
};
|
||||
|
||||
const fetch_currentpickedusers = async (): Promise<CurrentPickedUser[]> => {
|
||||
const currentpickedusers: CurrentPickedUser[] = await locals.pb
|
||||
.collection("currentpickedusers")
|
||||
.getFullList();
|
||||
|
||||
currentpickedusers.map((currentpickeduser: CurrentPickedUser) => {
|
||||
if (currentpickeduser.avatar) {
|
||||
currentpickeduser.avatar_url = locals.pb.files.getURL(
|
||||
currentpickeduser,
|
||||
currentpickeduser.avatar,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
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 locals.pb.collection("raceresultsdesc").getFullList();
|
||||
|
||||
return raceresults;
|
||||
};
|
||||
|
||||
return {
|
||||
racepicks: await fetch_racepicks(),
|
||||
currentrace: await fetch_currentrace(),
|
||||
currentpickedusers: await fetch_currentpickedusers(),
|
||||
raceresults: await fetch_raceresults(),
|
||||
};
|
||||
};
|
||||
|
||||
export const actions = {
|
||||
create_racepick: async ({ request, locals }) => {
|
||||
const data: FormData = form_data_clean(await request.formData());
|
||||
@ -91,3 +32,61 @@ export const actions = {
|
||||
await locals.pb.collection("racepicks").delete(id);
|
||||
},
|
||||
} satisfies Actions;
|
||||
|
||||
export const load: PageServerLoad = async ({ fetch, locals }) => {
|
||||
const fetch_currentrace = async (): Promise<Race | null> => {
|
||||
const currentrace: Race[] = await locals.pb.collection("currentrace").getFullList();
|
||||
|
||||
// The currentrace collection either has a single or no entries
|
||||
if (currentrace.length == 0) return null;
|
||||
|
||||
currentrace[0].pictogram_url = await locals.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 locals.pb
|
||||
.collection("racepicks")
|
||||
.getFullList({ fetch: fetch, expand: "user" });
|
||||
|
||||
return racepicks;
|
||||
};
|
||||
|
||||
const fetch_currentpickedusers = async (): Promise<CurrentPickedUser[]> => {
|
||||
const currentpickedusers: CurrentPickedUser[] = await locals.pb
|
||||
.collection("currentpickedusers")
|
||||
.getFullList();
|
||||
|
||||
currentpickedusers.map((currentpickeduser: CurrentPickedUser) => {
|
||||
if (currentpickeduser.avatar) {
|
||||
currentpickeduser.avatar_url = locals.pb.files.getURL(
|
||||
currentpickeduser,
|
||||
currentpickeduser.avatar,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
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 locals.pb.collection("raceresultsdesc").getFullList();
|
||||
|
||||
return raceresults;
|
||||
};
|
||||
|
||||
return {
|
||||
currentrace: await fetch_currentrace(),
|
||||
|
||||
racepicks: fetch_racepicks(),
|
||||
currentpickedusers: fetch_currentpickedusers(),
|
||||
raceresults: fetch_raceresults(),
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user