Compare commits

...

8 Commits

Author SHA1 Message Date
8cb665cae8 Lib: Fix nullpointer in RacePickCard/SubstitutionCard preview handler
All checks were successful
Build Formula11 Docker Image / pocketbase-docker (push) Successful in 26s
2025-02-05 03:01:29 +01:00
da47668c29 Lib: Simplify RacePickCard 2025-02-05 02:56:31 +01:00
f90f5734e8 Lib: Simplify SubstitutionCard 2025-02-05 02:33:46 +01:00
042cb42e65 Lib: Simplify RaceCard 2025-02-05 02:11:02 +01:00
2a51c76e2f Lib: Remove caching from dropdown options generators 2025-02-05 01:49:40 +01:00
206d897fca Lib: Simplify DriverCard 2025-02-05 01:49:30 +01:00
603c7d0e40 Lib: Simplify TeamCard 2025-02-05 01:48:37 +01:00
347b5e1470 Racepicks: Fix progressive form enhancement (derive variables correctly) 2025-02-04 23:38:39 +01:00
11 changed files with 806 additions and 1092 deletions

View File

@ -7,138 +7,111 @@
type ModalStore, type ModalStore,
} from "@skeletonlabs/skeleton"; } from "@skeletonlabs/skeleton";
import { Button, Input, Card, Dropdown } from "$lib/components"; import { Button, Input, Card, Dropdown } from "$lib/components";
import type { Driver, Team } from "$lib/schema"; import type { Driver } from "$lib/schema";
import { DRIVER_HEADSHOT_HEIGHT, DRIVER_HEADSHOT_WIDTH } from "$lib/config"; import { DRIVER_HEADSHOT_HEIGHT, DRIVER_HEADSHOT_WIDTH } from "$lib/config";
import { team_dropdown_options } from "$lib/dropdown"; import { team_dropdown_options } from "$lib/dropdown";
import { enhance } from "$app/forms"; import { enhance } from "$app/forms";
import { get_driver_headshot_template } from "$lib/database";
interface DriverCardProps { interface DriverCardProps {
/** Data passed from the page context */
data: any;
/** The [Driver] object used to prefill values. */ /** The [Driver] object used to prefill values. */
driver?: Driver | undefined; driver?: Driver;
/** The teams (for the dropdown options) */
teams: Team[];
/** Disable all inputs if [true] */
disable_inputs?: boolean;
/** Require all inputs if [true] */
require_inputs?: boolean;
/** The [src] of the driver headshot template preview */
headshot_template?: string;
/** The value this component's team select dropdown will bind to */
// TODO: Move this into this component? Why am I passing it from the outside?
// This also applies to the other card components...
team_select_value: string;
/** The value this component's active switch will bind to */
active_value: boolean;
} }
let { let { data, driver = undefined }: DriverCardProps = $props();
driver = undefined,
teams,
disable_inputs = false,
require_inputs = false,
headshot_template = undefined,
team_select_value,
active_value,
}: DriverCardProps = $props();
const modalStore: ModalStore = getModalStore(); const modalStore: ModalStore = getModalStore();
if ($modalStore[0].meta) { if ($modalStore[0].meta) {
const meta = $modalStore[0].meta; const meta = $modalStore[0].meta;
// Stuff thats required for the "update" card data = meta.data;
driver = meta.driver; driver = meta.driver;
teams = meta.teams;
team_select_value = meta.team_select_value;
active_value = meta.active_value;
disable_inputs = meta.disable_inputs;
// Stuff thats additionally required for the "create" card
require_inputs = meta.require_inputs;
headshot_template = meta.headshot_template;
} }
const required: boolean = $derived(!driver);
const disabled: boolean = $derived(!data.admin);
const labelwidth: string = "120px";
let team_select_value: string = $state(driver?.team ?? "");
let active_value: boolean = $state(driver?.active ?? true);
</script> </script>
{#await data.graphics then graphics}
<Card <Card
imgsrc={driver?.headshot_url ?? headshot_template} imgsrc={driver?.headshot_url ?? get_driver_headshot_template(graphics)}
imgid="update_driver_headshot_preview_{driver?.id ?? 'create'}" imgid="headshot_preview"
width="w-full sm:w-auto" width="w-full sm:w-auto"
imgwidth={DRIVER_HEADSHOT_WIDTH} imgwidth={DRIVER_HEADSHOT_WIDTH}
imgheight={DRIVER_HEADSHOT_HEIGHT} imgheight={DRIVER_HEADSHOT_HEIGHT}
imgonclick={(event: Event) => modalStore.close()} imgonclick={(event: Event) => modalStore.close()}
> >
<form method="POST" enctype="multipart/form-data" use:enhance> <form
method="POST"
enctype="multipart/form-data"
use:enhance
onsubmit={() => modalStore.close()}
>
<!-- This is also disabled, because the ID should only be --> <!-- This is also disabled, because the ID should only be -->
<!-- "leaked" to users that are allowed to use the inputs --> <!-- "leaked" to users that are allowed to use the inputs -->
{#if driver && !disable_inputs} {#if driver && !disabled}
<input name="id" type="hidden" value={driver.id} /> <input name="id" type="hidden" value={driver.id} />
{/if} {/if}
<div class="flex flex-col gap-2"> <div class="flex flex-col gap-2">
<!-- Driver name input --> <!-- Driver name input -->
<Input <Input name="firstname" value={driver?.firstname ?? ""} {labelwidth} {disabled} {required}>
id="driver_first_name_{driver?.id ?? 'create'}" First Name
name="firstname"
value={driver?.firstname ?? ""}
autocomplete="off"
labelwidth="120px"
disabled={disable_inputs}
required={require_inputs}
>First Name
</Input> </Input>
<Input <Input
id="driver_last_name_{driver?.id ?? 'create'}"
name="lastname" name="lastname"
value={driver?.lastname ?? ""} value={driver?.lastname ?? ""}
autocomplete="off" autocomplete="off"
labelwidth="120px" {labelwidth}
disabled={disable_inputs} {disabled}
required={require_inputs} {required}
>Last Name >
Last Name
</Input> </Input>
<Input <Input
id="driver_code_{driver?.id ?? 'create'}"
name="code" name="code"
value={driver?.code ?? ""} value={driver?.code ?? ""}
autocomplete="off" autocomplete="off"
minlength={3} minlength={3}
maxlength={3} maxlength={3}
labelwidth="120px" {labelwidth}
disabled={disable_inputs} {disabled}
required={require_inputs} {required}
>Driver Code >
Driver Code
</Input> </Input>
<!-- Driver team input --> <!-- Driver team input -->
{#await data.teams then teams}
<Dropdown <Dropdown
name="team" name="team"
input_variable={team_select_value} input_variable={team_select_value}
options={team_dropdown_options(teams)} options={team_dropdown_options(teams)}
labelwidth="120px" {labelwidth}
disabled={disable_inputs} {disabled}
required={require_inputs} {required}
> >
Team Team
</Dropdown> </Dropdown>
{/await}
<!-- Headshot upload --> <!-- Headshot upload -->
<FileDropzone <FileDropzone
name="headshot" name="headshot"
id="driver_headshot_{driver?.id ?? 'create'}" onchange={get_image_preview_event_handler("headshot_preview")}
onchange={get_image_preview_event_handler( {disabled}
`update_driver_headshot_preview_${driver?.id ?? "create"}`, {required}
)}
disabled={disable_inputs}
required={require_inputs}
>
<svelte:fragment slot="message"
><span class="font-bold">Upload Headshot</span></svelte:fragment
> >
<svelte:fragment slot="message">
<span class="font-bold">Upload Headshot</span>
</svelte:fragment>
</FileDropzone> </FileDropzone>
<!-- Save/Delete buttons --> <!-- Save/Delete buttons -->
@ -149,39 +122,18 @@
background="bg-primary-500" background="bg-primary-500"
active="bg-tertiary-500" active="bg-tertiary-500"
bind:checked={active_value} bind:checked={active_value}
disabled={disable_inputs} {disabled}
/> />
</div> </div>
{#if driver} {#if driver}
<Button <Button formaction="?/update_driver" color="secondary" {disabled} submit width="w-1/2">
formaction="?/update_driver"
color="secondary"
disabled={disable_inputs}
submit
width="w-1/2"
onclick={() => modalStore.close()}
>
Save Save
</Button> </Button>
<Button <Button formaction="?/delete_driver" color="primary" {disabled} submit width="w-1/2">
color="primary"
submit
disabled={disable_inputs}
formaction="?/delete_driver"
width="w-1/2"
onclick={() => modalStore.close()}
>
Delete Delete
</Button> </Button>
{:else} {:else}
<Button <Button formaction="?/create_driver" color="tertiary" {disabled} submit width="w-full">
formaction="?/create_driver"
color="tertiary"
submit
width="w-full"
disabled={disable_inputs}
onclick={() => modalStore.close()}
>
Create Driver Create Driver
</Button> </Button>
{/if} {/if}
@ -189,3 +141,4 @@
</div> </div>
</form> </form>
</Card> </Card>
{/await}

View File

@ -6,210 +6,188 @@
import { format } from "date-fns"; import { format } from "date-fns";
import { RACE_PICTOGRAM_HEIGHT, RACE_PICTOGRAM_WIDTH } from "$lib/config"; import { RACE_PICTOGRAM_HEIGHT, RACE_PICTOGRAM_WIDTH } from "$lib/config";
import { enhance } from "$app/forms"; import { enhance } from "$app/forms";
import { get_race_pictogram_template } from "$lib/database";
interface RaceCardProps { interface RaceCardProps {
/** Data passed from the page context */
data: any;
/** The [Race] object used to prefill values. */ /** The [Race] object used to prefill values. */
race?: Race | undefined; race?: Race;
/** Disable all inputs if [true] */
disable_inputs?: boolean;
/** Require all inputs if [true] */
require_inputs?: boolean;
/** The [src] of the race pictogram template preview */
pictogram_template?: string;
} }
let { let { data, race = undefined }: RaceCardProps = $props();
race = undefined,
disable_inputs = false,
require_inputs = false,
pictogram_template = "",
}: RaceCardProps = $props();
const modalStore: ModalStore = getModalStore(); const modalStore: ModalStore = getModalStore();
if ($modalStore[0].meta) { if ($modalStore[0].meta) {
const meta = $modalStore[0].meta; const meta = $modalStore[0].meta;
// Stuff thats required for the "update" card data = meta.data;
race = meta.race; race = meta.race;
disable_inputs = meta.disable_inputs;
// Stuff thats additionally required for the "create" card
require_inputs = meta.require_inputs;
pictogram_template = meta.pictogram_template;
} }
const clear_sprint = () => {
(document.getElementById("sqdate") as HTMLInputElement).value = "";
(document.getElementById("sdate") as HTMLInputElement).value = "";
};
const required: boolean = $derived(!race);
const disabled: boolean = $derived(!data.admin);
const labelwidth = "80px";
// Dates have to be formatted to datetime-local format // Dates have to be formatted to datetime-local format
const dateformat: string = "yyyy-MM-dd'T'HH:mm"; const dateformat: string = "yyyy-MM-dd'T'HH:mm";
const sprintqualidate: string | undefined = const sprintqualidate: string | undefined = $derived(
race && race.sprintqualidate ? format(new Date(race.sprintqualidate), dateformat) : undefined; race?.sprintqualidate ? format(new Date(race.sprintqualidate), dateformat) : undefined,
const sprintdate: string | undefined = );
race && race.sprintdate ? format(new Date(race.sprintdate), dateformat) : undefined; const sprintdate: string | undefined = $derived(
const qualidate: string | undefined = race race?.sprintdate ? format(new Date(race.sprintdate), dateformat) : undefined,
? format(new Date(race.qualidate), dateformat) );
: undefined; const qualidate: string | undefined = $derived(
const racedate: string | undefined = race race ? format(new Date(race.qualidate), dateformat) : undefined,
? format(new Date(race.racedate), dateformat) );
: undefined; const racedate: string | undefined = $derived(
race ? format(new Date(race.racedate), dateformat) : undefined,
const clear_sprint = () => { );
const sprintquali: HTMLInputElement = document.getElementById(
`race_sprintqualidate_${race?.id ?? "create"}`,
) as HTMLInputElement;
const sprint: HTMLInputElement = document.getElementById(
`race_sprintdate_${race?.id ?? "create"}`,
) as HTMLInputElement;
sprintquali.value = "";
sprint.value = "";
};
const labelwidth = "80px";
</script> </script>
{#await data.graphics then graphics}
<Card <Card
imgsrc={race?.pictogram_url ?? pictogram_template} imgsrc={race?.pictogram_url ?? get_race_pictogram_template(graphics)}
imgid="update_race_pictogram_preview_{race?.id ?? 'create'}" imgid="pictogram_preview"
width="w-full sm:w-auto" width="w-full sm:w-auto"
imgwidth={RACE_PICTOGRAM_WIDTH} imgwidth={RACE_PICTOGRAM_WIDTH}
imgheight={RACE_PICTOGRAM_HEIGHT} imgheight={RACE_PICTOGRAM_HEIGHT}
imgonclick={(event: Event) => modalStore.close()} imgonclick={(event: Event) => modalStore.close()}
> >
<form method="POST" enctype="multipart/form-data" use:enhance> <form
method="POST"
enctype="multipart/form-data"
use:enhance
onsubmit={() => modalStore.close()}
>
<!-- This is also disabled, because the ID should only be --> <!-- This is also disabled, because the ID should only be -->
<!-- "leaked" to users that are allowed to use the inputs --> <!-- "leaked" to users that are allowed to use the inputs -->
{#if race && !disable_inputs} {#if race && !disabled}
<input name="id" type="hidden" value={race.id} /> <input name="id" type="hidden" value={race.id} />
{/if} {/if}
<div class="flex flex-col gap-2"> <div class="flex flex-col gap-2">
<!-- Driver name input --> <!-- Driver name input -->
<Input <Input
id="race_name_{race?.id ?? 'create'}"
name="name" name="name"
value={race?.name ?? ""} value={race?.name ?? ""}
autocomplete="off" autocomplete="off"
{labelwidth} {labelwidth}
disabled={disable_inputs} {disabled}
required={require_inputs}>Name</Input {required}
> >
Name
</Input>
<Input <Input
id="race_step_{race?.id ?? 'create'}"
name="step" name="step"
value={race?.step ?? ""} value={race?.step ?? ""}
autocomplete="off" autocomplete="off"
{labelwidth}
type="number" type="number"
min={1} min={1}
max={24} max={24}
disabled={disable_inputs} {labelwidth}
required={require_inputs}>Step</Input {disabled}
{required}
> >
Step
</Input>
<Input <Input
id="race_pxx_{race?.id ?? 'create'}"
name="pxx" name="pxx"
value={race?.pxx ?? ""} value={race?.pxx ?? ""}
autocomplete="off" autocomplete="off"
{labelwidth}
type="number" type="number"
min={1} min={1}
max={20} max={20}
disabled={disable_inputs} {labelwidth}
required={require_inputs}>PXX</Input {disabled}
{required}
> >
PXX
</Input>
<!-- NOTE: Input datetime-local accepts YYYY-mm-ddTHH:MM format --> <!-- NOTE: Input datetime-local accepts YYYY-mm-ddTHH:MM format -->
<Input <Input
id="race_sprintqualidate_{race?.id ?? 'create'}" id="sqdate"
name="sprintqualidate" name="sprintqualidate"
value={sprintqualidate ?? ""} value={sprintqualidate ?? ""}
autocomplete="off" autocomplete="off"
{labelwidth}
type="datetime-local" type="datetime-local"
disabled={disable_inputs}>SQuali</Input {labelwidth}
{disabled}
> >
SQuali
</Input>
<Input <Input
id="race_sprintdate_{race?.id ?? 'create'}" id="sdate"
name="sprintdate" name="sprintdate"
value={sprintdate ?? ""} value={sprintdate ?? ""}
autocomplete="off" autocomplete="off"
{labelwidth}
type="datetime-local" type="datetime-local"
disabled={disable_inputs}>SRace</Input {labelwidth}
{disabled}
> >
SRace
</Input>
<Input <Input
id="race_qualidate_{race?.id ?? 'create'}"
name="qualidate" name="qualidate"
value={qualidate ?? ""} value={qualidate ?? ""}
autocomplete="off" autocomplete="off"
{labelwidth}
type="datetime-local" type="datetime-local"
disabled={disable_inputs} {labelwidth}
required={require_inputs}>Quali</Input {disabled}
{required}
> >
Quali
</Input>
<Input <Input
id="race_racedate_{race?.id ?? 'create'}"
name="racedate" name="racedate"
value={racedate ?? ""} value={racedate ?? ""}
autocomplete="off" autocomplete="off"
{labelwidth}
type="datetime-local" type="datetime-local"
disabled={disable_inputs} {labelwidth}
required={require_inputs}>Race</Input {disabled}
{required}
> >
Race
</Input>
<!-- Headshot upload --> <!-- Headshot upload -->
<FileDropzone <FileDropzone
name="pictogram" name="pictogram"
id="race_pictogram_{race?.id ?? 'create'}" onchange={get_image_preview_event_handler("pictogram_preview")}
onchange={get_image_preview_event_handler( {disabled}
`update_race_pictogram_preview_${race?.id ?? "create"}`, {required}
)}
disabled={disable_inputs}
required={require_inputs}
>
<svelte:fragment slot="message"
><span class="font-bold">Upload Pictogram</span></svelte:fragment
> >
<svelte:fragment slot="message">
<span class="font-bold">Upload Pictogram</span>
</svelte:fragment>
</FileDropzone> </FileDropzone>
<!-- Save/Delete buttons --> <!-- Save/Delete buttons -->
<div class="flex justify-end gap-2"> <div class="flex justify-end gap-2">
<Button onclick={clear_sprint} color="secondary" disabled={disable_inputs} width="w-1/3"> <Button
onclick={clear_sprint}
color="secondary"
{disabled}
width={race ? "w-1/3" : "w-1/2"}
>
Remove Sprint Remove Sprint
</Button> </Button>
{#if race} {#if race}
<Button <Button formaction="?/update_race" color="secondary" {disabled} submit width="w-1/3">
formaction="?/update_race"
color="secondary"
disabled={disable_inputs}
submit
width="w-1/3"
onclick={() => modalStore.close()}
>
Save Changes Save Changes
</Button> </Button>
<Button <Button formaction="?/delete_race" color="primary" {disabled} submit width="w-1/3">
color="primary"
submit
disabled={disable_inputs}
formaction="?/delete_race"
width="w-1/3"
onclick={() => modalStore.close()}
>
Delete Delete
</Button> </Button>
{:else} {:else}
<Button <Button formaction="?/create_race" color="tertiary" {disabled} submit width="w-1/2">
formaction="?/create_race"
color="tertiary"
submit
width="w-1/2"
disabled={disable_inputs}
onclick={() => modalStore.close()}
>
Create Race Create Race
</Button> </Button>
{/if} {/if}
@ -217,3 +195,4 @@
</div> </div>
</form> </form>
</Card> </Card>
{/await}

View File

@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import { Card, Button, Dropdown } from "$lib/components"; import { Card, Button, Dropdown } from "$lib/components";
import type { Driver, Race, RacePick, User } from "$lib/schema"; import type { Driver, Race, RacePick, Substitution } from "$lib/schema";
import { get_by_value } from "$lib/database"; import { get_by_value, get_driver_headshot_template } from "$lib/database";
import type { Action } from "svelte/action"; import type { Action } from "svelte/action";
import { getModalStore, type ModalStore } from "@skeletonlabs/skeleton"; import { getModalStore, type ModalStore } from "@skeletonlabs/skeleton";
import { DRIVER_HEADSHOT_HEIGHT, DRIVER_HEADSHOT_WIDTH } from "$lib/config"; import { DRIVER_HEADSHOT_HEIGHT, DRIVER_HEADSHOT_WIDTH } from "$lib/config";
@ -9,122 +9,125 @@
import { enhance } from "$app/forms"; import { enhance } from "$app/forms";
interface RacePickCardProps { interface RacePickCardProps {
/** Data passed from the page context */
data: any;
/** The [RacePick] object used to prefill values. */ /** The [RacePick] object used to prefill values. */
racepick?: RacePick | undefined; racepick?: RacePick;
/** The [Race] object containing the place to guess */
currentrace: Race | null;
/** The [User] currently logged in */
user?: User;
/** The drivers (to display the headshot) */
drivers: Driver[];
/** Disable all inputs if [true] */
disable_inputs?: boolean;
/** The [src] of the driver headshot template preview */
headshot_template?: string;
/** The value this component's pxx select dropdown will bind to */
pxx_select_value: string;
/** The value this component's dnf select dropdown will bind to */
dnf_select_value: string;
} }
let { let { data, racepick = undefined }: RacePickCardProps = $props();
racepick = undefined,
currentrace = null,
user = undefined,
drivers,
disable_inputs = false,
headshot_template = "",
pxx_select_value,
dnf_select_value,
}: RacePickCardProps = $props();
const modalStore: ModalStore = getModalStore(); const modalStore: ModalStore = getModalStore();
if ($modalStore[0].meta) { if ($modalStore[0].meta) {
const meta = $modalStore[0].meta; const meta = $modalStore[0].meta;
// Stuff thats required for the "update" card data = meta.data;
racepick = meta.racepick; racepick = meta.racepick;
currentrace = meta.currentrace;
user = meta.user;
drivers = meta.drivers;
disable_inputs = meta.disable_inputs;
headshot_template = meta.headshot_template;
pxx_select_value = meta.pxx_select_value;
dnf_select_value = meta.dnf_select_value;
} }
// This action is used on the <Dropdown> element. // This is executed on mount of the element specifying the "action"
// It will trigger once the Dropdown's <input> elements is mounted.
// This way we'll receive a reference to the object so we can register our event handler.
const register_pxx_preview_handler: Action = (node: HTMLElement) => { const register_pxx_preview_handler: Action = (node: HTMLElement) => {
node.addEventListener("DropdownChange", update_pxx_preview); node.addEventListener("DropdownChange", update_pxx_preview);
}; };
// This event handler is registered to the Dropdown's <input> element through the action above. // This event handler is registered to the Dropdown's <input> element through the action above.
const update_pxx_preview = (event: Event) => { const update_pxx_preview = async (event: Event) => {
const target: HTMLInputElement = event.target as HTMLInputElement; const target: HTMLInputElement = event.target as HTMLInputElement;
// The option "label" gets put into the Dropdown's input value, const src: string =
// so we need to lookup the driver by "code". get_by_value<Driver>(await data.drivers, "code", target.value)?.headshot_url || "";
const src: string = get_by_value(drivers, "code", target.value)?.headshot_url || ""; const img = document.getElementById("headshot_preview") as HTMLImageElement;
if (src) {
const preview: HTMLImageElement = document.getElementById(
`update_substitution_headshot_preview_${racepick?.id ?? "create"}`,
) as HTMLImageElement;
if (preview) preview.src = src; // Can be null if lazyimg not loaded
} if (img) img.src = src;
}; };
const required: boolean = $derived(!racepick);
const disabled: boolean = $derived(!data.admin);
const labelwidth: string = "60px";
const active_drivers_and_substitutes: Promise<Driver[]> = $derived.by(async () => {
if (!data.currentrace) return [];
let drivers: Driver[] = await data.drivers;
let substitutions: Substitution[] = await data.substitutions;
let active_and_substitutes: Driver[] = drivers.filter((driver: Driver) => driver.active);
substitutions
.filter((substitution: Substitution) => substitution.race === data.currentrace?.id)
.forEach((substitution: Substitution) => {
const for_index = active_and_substitutes.findIndex(
(driver: Driver) => driver.id === substitution.for,
);
const sub_index = drivers.findIndex(
(driver: Driver) => driver.id === substitution.substitute,
);
active_and_substitutes[for_index] = drivers[sub_index];
});
return active_and_substitutes.sort((a: Driver, b: Driver) => a.code.localeCompare(b.code));
});
let pxx_select_value: string = $state(racepick?.pxx ?? "");
let dnf_select_value: string = $state(racepick?.dnf ?? "");
</script> </script>
{#await data.graphics then graphics}
{#await data.drivers then drivers}
<Card <Card
imgsrc={get_by_value(drivers, "id", racepick?.pxx ?? "")?.headshot_url ?? headshot_template} imgsrc={get_by_value<Driver>(drivers, "id", racepick?.pxx ?? "")?.headshot_url ??
imgid="update_substitution_headshot_preview_{racepick?.id ?? 'create'}" get_driver_headshot_template(graphics)}
imgid="headshot_preview"
width="w-full sm:w-auto" width="w-full sm:w-auto"
imgwidth={DRIVER_HEADSHOT_WIDTH} imgwidth={DRIVER_HEADSHOT_WIDTH}
imgheight={DRIVER_HEADSHOT_HEIGHT} imgheight={DRIVER_HEADSHOT_HEIGHT}
imgonclick={(event: Event) => modalStore.close()} imgonclick={(event: Event) => modalStore.close()}
> >
<form method="POST" enctype="multipart/form-data" use:enhance> <form
method="POST"
enctype="multipart/form-data"
use:enhance
onsubmit={() => modalStore.close()}
>
<!-- This is also disabled, because the ID should only be --> <!-- This is also disabled, because the ID should only be -->
<!-- "leaked" to users that are allowed to use the inputs --> <!-- "leaked" to users that are allowed to use the inputs -->
{#if racepick && !disable_inputs} {#if racepick && !disabled}
<input name="id" type="hidden" value={racepick.id} /> <input name="id" type="hidden" value={racepick.id} />
{/if} {/if}
<input name="user" type="hidden" value={user?.id} /> <input name="user" type="hidden" value={data.user?.id} />
<input name="race" type="hidden" value={currentrace?.id} /> <input name="race" type="hidden" value={data.currentrace?.id} />
<div class="flex flex-col gap-2"> <div class="flex flex-col gap-2">
<!-- PXX select --> <!-- PXX select -->
{#await active_drivers_and_substitutes then pxx_drivers}
<Dropdown <Dropdown
name="pxx" name="pxx"
input_variable={pxx_select_value} input_variable={pxx_select_value}
action={register_pxx_preview_handler} action={register_pxx_preview_handler}
options={driver_dropdown_options(drivers)} options={driver_dropdown_options(pxx_drivers)}
labelwidth="60px" {labelwidth}
disabled={disable_inputs} {disabled}
> >
P{currentrace?.pxx ?? "XX"} P{data.currentrace?.pxx ?? "XX"}
</Dropdown> </Dropdown>
{/await}
<!-- DNF select --> <!-- DNF select -->
{#await active_drivers_and_substitutes then pxx_drivers}
<Dropdown <Dropdown
name="dnf" name="dnf"
input_variable={dnf_select_value} input_variable={dnf_select_value}
options={driver_dropdown_options(drivers)} options={driver_dropdown_options(pxx_drivers)}
labelwidth="60px" {labelwidth}
disabled={disable_inputs} {disabled}
> >
DNF DNF
</Dropdown> </Dropdown>
{/await}
<!-- Save/Delete buttons --> <!-- Save/Delete buttons -->
<div class="flex justify-end gap-2"> <div class="flex justify-end gap-2">
@ -132,20 +135,18 @@
<Button <Button
formaction="?/update_racepick" formaction="?/update_racepick"
color="secondary" color="secondary"
disabled={disable_inputs} {disabled}
submit submit
width="w-1/2" width="w-1/2"
onclick={() => modalStore.close()}
> >
Save Changes Save Changes
</Button> </Button>
<Button <Button
color="primary"
submit
disabled={disable_inputs}
formaction="?/delete_racepick" formaction="?/delete_racepick"
color="primary"
{disabled}
submit
width="w-1/2" width="w-1/2"
onclick={() => modalStore.close()}
> >
Delete Delete
</Button> </Button>
@ -153,9 +154,9 @@
<Button <Button
formaction="?/create_racepick" formaction="?/create_racepick"
color="tertiary" color="tertiary"
{disabled}
submit submit
width="w-full" width="w-full"
onclick={() => modalStore.close()}
> >
Make Pick Make Pick
</Button> </Button>
@ -164,3 +165,5 @@
</div> </div>
</form> </form>
</Card> </Card>
{/await}
{/await}

View File

@ -1,114 +1,82 @@
<script lang="ts"> <script lang="ts">
import { Card, Button, Dropdown } from "$lib/components"; import { Card, Button, Dropdown } from "$lib/components";
import type { Driver, Race, Substitution } from "$lib/schema"; import type { Driver, Race, Substitution } from "$lib/schema";
import { get_by_value } from "$lib/database"; import { get_by_value, get_driver_headshot_template } from "$lib/database";
import type { Action } from "svelte/action"; import type { Action } from "svelte/action";
import { getModalStore, type ModalStore } from "@skeletonlabs/skeleton"; import { getModalStore, type ModalStore } from "@skeletonlabs/skeleton";
import { DRIVER_HEADSHOT_HEIGHT, DRIVER_HEADSHOT_WIDTH } from "$lib/config"; import { DRIVER_HEADSHOT_HEIGHT, DRIVER_HEADSHOT_WIDTH } from "$lib/config";
import { driver_dropdown_options, race_dropdown_options } from "$lib/dropdown"; import { driver_dropdown_options, race_dropdown_options } from "$lib/dropdown";
import { enhance } from "$app/forms"; import { enhance } from "$app/forms";
import { sub } from "date-fns";
interface SubstitutionCardProps { interface SubstitutionCardProps {
/** Data passed from the page context */
data: any;
/** The [Substitution] object used to prefill values. */ /** The [Substitution] object used to prefill values. */
substitution?: Substitution | undefined; substitution?: Substitution;
/** The drivers (to display the headshot) */
drivers: Driver[];
races: Race[];
/** Disable all inputs if [true] */
disable_inputs?: boolean;
/** Require all inputs if [true] */
require_inputs?: boolean;
/** The [src] of the driver headshot template preview */
headshot_template?: string;
/** The value this component's substitute select dropdown will bind to */
substitute_select_value: string;
/** The value this component's driver select dropdown will bind to */
driver_select_value: string;
/** The value this component's race select dropdown will bind to */
race_select_value: string;
} }
let { let { data, substitution = undefined }: SubstitutionCardProps = $props();
substitution = undefined,
drivers,
races,
disable_inputs = false,
require_inputs = false,
headshot_template = "",
substitute_select_value,
driver_select_value,
race_select_value,
}: SubstitutionCardProps = $props();
const active_drivers = (drivers: Driver[]): Driver[] =>
drivers.filter((driver: Driver) => driver.active);
const inactive_drivers = (drivers: Driver[]): Driver[] =>
drivers.filter((driver: Driver) => !driver.active);
const modalStore: ModalStore = getModalStore(); const modalStore: ModalStore = getModalStore();
if ($modalStore[0].meta) { if ($modalStore[0].meta) {
const meta = $modalStore[0].meta; const meta = $modalStore[0].meta;
// Stuff thats required for the "update" card data = meta.data;
substitution = meta.substitution; substitution = meta.substitution;
drivers = meta.drivers;
races = meta.races;
disable_inputs = meta.disable_inputs;
substitute_select_value = meta.substitute_select_value;
driver_select_value = meta.driver_select_value;
race_select_value = meta.race_select_value;
// Stuff thats additionally required for the "create" card
require_inputs = meta.require_inputs;
headshot_template = meta.headshot_template;
} }
// This action is used on the <Dropdown> element. // This is executed on mount of the element specifying the "action"
// It will trigger once the Dropdown's <input> elements is mounted. const register_substitute_preview_handler: Action = (node: HTMLElement) =>
// This way we'll receive a reference to the object so we can register our event handler.
const register_substitute_preview_handler: Action = (node: HTMLElement) => {
node.addEventListener("DropdownChange", update_substitute_preview); node.addEventListener("DropdownChange", update_substitute_preview);
};
// This event handler is registered to the Dropdown's <input> element through the action above. // This event handler is registered to the Dropdown's <input> element through the action above.
const update_substitute_preview = (event: Event) => { const update_substitute_preview = async (event: Event) => {
const target: HTMLInputElement = event.target as HTMLInputElement; const target: HTMLInputElement = event.target as HTMLInputElement;
// The option "label" gets put into the Dropdown's input value, const src: string =
// so we need to lookup the driver by "code". get_by_value<Driver>(await data.drivers, "code", target.value)?.headshot_url ?? "";
const src: string = get_by_value(drivers, "code", target.value)?.headshot_url || ""; const img = document.getElementById("headshot_preview") as HTMLImageElement;
if (src) {
const preview: HTMLImageElement = document.getElementById(
`update_substitution_headshot_preview_${substitution?.id ?? "create"}`,
) as HTMLImageElement;
if (preview) preview.src = src; // Can be null if lazyimage hasn't loaded
} if (img) img.src = src;
}; };
const active_drivers = (drivers: Driver[]): Driver[] =>
drivers.filter((driver: Driver) => driver.active);
const inactive_drivers = (drivers: Driver[]): Driver[] =>
drivers.filter((driver: Driver) => !driver.active);
const required: boolean = $derived(!substitution);
const disabled: boolean = $derived(!data.admin);
const labelwidth: string = "120px";
let substitute_select_value: string = $state(substitution?.substitute ?? "");
let driver_select_value: string = $state(substitution?.for ?? "");
let race_select_value: string = $state(substitution?.race ?? "");
</script> </script>
{#await data.graphics then graphics}
{#await data.drivers then drivers}
<Card <Card
imgsrc={get_by_value(drivers, "id", substitution?.substitute ?? "")?.headshot_url ?? imgsrc={get_by_value<Driver>(drivers, "id", substitution?.substitute ?? "")?.headshot_url ??
headshot_template} get_driver_headshot_template(graphics)}
imgid="update_substitution_headshot_preview_{substitution?.id ?? 'create'}" imgid="headshot_preview"
width="w-full sm:w-auto" width="w-full sm:w-auto"
imgwidth={DRIVER_HEADSHOT_WIDTH} imgwidth={DRIVER_HEADSHOT_WIDTH}
imgheight={DRIVER_HEADSHOT_HEIGHT} imgheight={DRIVER_HEADSHOT_HEIGHT}
imgonclick={(event: Event) => modalStore.close()} imgonclick={(event: Event) => modalStore.close()}
> >
<form method="POST" enctype="multipart/form-data" use:enhance> <form
method="POST"
enctype="multipart/form-data"
use:enhance
onsubmit={() => modalStore.close()}
>
<!-- This is also disabled, because the ID should only be --> <!-- This is also disabled, because the ID should only be -->
<!-- "leaked" to users that are allowed to use the inputs --> <!-- "leaked" to users that are allowed to use the inputs -->
{#if substitution && !disable_inputs} {#if substitution && !disabled}
<input name="id" type="hidden" value={substitution.id} /> <input name="id" type="hidden" value={substitution.id} />
{/if} {/if}
@ -119,9 +87,9 @@
input_variable={substitute_select_value} input_variable={substitute_select_value}
action={register_substitute_preview_handler} action={register_substitute_preview_handler}
options={driver_dropdown_options(inactive_drivers(drivers))} options={driver_dropdown_options(inactive_drivers(drivers))}
labelwidth="120px" {labelwidth}
disabled={disable_inputs} {disabled}
required={require_inputs} {required}
> >
Substitute Substitute
</Dropdown> </Dropdown>
@ -131,24 +99,26 @@
name="for" name="for"
input_variable={driver_select_value} input_variable={driver_select_value}
options={driver_dropdown_options(active_drivers(drivers))} options={driver_dropdown_options(active_drivers(drivers))}
labelwidth="120px" {labelwidth}
disabled={disable_inputs} {disabled}
required={require_inputs} {required}
> >
For For
</Dropdown> </Dropdown>
<!-- Race select --> <!-- Race select -->
{#await data.races then races}
<Dropdown <Dropdown
name="race" name="race"
input_variable={race_select_value} input_variable={race_select_value}
options={race_dropdown_options(races)} options={race_dropdown_options(races)}
labelwidth="120px" {labelwidth}
disabled={disable_inputs} {disabled}
required={require_inputs} {required}
> >
Race Race
</Dropdown> </Dropdown>
{/await}
<!-- Save/Delete buttons --> <!-- Save/Delete buttons -->
<div class="flex justify-end gap-2"> <div class="flex justify-end gap-2">
@ -156,20 +126,18 @@
<Button <Button
formaction="?/update_substitution" formaction="?/update_substitution"
color="secondary" color="secondary"
disabled={disable_inputs} {disabled}
submit submit
width="w-1/2" width="w-1/2"
onclick={() => modalStore.close()}
> >
Save Changes Save Changes
</Button> </Button>
<Button <Button
color="primary"
submit
disabled={disable_inputs}
formaction="?/delete_substitution" formaction="?/delete_substitution"
color="primary"
{disabled}
submit
width="w-1/2" width="w-1/2"
onclick={() => modalStore.close()}
> >
Delete Delete
</Button> </Button>
@ -177,10 +145,9 @@
<Button <Button
formaction="?/create_substitution" formaction="?/create_substitution"
color="tertiary" color="tertiary"
{disabled}
submit submit
width="w-full" width="w-full"
disabled={disable_inputs}
onclick={() => modalStore.close()}
> >
Create Substitution Create Substitution
</Button> </Button>
@ -189,3 +156,5 @@
</div> </div>
</form> </form>
</Card> </Card>
{/await}
{/await}

View File

@ -5,95 +5,81 @@
import type { Team } from "$lib/schema"; import type { Team } from "$lib/schema";
import { TEAM_BANNER_HEIGHT, TEAM_BANNER_WIDTH } from "$lib/config"; import { TEAM_BANNER_HEIGHT, TEAM_BANNER_WIDTH } from "$lib/config";
import { enhance } from "$app/forms"; import { enhance } from "$app/forms";
import { get_team_banner_template, get_team_logo_template } from "$lib/database";
interface TeamCardProps { interface TeamCardProps {
/** Data from the page context */
data: any;
/** The [Team] object used to prefill values. */ /** The [Team] object used to prefill values. */
team?: Team | undefined; team?: Team;
/** Disable all inputs if [true] */
disable_inputs?: boolean;
/** Require all inputs if [true] */
require_inputs?: boolean;
/** The [src] of the team banner template preview */
banner_template?: string;
/** The [src] of the team logo template preview */
logo_template?: string;
} }
let { let { data, team = undefined }: TeamCardProps = $props();
team = undefined,
disable_inputs = false,
require_inputs = false,
banner_template = "",
logo_template = "",
}: TeamCardProps = $props();
const modalStore: ModalStore = getModalStore(); const modalStore: ModalStore = getModalStore();
if ($modalStore[0].meta) { if ($modalStore[0].meta) {
const meta = $modalStore[0].meta; const meta = $modalStore[0].meta;
// Stuff thats required for the "update" card data = meta.data;
team = meta.team; team = meta.team;
disable_inputs = meta.disable_inputs;
// Stuff thats additionally required for the "create" card
require_inputs = meta.require_inputs;
banner_template = meta.banner_template;
logo_template = meta.logo_template;
} }
const required: boolean = $derived(!team);
const disabled: boolean = $derived(!data.admin);
const labelwidth: string = "110px"; const labelwidth: string = "110px";
let colorpreview: string = $state(team?.color ?? "white"); let colorpreview: string = $state(team?.color ?? "Invalid");
</script> </script>
{#await data.graphics then graphics}
<Card <Card
imgsrc={team?.banner_url ?? banner_template} imgsrc={team?.banner_url ?? get_team_banner_template(graphics)}
imgid="update_team_banner_preview_{team?.id ?? 'create'}" imgid="banner_preview"
width="w-full sm:w-auto" width="w-full sm:w-auto"
imgwidth={TEAM_BANNER_WIDTH} imgwidth={TEAM_BANNER_WIDTH}
imgheight={TEAM_BANNER_HEIGHT} imgheight={TEAM_BANNER_HEIGHT}
imgonclick={(event: Event) => modalStore.close()} imgonclick={(event: Event) => modalStore.close()}
> >
<form method="POST" enctype="multipart/form-data" use:enhance> <form
method="POST"
enctype="multipart/form-data"
use:enhance
onsubmit={() => modalStore.close()}
>
<!-- This is also disabled, because the ID should only be --> <!-- This is also disabled, because the ID should only be -->
<!-- "leaked" to users that are allowed to use the inputs --> <!-- "leaked" to users that are allowed to use the inputs -->
{#if team && !disable_inputs} {#if team && !disabled}
<input name="id" type="hidden" value={team.id} /> <input name="id" type="hidden" value={team.id} />
{/if} {/if}
<div class="flex flex-col gap-2"> <div class="flex flex-col gap-2">
<!-- Team name input --> <!-- Team name input -->
<Input <Input
id="team_name_{team?.id ?? 'create'}"
name="name" name="name"
value={team?.name ?? ""} value={team?.name ?? ""}
{labelwidth}
autocomplete="off" autocomplete="off"
disabled={disable_inputs} {labelwidth}
required={require_inputs} {disabled}
{required}
> >
Name Name
</Input> </Input>
<!-- Team color input --> <!-- Team color input -->
<Input <Input
id="team_color_{team?.id ?? 'create'}"
name="color" name="color"
value={team?.color ?? ""} value={team?.color ?? ""}
{labelwidth}
autocomplete="off" autocomplete="off"
placeholder="Enter as '#XXXXXX'" placeholder="Enter as '#XXXXXX'"
disabled={disable_inputs}
required={require_inputs}
minlength={7} minlength={7}
maxlength={7} maxlength={7}
onchange={(event: Event) => { oninput={(event: Event) => {
colorpreview = (event.target as HTMLInputElement).value; colorpreview = (event.target as HTMLInputElement).value;
}} }}
{labelwidth}
{disabled}
{required}
> >
Color Color
<span class="badge ml-2 border" style="color: {colorpreview}; background: {colorpreview}"> <span class="badge ml-2 border" style="color: {colorpreview}; background: {colorpreview}">
@ -104,34 +90,28 @@
<!-- Banner upload --> <!-- Banner upload -->
<FileDropzone <FileDropzone
name="banner" name="banner"
id="team_banner_{team?.id ?? 'create'}" onchange={get_image_preview_event_handler("banner_preview")}
onchange={get_image_preview_event_handler( {disabled}
`update_team_banner_preview_${team?.id ?? "create"}`, {required}
)}
disabled={disable_inputs}
required={require_inputs}
>
<svelte:fragment slot="message"
><span class="font-bold">Upload Banner</span></svelte:fragment
> >
<svelte:fragment slot="message">
<span class="font-bold">Upload Banner</span>
</svelte:fragment>
</FileDropzone> </FileDropzone>
<!-- Logo upload --> <!-- Logo upload -->
<FileDropzone <FileDropzone
name="logo" name="logo"
id="team_logo_{team?.id ?? 'create'}" onchange={get_image_preview_event_handler("logo_preview")}
onchange={get_image_preview_event_handler( {disabled}
`update_team_logo_preview_${team?.id ?? "create"}`, {required}
)}
disabled={disable_inputs}
required={require_inputs}
> >
<svelte:fragment slot="message"> <svelte:fragment slot="message">
<div class="inline-flex flex-nowrap items-center gap-2"> <div class="inline-flex flex-nowrap items-center gap-2">
<span class="font-bold">Upload Logo</span> <span class="font-bold">Upload Logo</span>
<LazyImage <LazyImage
src={team?.logo_url ?? logo_template} src={team?.logo_url ?? get_team_logo_template(graphics)}
id="update_team_logo_preview_{team?.id ?? 'create'}" id="logo_preview"
imgwidth={32} imgwidth={32}
imgheight={32} imgheight={32}
/> />
@ -142,35 +122,14 @@
<!-- Save/Delete buttons --> <!-- Save/Delete buttons -->
<div class="flex justify-end gap-2"> <div class="flex justify-end gap-2">
{#if team} {#if team}
<Button <Button formaction="?/update_team" color="secondary" {disabled} submit width="w-1/2">
formaction="?/update_team"
color="secondary"
disabled={disable_inputs}
submit
width="w-1/2"
onclick={() => modalStore.close()}
>
Save Save
</Button> </Button>
<Button <Button formaction="?/delete_team" color="primary" {disabled} submit width="w-1/2">
color="primary"
submit
disabled={disable_inputs}
formaction="?/delete_team"
width="w-1/2"
onclick={() => modalStore.close()}
>
Delete Delete
</Button> </Button>
{:else} {:else}
<Button <Button formaction="?/create_team" color="tertiary" {disabled} submit width="w-full">
formaction="?/create_team"
color="tertiary"
submit
width="w-full"
disabled={disable_inputs}
onclick={() => modalStore.close()}
>
Create Team Create Team
</Button> </Button>
{/if} {/if}
@ -178,3 +137,4 @@
</div> </div>
</form> </form>
</Card> </Card>
{/await}

View File

@ -9,16 +9,11 @@ import {
TEAM_BANNER_WIDTH, TEAM_BANNER_WIDTH,
} from "$lib/config"; } from "$lib/config";
let team_dropdown_opts: DropdownOption[] | null = null;
/** /**
* Generates a list of [DropdownOptions] for a <Dropdown> component. * Generates a list of [DropdownOptions] for a <Dropdown> component.
* Cached until page reload.
*/ */
export const team_dropdown_options = (teams: Team[]): DropdownOption[] => { export const team_dropdown_options = (teams: Team[]): DropdownOption[] =>
if (!team_dropdown_opts) { teams.map((team: Team) => {
console.log("team_dropdown_options");
team_dropdown_opts = teams.map((team: Team) => {
return { return {
label: team.name, label: team.name,
value: team.id, value: team.id,
@ -27,17 +22,12 @@ export const team_dropdown_options = (teams: Team[]): DropdownOption[] => {
icon_height: TEAM_BANNER_HEIGHT, icon_height: TEAM_BANNER_HEIGHT,
}; };
}); });
}
return team_dropdown_opts;
};
/** /**
* Generates a list of [DropdownOptions] for a <Dropdown> component. * Generates a list of [DropdownOptions] for a <Dropdown> component.
* Not cached, because drivers are often filtered by active/inactive.
*/ */
export const driver_dropdown_options = (drivers: Driver[]): DropdownOption[] => { export const driver_dropdown_options = (drivers: Driver[]): DropdownOption[] =>
return drivers.map((driver: Driver) => { drivers.map((driver: Driver) => {
return { return {
label: driver.code, label: driver.code,
value: driver.id, value: driver.id,
@ -46,18 +36,12 @@ export const driver_dropdown_options = (drivers: Driver[]): DropdownOption[] =>
icon_height: DRIVER_HEADSHOT_HEIGHT, icon_height: DRIVER_HEADSHOT_HEIGHT,
}; };
}); });
};
let race_dropdown_opts: DropdownOption[] | null = null;
/** /**
* Generates a list of [DropdownOptions] for a <Dropdown> component. * Generates a list of [DropdownOptions] for a <Dropdown> component.
* Cached until page reload.
*/ */
export const race_dropdown_options = (races: Race[]): DropdownOption[] => { export const race_dropdown_options = (races: Race[]): DropdownOption[] =>
if (!race_dropdown_opts) { races.map((race: Race) => {
console.log("race_dropdown_options");
race_dropdown_opts = races.map((race: Race) => {
return { return {
label: race.name, label: race.name,
value: race.id, value: race.id,
@ -66,7 +50,3 @@ export const race_dropdown_options = (races: Race[]): DropdownOption[] => {
icon_height: RACE_PICTOGRAM_HEIGHT, icon_height: RACE_PICTOGRAM_HEIGHT,
}; };
}); });
}
return race_dropdown_opts;
};

View File

@ -1,24 +1,31 @@
<script lang="ts"> <script lang="ts">
import { Button, type TableColumn, Table } from "$lib/components"; import { Button, type TableColumn, Table } from "$lib/components";
import { get_by_value, get_driver_headshot_template } from "$lib/database"; import { get_by_value } from "$lib/database";
import type { Driver, Team } from "$lib/schema"; import type { Driver, Team } from "$lib/schema";
import { getModalStore, type ModalSettings, type ModalStore } from "@skeletonlabs/skeleton"; import { getModalStore, type ModalSettings, type ModalStore } from "@skeletonlabs/skeleton";
import type { PageData } from "./$types"; import type { PageData } from "./$types";
let { data }: { data: PageData } = $props(); let { data }: { data: PageData } = $props();
const update_driver_team_select_values: { [key: string]: string } = $state({}); // <driver.id, team.id> const modalStore: ModalStore = getModalStore();
const update_driver_active_values: { [key: string]: boolean } = $state({}); const driver_handler = async (event: Event, id?: string) => {
data.drivers.then((drivers: Driver[]) => const driver: Driver | undefined = get_by_value(await data.drivers, "id", id ?? "Invalid");
drivers.forEach((driver: Driver) => {
update_driver_team_select_values[driver.id] = driver.team;
update_driver_active_values[driver.id] = driver.active;
}),
);
update_driver_team_select_values["create"] = "";
update_driver_active_values["create"] = true;
const drivers_columns: TableColumn[] = [ if (id && !driver) return;
const modalSettings: ModalSettings = {
type: "component",
component: "driverCard",
meta: {
data,
driver,
},
};
modalStore.trigger(modalSettings);
};
const drivers_columns: TableColumn[] = $derived([
{ {
data_value_name: "code", data_value_name: "code",
label: "Driver Code", label: "Driver Code",
@ -32,9 +39,7 @@
label: "Team", label: "Team",
valuefun: async (value: string): Promise<string> => { valuefun: async (value: string): Promise<string> => {
const team: Team | undefined = get_by_value(await data.teams, "id", value); const team: Team | undefined = get_by_value(await data.teams, "id", value);
return team return `<span class='badge border mr-2' style='color: ${team?.color ?? "#FFFFFF"}; background: ${team?.color ?? "#FFFFFF"};'>C</span>${team?.name ?? "Invalid"}`;
? `<span class='badge border mr-2' style='color: ${team.color}; background: ${team.color};'>C</span>${team.name}`
: "<span class='badge variant-filled-primary'>Invalid</span>";
}, },
}, },
{ {
@ -43,53 +48,14 @@
valuefun: async (value: boolean): Promise<string> => valuefun: async (value: boolean): Promise<string> =>
`<span class='badge variant-filled-${value ? "tertiary" : "primary"} text-center' style='width: 36px;'>${value ? "Yes" : "No"}</span>`, `<span class='badge variant-filled-${value ? "tertiary" : "primary"} text-center' style='width: 36px;'>${value ? "Yes" : "No"}</span>`,
}, },
]; ]);
const modalStore: ModalStore = getModalStore();
/** Shows the DriverCard modal to edit the clicked driver */
const drivers_handler = async (event: Event, id: string) => {
const driver: Driver | undefined = get_by_value(await data.drivers, "id", id);
if (!driver) return;
const modalSettings: ModalSettings = {
type: "component",
component: "driverCard",
meta: {
driver: driver,
teams: await data.teams,
team_select_value: update_driver_team_select_values[driver.id],
active_value: update_driver_active_values[driver.id],
disable_inputs: !data.admin,
},
};
modalStore.trigger(modalSettings);
};
const create_driver_handler = async (event: Event) => {
const modalSettings: ModalSettings = {
type: "component",
component: "driverCard",
meta: {
teams: await data.teams,
team_select_value: update_driver_team_select_values["create"],
active_value: update_driver_active_values["create"],
disable_inputs: !data.admin,
require_inputs: true,
headshot_template: get_driver_headshot_template(await data.graphics),
},
};
modalStore.trigger(modalSettings);
};
</script> </script>
<div class="pb-2"> <div class="pb-2">
<Button width="w-full" color="tertiary" onclick={create_driver_handler} shadow> <Button width="w-full" color="tertiary" onclick={driver_handler} shadow>
<span class="font-bold">Create New Driver</span> <span class="font-bold">Create New Driver</span>
</Button> </Button>
</div> </div>
{#await data.drivers then drivers} {#await data.drivers then drivers}
<Table data={drivers} columns={drivers_columns} handler={drivers_handler} /> <Table data={drivers} columns={drivers_columns} handler={driver_handler} />
{/await} {/await}

View File

@ -2,12 +2,31 @@
import { Button, Table, type TableColumn } from "$lib/components"; import { Button, Table, type TableColumn } from "$lib/components";
import { getModalStore, type ModalSettings, type ModalStore } from "@skeletonlabs/skeleton"; import { getModalStore, type ModalSettings, type ModalStore } from "@skeletonlabs/skeleton";
import type { PageData } from "./$types"; import type { PageData } from "./$types";
import { get_by_value, get_race_pictogram_template } from "$lib/database"; import { get_by_value } from "$lib/database";
import type { Race } from "$lib/schema"; import type { Race } from "$lib/schema";
let { data }: { data: PageData } = $props(); let { data }: { data: PageData } = $props();
const races_columns: TableColumn[] = [ const modalStore: ModalStore = getModalStore();
const race_handler = async (event: Event, id?: string) => {
const race: Race | undefined = get_by_value(await data.races, "id", id ?? "Invalid");
if (id && !race) return;
const modalSettings: ModalSettings = {
type: "component",
component: "raceCard",
meta: {
data,
race,
},
};
modalStore.trigger(modalSettings);
};
const races_columns: TableColumn[] = $derived([
{ {
data_value_name: "name", data_value_name: "name",
label: "Name", label: "Name",
@ -35,46 +54,14 @@
label: "Race", label: "Race",
valuefun: async (value: string): Promise<string> => value.slice(0, -5), valuefun: async (value: string): Promise<string> => value.slice(0, -5),
}, },
]; ]);
const modalStore: ModalStore = getModalStore();
const races_handler = async (event: Event, id: string) => {
const race: Race | undefined = get_by_value(await data.races, "id", id);
if (!race) return;
const modalSettings: ModalSettings = {
type: "component",
component: "raceCard",
meta: {
race: race,
disable_inputs: !data.admin,
},
};
modalStore.trigger(modalSettings);
};
const create_race_handler = async (event: Event) => {
const modalSettings: ModalSettings = {
type: "component",
component: "raceCard",
meta: {
disable_inputs: !data.admin,
require_inputs: true,
pictogram_template: get_race_pictogram_template(await data.graphics),
},
};
modalStore.trigger(modalSettings);
};
</script> </script>
<div class="pb-2"> <div class="pb-2">
<Button width="w-full" color="tertiary" onclick={create_race_handler} shadow> <Button width="w-full" color="tertiary" onclick={race_handler} shadow>
<span class="font-bold">Create New Race</span> <span class="font-bold">Create New Race</span>
</Button> </Button>
</div> </div>
{#await data.races then races} {#await data.races then races}
<Table data={races} columns={races_columns} handler={races_handler} /> <Table data={races} columns={races_columns} handler={race_handler} />
{/await} {/await}

View File

@ -1,28 +1,35 @@
<script lang="ts"> <script lang="ts">
import { get_by_value, get_driver_headshot_template } from "$lib/database"; import { get_by_value } from "$lib/database";
import { getModalStore, type ModalSettings, type ModalStore } from "@skeletonlabs/skeleton"; import { getModalStore, type ModalSettings, type ModalStore } from "@skeletonlabs/skeleton";
import type { PageData } from "./$types"; import type { PageData } from "./$types";
import type { Driver, Race, Substitution } from "$lib/schema"; import type { Race, Substitution } from "$lib/schema";
import { Button, Table, type TableColumn } from "$lib/components"; import { Button, Table, type TableColumn } from "$lib/components";
let { data }: { data: PageData } = $props(); let { data }: { data: PageData } = $props();
// TODO: Cleanup const modalStore: ModalStore = getModalStore();
const update_substitution_substitute_select_values: { [key: string]: string } = $state({}); const substitution_handler = async (event: Event, id?: string) => {
const update_substitution_for_select_values: { [key: string]: string } = $state({}); const substitution: Substitution | undefined = get_by_value(
const update_substitution_race_select_values: { [key: string]: string } = $state({}); await data.substitutions,
data.substitutions.then((substitutions: Substitution[]) => "id",
substitutions.forEach((substitution: Substitution) => { id ?? "Invalid",
update_substitution_substitute_select_values[substitution.id] = substitution.substitute;
update_substitution_for_select_values[substitution.id] = substitution.for;
update_substitution_race_select_values[substitution.id] = substitution.race;
}),
); );
update_substitution_substitute_select_values["create"] = "";
update_substitution_for_select_values["create"] = "";
update_substitution_race_select_values["create"] = "";
const substitutions_columns: TableColumn[] = [ if (id && !substitution) return;
const modalSettings: ModalSettings = {
type: "component",
component: "substitutionCard",
meta: {
data,
substitution,
},
};
modalStore.trigger(modalSettings);
};
const substitutions_columns: TableColumn[] = $derived([
{ {
data_value_name: "expand", data_value_name: "expand",
label: "Step", label: "Step",
@ -49,56 +56,14 @@
valuefun: async (value: string): Promise<string> => valuefun: async (value: string): Promise<string> =>
get_by_value(await data.races, "id", value)?.name ?? "Invalid", get_by_value(await data.races, "id", value)?.name ?? "Invalid",
}, },
]; ]);
const modalStore: ModalStore = getModalStore();
const substitutions_handler = async (event: Event, id: string) => {
const substitution: Substitution | undefined = get_by_value(await data.substitutions, "id", id);
if (!substitution) return;
const modalSettings: ModalSettings = {
type: "component",
component: "substitutionCard",
meta: {
substitution: substitution,
drivers: await data.drivers,
races: await data.races,
substitute_select_value: update_substitution_substitute_select_values[substitution.id],
driver_select_value: update_substitution_for_select_values[substitution.id],
race_select_value: update_substitution_race_select_values[substitution.id],
disable_inputs: !data.admin,
},
};
modalStore.trigger(modalSettings);
};
const create_substitution_handler = async (event: Event) => {
const modalSettings: ModalSettings = {
type: "component",
component: "substitutionCard",
meta: {
drivers: await data.drivers,
races: await data.races,
substitute_select_value: update_substitution_substitute_select_values["create"],
driver_select_value: update_substitution_for_select_values["create"],
disable_inputs: !data.admin,
race_select_value: update_substitution_race_select_values["create"],
require_inputs: true,
headshot_template: get_driver_headshot_template(await data.graphics),
},
};
modalStore.trigger(modalSettings);
};
</script> </script>
<div class="pb-2"> <div class="pb-2">
<Button width="w-full" color="tertiary" onclick={create_substitution_handler} shadow> <Button width="w-full" color="tertiary" onclick={substitution_handler} shadow>
<span class="font-bold">Create New Substitution</span> <span class="font-bold">Create New Substitution</span>
</Button> </Button>
</div> </div>
{#await data.substitutions then substitutions} {#await data.substitutions then substitutions}
<Table data={substitutions} columns={substitutions_columns} handler={substitutions_handler} /> <Table data={substitutions} columns={substitutions_columns} handler={substitution_handler} />
{/await} {/await}

View File

@ -3,11 +3,30 @@
import type { Team } from "$lib/schema"; import type { Team } from "$lib/schema";
import { getModalStore, type ModalSettings, type ModalStore } from "@skeletonlabs/skeleton"; import { getModalStore, type ModalSettings, type ModalStore } from "@skeletonlabs/skeleton";
import type { PageData } from "./$types"; import type { PageData } from "./$types";
import { get_by_value, get_team_banner_template, get_team_logo_template } from "$lib/database"; import { get_by_value } from "$lib/database";
let { data }: { data: PageData } = $props(); let { data }: { data: PageData } = $props();
const teams_columns: TableColumn[] = [ const modalStore: ModalStore = getModalStore();
const team_handler = async (event: Event, id?: string) => {
const team: Team | undefined = get_by_value(await data.teams, "id", id ?? "Invalid");
// If we expect to find a team but don't, abort
if (id && !team) return;
const modalSettings: ModalSettings = {
type: "component",
component: "teamCard",
meta: {
data,
team,
},
};
modalStore.trigger(modalSettings);
};
const teams_columns: TableColumn[] = $derived([
{ {
data_value_name: "name", data_value_name: "name",
label: "Name", label: "Name",
@ -20,47 +39,14 @@
valuefun: async (value: string): Promise<string> => valuefun: async (value: string): Promise<string> =>
`<span class='badge border mr-2' style='color: ${value}; background: ${value};'>C</span>`, `<span class='badge border mr-2' style='color: ${value}; background: ${value};'>C</span>`,
}, },
]; ]);
const modalStore: ModalStore = getModalStore();
const teams_handler = async (event: Event, id: string) => {
const team: Team | undefined = get_by_value(await data.teams, "id", id);
if (!team) return;
const modalSettings: ModalSettings = {
type: "component",
component: "teamCard",
meta: {
team: team,
disable_inputs: !data.admin,
},
};
modalStore.trigger(modalSettings);
};
const create_team_handler = async (event: Event) => {
const modalSettings: ModalSettings = {
type: "component",
component: "teamCard",
meta: {
banner_template: get_team_banner_template(await data.graphics),
logo_template: get_team_logo_template(await data.graphics),
require_inputs: true,
disable_inputs: !data.admin,
},
};
modalStore.trigger(modalSettings);
};
</script> </script>
<div class="pb-2"> <div class="pb-2">
<Button width="w-full" color="tertiary" onclick={create_team_handler} shadow> <Button width="w-full" color="tertiary" onclick={team_handler} shadow>
<span class="font-bold">Create New Team</span> <span class="font-bold">Create New Team</span>
</Button> </Button>
</div> </div>
{#await data.teams then teams} {#await data.teams then teams}
<Table data={teams} columns={teams_columns} handler={teams_handler} /> <Table data={teams} columns={teams_columns} handler={team_handler} />
{/await} {/await}

View File

@ -19,71 +19,46 @@
RACE_PICTOGRAM_HEIGHT, RACE_PICTOGRAM_HEIGHT,
RACE_PICTOGRAM_WIDTH, RACE_PICTOGRAM_WIDTH,
} from "$lib/config"; } from "$lib/config";
import type { CurrentPickedUser, Driver, RacePick, Substitution } from "$lib/schema"; import type { CurrentPickedUser, RacePick } from "$lib/schema";
import { get_by_value, get_driver_headshot_template } from "$lib/database"; import { get_by_value, get_driver_headshot_template } from "$lib/database";
import { format } from "date-fns"; import { format } from "date-fns";
let { data }: { data: PageData } = $props(); let { data }: { data: PageData } = $props();
let currentpick = (): RacePick | undefined => const racepick: RacePick | undefined = $derived(
data.racepicks.filter( data.racepicks.filter(
(racepick: RacePick) => (racepick: RacePick) =>
racepick.expand.user.username === data.user?.username && racepick.expand.user.username === data.user?.username &&
racepick.race === data.currentrace?.id, racepick.race === data.currentrace?.id,
)[0] ?? undefined; )[0] ?? undefined,
let pxx_select_value: string = $state(currentpick()?.pxx ?? "");
let dnf_select_value: string = $state(currentpick()?.dnf ?? "");
const active_drivers_and_substitutes = (
drivers: Driver[],
substitutions: Substitution[],
): Driver[] => {
if (!data.currentrace) return [];
let active_and_substitutes: Driver[] = drivers.filter((driver: Driver) => driver.active);
substitutions
.filter((substitution: Substitution) => substitution.race === data.currentrace?.id)
.forEach((substitution: Substitution) => {
const for_index = active_and_substitutes.findIndex(
(driver: Driver) => driver.id === substitution.for,
); );
const sub_index = drivers.findIndex(
(driver: Driver) => driver.id === substitution.substitute,
);
active_and_substitutes[for_index] = drivers[sub_index];
});
return active_and_substitutes.sort((a: Driver, b: Driver) => a.code.localeCompare(b.code));
};
const modalStore: ModalStore = getModalStore(); const modalStore: ModalStore = getModalStore();
const create_guess_handler = async (event: Event) => { const racepick_handler = async (event: Event) => {
const modalSettings: ModalSettings = { const modalSettings: ModalSettings = {
type: "component", type: "component",
component: "racePickCard", component: "racePickCard",
meta: { meta: {
racepick: currentpick(), data,
currentrace: data.currentrace, racepick,
user: data.user,
drivers: active_drivers_and_substitutes(await data.drivers, await data.substitutions),
disable_inputs: false, // TODO: Datelock
headshot_template: get_driver_headshot_template(await data.graphics),
pxx_select_value: pxx_select_value,
dnf_select_value: dnf_select_value,
}, },
}; };
modalStore.trigger(modalSettings); modalStore.trigger(modalSettings);
}; };
const pickedusers = data.currentpickedusers.filter( // Users that have already picked the current race
let pickedusers: CurrentPickedUser[] = $derived(
data.currentpickedusers.filter(
(currentpickeduser: CurrentPickedUser) => currentpickeduser.picked, (currentpickeduser: CurrentPickedUser) => currentpickeduser.picked,
),
); );
const outstandingusers = data.currentpickedusers.filter(
// Users that didn't already pick the current race
let outstandingusers: CurrentPickedUser[] = $derived(
data.currentpickedusers.filter(
(currentpickeduser: CurrentPickedUser) => !currentpickeduser.picked, (currentpickeduser: CurrentPickedUser) => !currentpickeduser.picked,
),
); );
const dateformat: string = "dd.MM' 'HH:mm"; const dateformat: string = "dd.MM' 'HH:mm";
@ -109,7 +84,7 @@
<!-- Show information about the next race --> <!-- Show information about the next race -->
<div class="justify-center gap-2 lg:flex"> <div class="justify-center gap-2 lg:flex">
<div class="mt-2 flex gap-2"> <div class="mt-2 flex gap-2">
<div class="card flex w-full flex-col p-2 shadow"> <div class="card flex w-full min-w-40 flex-col p-2 shadow">
<span class="font-bold"> <span class="font-bold">
Step {data.currentrace.step}: {data.currentrace.name} Step {data.currentrace.step}: {data.currentrace.name}
</span> </span>
@ -138,13 +113,13 @@
<Countdown date={data.currentrace.racedate} extraclass="font-bold" /> <Countdown date={data.currentrace.racedate} extraclass="font-bold" />
</div> </div>
</div> </div>
<div class="card w-full p-2 shadow"> <div class="card w-full min-w-40 p-2 shadow">
<span class="text-nowrap font-bold">Track Layout:</span> <h1 class="mb-2 text-nowrap font-bold">Track Layout:</h1>
<LazyImage <LazyImage
src={data.currentrace.pictogram_url ?? "Invalid"} src={data.currentrace.pictogram_url ?? "Invalid"}
imgwidth={RACE_PICTOGRAM_WIDTH} imgwidth={RACE_PICTOGRAM_WIDTH}
imgheight={RACE_PICTOGRAM_HEIGHT} imgheight={RACE_PICTOGRAM_HEIGHT}
containerstyle="height: 115px; margin: auto;" containerstyle="height: 105px; margin: auto;"
imgstyle="background: transparent;" imgstyle="background: transparent;"
/> />
</div> </div>
@ -153,36 +128,36 @@
<!-- Only show the userguess if signed in --> <!-- Only show the userguess if signed in -->
{#if data.user} {#if data.user}
<div class="mt-2 flex gap-2"> <div class="mt-2 flex gap-2">
<div class="card w-full p-2 pb-0 shadow"> <div class="card w-full min-w-40 p-2 pb-0 shadow">
<h1 class="mb-2 text-nowrap font-bold">Your P{data.currentrace.pxx} Pick:</h1> <h1 class="mb-2 text-nowrap font-bold">Your P{data.currentrace.pxx} Pick:</h1>
{#await data.graphics then graphics} {#await data.graphics then graphics}
{#await data.drivers then drivers} {#await data.drivers then drivers}
<LazyImage <LazyImage
src={get_by_value(drivers, "id", currentpick()?.pxx ?? "")?.headshot_url ?? src={get_by_value(drivers, "id", racepick?.pxx ?? "")?.headshot_url ??
get_driver_headshot_template(graphics)} get_driver_headshot_template(graphics)}
imgwidth={DRIVER_HEADSHOT_WIDTH} imgwidth={DRIVER_HEADSHOT_WIDTH}
imgheight={DRIVER_HEADSHOT_HEIGHT} imgheight={DRIVER_HEADSHOT_HEIGHT}
containerstyle="height: 115px; margin: auto;" containerstyle="height: 115px; margin: auto;"
imgclass="bg-transparent cursor-pointer" imgclass="bg-transparent cursor-pointer"
hoverzoom hoverzoom
onclick={create_guess_handler} onclick={racepick_handler}
/> />
{/await} {/await}
{/await} {/await}
</div> </div>
<div class="card w-full p-2 pb-0 shadow"> <div class="card w-full min-w-40 p-2 pb-0 shadow">
<h1 class="mb-2 text-nowrap font-bold">Your DNF Pick:</h1> <h1 class="mb-2 text-nowrap font-bold">Your DNF Pick:</h1>
{#await data.graphics then graphics} {#await data.graphics then graphics}
{#await data.drivers then drivers} {#await data.drivers then drivers}
<LazyImage <LazyImage
src={get_by_value(drivers, "id", currentpick()?.dnf ?? "")?.headshot_url ?? src={get_by_value(drivers, "id", racepick?.dnf ?? "")?.headshot_url ??
get_driver_headshot_template(graphics)} get_driver_headshot_template(graphics)}
imgwidth={DRIVER_HEADSHOT_WIDTH} imgwidth={DRIVER_HEADSHOT_WIDTH}
imgheight={DRIVER_HEADSHOT_HEIGHT} imgheight={DRIVER_HEADSHOT_HEIGHT}
containerstyle="height: 115px; margin: auto;" containerstyle="height: 115px; margin: auto;"
imgclass="bg-transparent cursor-pointer" imgclass="bg-transparent cursor-pointer"
hoverzoom hoverzoom
onclick={create_guess_handler} onclick={racepick_handler}
/> />
{/await} {/await}
{/await} {/await}
@ -192,7 +167,7 @@
<!-- Show users that have and have not picked yet --> <!-- Show users that have and have not picked yet -->
<div class="mt-2 flex gap-2"> <div class="mt-2 flex gap-2">
<div class="card w-full p-2 shadow"> <div class="card w-full min-w-40 p-2 shadow">
<h1 class="text-nowrap font-bold"> <h1 class="text-nowrap font-bold">
Picked ({pickedusers.length}/{data.currentpickedusers.length}): Picked ({pickedusers.length}/{data.currentpickedusers.length}):
</h1> </h1>
@ -210,7 +185,7 @@
{/await} {/await}
</div> </div>
</div> </div>
<div class="card w-full p-2 shadow"> <div class="card w-full min-w-40 p-2 shadow">
<h1 class="text-nowrap font-bold"> <h1 class="text-nowrap font-bold">
Outstanding ({outstandingusers.length}/{data.currentpickedusers.length}): Outstanding ({outstandingusers.length}/{data.currentpickedusers.length}):
</h1> </h1>
@ -399,12 +374,3 @@
{/each} {/each}
</div> </div>
</div> </div>
<!-- "Table" of past guesses (not an actual table this time): -->
<!-- - Left column (rounded div column with race information: name, step, pxx) -->
<!-- - Show full result on-click? -->
<!-- - Rounded middle block (rest of width) with guesses: -->
<!-- - Just avatar at the top (reduce width) with points below, username on hover? -->
<!-- - Color-coded compact pxx + dnf picks (with podium + skull icons to differentiate?) -->
<!-- Make 2 variations (races as rows and races as columns) + settings toggle? -->