Data/Season: Stream drivers, races and substitutes as promises for data/season page

This commit is contained in:
2024-12-16 04:03:37 +01:00
parent cb51e01e98
commit 57b4253d99
2 changed files with 89 additions and 66 deletions

View File

@ -1,4 +1,4 @@
import type { Actions, PageServerLoad } from "./$types"; import type { ActionData, Actions, PageServerLoad } from "./$types";
import { import {
form_data_clean, form_data_clean,
form_data_ensure_keys, form_data_ensure_keys,
@ -286,10 +286,13 @@ export const load: PageServerLoad = async ({ fetch, locals }) => {
}; };
return { return {
// Graphics and teams are awaited, since those are visible on page load.
graphics: await fetch_graphics(), graphics: await fetch_graphics(),
teams: await fetch_teams(), teams: await fetch_teams(),
drivers: await fetch_drivers(),
races: await fetch_races(), // The rest is streamed gradually, since the user has to switch tabs to need them.
substitutions: await fetch_substitutions(), drivers: fetch_drivers(),
races: fetch_races(),
substitutions: fetch_substitutions(),
}; };
}; };

View File

@ -17,47 +17,57 @@
} }
// Values for driver cards // Values for driver cards
// Maps driver to team: <driver.id, team.id> let update_driver_team_select_values: { [key: string]: string } = $state({}); // <driver.id, team.id>
let update_driver_team_select_values: { [key: string]: string } = $state({});
let update_driver_active_values: { [key: string]: boolean } = $state({}); let update_driver_active_values: { [key: string]: boolean } = $state({});
data.drivers.forEach((driver: Driver) => { data.drivers.then((drivers: Driver[]) =>
drivers.forEach((driver: Driver) => {
update_driver_team_select_values[driver.id] = driver.team; update_driver_team_select_values[driver.id] = driver.team;
update_driver_active_values[driver.id] = driver.active; update_driver_active_values[driver.id] = driver.active;
}); }),
);
update_driver_team_select_values["create"] = ""; update_driver_team_select_values["create"] = "";
update_driver_active_values["create"] = true; update_driver_active_values["create"] = true;
// Values for substitution cards // Values for substitution cards
let update_substitution_substitute_select_values: { [key: string]: string } = $state({}); const update_substitution_substitute_select_values: { [key: string]: string } = $state({});
let update_substitution_for_select_values: { [key: string]: string } = $state({}); const update_substitution_for_select_values: { [key: string]: string } = $state({});
let update_substitution_race_select_values: { [key: string]: string } = $state({}); const update_substitution_race_select_values: { [key: string]: string } = $state({});
data.substitutions.forEach((substitution: Substitution) => { data.substitutions.then((substitutions: Substitution[]) =>
substitutions.forEach((substitution: Substitution) => {
update_substitution_substitute_select_values[substitution.id] = substitution.substitute; update_substitution_substitute_select_values[substitution.id] = substitution.substitute;
update_substitution_for_select_values[substitution.id] = substitution.for; update_substitution_for_select_values[substitution.id] = substitution.for;
update_substitution_race_select_values[substitution.id] = substitution.race; update_substitution_race_select_values[substitution.id] = substitution.race;
}); }),
);
update_substitution_substitute_select_values["create"] = ""; update_substitution_substitute_select_values["create"] = "";
update_substitution_for_select_values["create"] = ""; update_substitution_for_select_values["create"] = "";
update_substitution_race_select_values["create"] = ""; update_substitution_race_select_values["create"] = "";
// All options to create a <Dropdown> component for the teams // All options to create a <Dropdown> component for the teams
const team_dropdown_options: DropdownOption[] = data.teams.map((team: Team) => { const team_dropdown_options: DropdownOption[] = [];
return { label: team.name, value: team.id, icon_url: team.logo_url } as DropdownOption; data.teams.forEach((team: Team) => {
team_dropdown_options.push({ label: team.name, value: team.id, icon_url: team.logo_url });
}); });
// All options to create a <Dropdown> component for the drivers // All options to create a <Dropdown> component for the drivers
const driver_dropdown_options: DropdownOption[] = data.drivers.map((driver: Driver) => { const driver_dropdown_options: DropdownOption[] = [];
return { data.drivers.then((drivers: Driver[]) =>
drivers.forEach((driver: Driver) => {
driver_dropdown_options.push({
label: driver.code, label: driver.code,
value: driver.id, value: driver.id,
icon_url: driver.headshot_url, icon_url: driver.headshot_url,
} as DropdownOption;
}); });
}),
);
// All options to create a <Dropdown> component for the races // All options to create a <Dropdown> component for the races
const race_dropdown_options: DropdownOption[] = data.races.map((race: Race) => { const race_dropdown_options: DropdownOption[] = [];
return { label: race.name, value: race.id } as DropdownOption; data.races.then((races: Race[]) =>
}); races.forEach((race: Race) => {
race_dropdown_options.push({ label: race.name, value: race.id });
}),
);
</script> </script>
<svelte:head> <svelte:head>
@ -116,7 +126,8 @@
{/if} {/if}
<!-- List all drivers inside the database --> <!-- List all drivers inside the database -->
{#each data.drivers as driver} {#await data.drivers then drivers}
{#each drivers as driver}
<DriverCard <DriverCard
{driver} {driver}
disable_inputs={!data.admin} disable_inputs={!data.admin}
@ -125,6 +136,7 @@
active_value={update_driver_active_values[driver.id]} active_value={update_driver_active_values[driver.id]}
/> />
{/each} {/each}
{/await}
</div> </div>
{:else if current_tab === 2} {:else if current_tab === 2}
<!-- Races Tab --> <!-- Races Tab -->
@ -138,18 +150,21 @@
/> />
{/if} {/if}
{#each data.races as race} {#await data.races then races}
{#each races as race}
<RaceCard {race} disable_inputs={!data.admin} /> <RaceCard {race} disable_inputs={!data.admin} />
{/each} {/each}
{/await}
</div> </div>
{:else if current_tab === 3} {:else if current_tab === 3}
<!-- Substitutions Tab --> <!-- Substitutions Tab -->
<!-- Substitutions Tab --> <!-- Substitutions Tab -->
<!-- Substitutions Tab --> <!-- Substitutions Tab -->
<div class="mt-2 grid grid-cols-1 gap-2 md:grid-cols-2 lg:grid-cols-4 xl:grid-cols-6"> <div class="mt-2 grid grid-cols-1 gap-2 md:grid-cols-2 lg:grid-cols-4 xl:grid-cols-6">
{#await data.drivers then drivers}
{#if data.admin} {#if data.admin}
<SubstitutionCard <SubstitutionCard
drivers={data.drivers} {drivers}
substitute_select_value={update_substitution_substitute_select_values["create"]} substitute_select_value={update_substitution_substitute_select_values["create"]}
driver_select_value={update_substitution_for_select_values["create"]} driver_select_value={update_substitution_for_select_values["create"]}
race_select_value={update_substitution_race_select_values["create"]} race_select_value={update_substitution_race_select_values["create"]}
@ -160,11 +175,14 @@
/> />
{/if} {/if}
{#each data.substitutions as substitution} {#await data.substitutions then substitutions}
{#each substitutions as substitution}
<SubstitutionCard <SubstitutionCard
{substitution} {substitution}
drivers={data.drivers} {drivers}
substitute_select_value={update_substitution_substitute_select_values[substitution.id]} substitute_select_value={update_substitution_substitute_select_values[
substitution.id
]}
driver_select_value={update_substitution_for_select_values[substitution.id]} driver_select_value={update_substitution_for_select_values[substitution.id]}
race_select_value={update_substitution_race_select_values[substitution.id]} race_select_value={update_substitution_race_select_values[substitution.id]}
driver_select_options={driver_dropdown_options} driver_select_options={driver_dropdown_options}
@ -172,6 +190,8 @@
disable_inputs={!data.admin} disable_inputs={!data.admin}
/> />
{/each} {/each}
{/await}
{/await}
</div> </div>
{/if} {/if}
</svelte:fragment> </svelte:fragment>