From da47668c29d20f27da692bc9a9bf1787d5f56169 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Wed, 5 Feb 2025 02:56:31 +0100 Subject: [PATCH] Lib: Simplify RacePickCard --- src/lib/components/cards/RacePickCard.svelte | 268 +++++++++---------- src/routes/racepicks/+page.svelte | 79 ++---- 2 files changed, 154 insertions(+), 193 deletions(-) diff --git a/src/lib/components/cards/RacePickCard.svelte b/src/lib/components/cards/RacePickCard.svelte index 9f0d0fd..f7f7ed6 100644 --- a/src/lib/components/cards/RacePickCard.svelte +++ b/src/lib/components/cards/RacePickCard.svelte @@ -1,7 +1,7 @@ - modalStore.close()} -> -
- - - {#if racepick && !disable_inputs} - - {/if} - - - - -
- - (drivers, "id", racepick?.pxx ?? "")?.headshot_url ?? + get_driver_headshot_template(graphics)} + imgid="headshot_preview" + width="w-full sm:w-auto" + imgwidth={DRIVER_HEADSHOT_WIDTH} + imgheight={DRIVER_HEADSHOT_HEIGHT} + imgonclick={(event: Event) => modalStore.close()} + > + modalStore.close()} > - P{currentrace?.pxx ?? "XX"} - - - - - DNF - - - -
- {#if racepick} - - - {:else} - + + + {#if racepick && !disabled} + {/if} -
-
-
-
+ + + + +
+ + {#await active_drivers_and_substitutes then pxx_drivers} + + P{data.currentrace?.pxx ?? "XX"} + + {/await} + + + {#await active_drivers_and_substitutes then pxx_drivers} + + DNF + + {/await} + + +
+ {#if racepick} + + + {:else} + + {/if} +
+
+ + + {/await} +{/await} diff --git a/src/routes/racepicks/+page.svelte b/src/routes/racepicks/+page.svelte index 93a2c70..77416ad 100644 --- a/src/routes/racepicks/+page.svelte +++ b/src/routes/racepicks/+page.svelte @@ -19,13 +19,13 @@ RACE_PICTOGRAM_HEIGHT, RACE_PICTOGRAM_WIDTH, } 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 { format } from "date-fns"; let { data }: { data: PageData } = $props(); - const currentpick: RacePick | undefined = $derived( + const racepick: RacePick | undefined = $derived( data.racepicks.filter( (racepick: RacePick) => racepick.expand.user.username === data.user?.username && @@ -33,58 +33,28 @@ )[0] ?? undefined, ); - const pxx_select_value: string = $derived(currentpick?.pxx ?? ""); - const dnf_select_value: string = $derived(currentpick?.dnf ?? ""); - - const active_drivers_and_substitutes: Promise = $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)); - }); - const modalStore: ModalStore = getModalStore(); - const create_guess_handler = async (event: Event) => { + const racepick_handler = async (event: Event) => { const modalSettings: ModalSettings = { type: "component", component: "racePickCard", meta: { - racepick: currentpick, - currentrace: data.currentrace, - user: data.user, - drivers: await active_drivers_and_substitutes, - 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, + data, + racepick, }, }; modalStore.trigger(modalSettings); }; + // Users that have already picked the current race let pickedusers: CurrentPickedUser[] = $derived( data.currentpickedusers.filter( (currentpickeduser: CurrentPickedUser) => currentpickeduser.picked, ), ); + + // Users that didn't already pick the current race let outstandingusers: CurrentPickedUser[] = $derived( data.currentpickedusers.filter( (currentpickeduser: CurrentPickedUser) => !currentpickeduser.picked, @@ -114,7 +84,7 @@
-
+
Step {data.currentrace.step}: {data.currentrace.name} @@ -143,13 +113,13 @@
-
- Track Layout: +
+

Track Layout:

@@ -158,36 +128,36 @@ {#if data.user}
-
+

Your P{data.currentrace.pxx} Pick:

{#await data.graphics then graphics} {#await data.drivers then drivers} {/await} {/await}
-
+

Your DNF Pick:

{#await data.graphics then graphics} {#await data.drivers then drivers} {/await} {/await} @@ -197,7 +167,7 @@
-
+

Picked ({pickedusers.length}/{data.currentpickedusers.length}):

@@ -215,7 +185,7 @@ {/await}
-
+

Outstanding ({outstandingusers.length}/{data.currentpickedusers.length}):

@@ -404,12 +374,3 @@ {/each}
- - - - - - - - -