diff --git a/src/lib/components/cards/RaceResultCard.svelte b/src/lib/components/cards/RaceResultCard.svelte index 1f80d06..95e061a 100644 --- a/src/lib/components/cards/RaceResultCard.svelte +++ b/src/lib/components/cards/RaceResultCard.svelte @@ -9,7 +9,7 @@ type ToastStore, } from "@skeletonlabs/skeleton"; import { Button, Card, Dropdown } from "$lib/components"; - import type { Driver, Race, RaceResult } from "$lib/schema"; + import type { Driver, Race, RaceResult, Substitution } from "$lib/schema"; import { get_by_value } from "$lib/database"; import { race_dropdown_options } from "$lib/dropdown"; import { pb } from "$lib/pocketbase"; @@ -36,12 +36,16 @@ const toastStore: ToastStore = getToastStore(); + // Await promises let races: Race[] | undefined = $state(undefined); data.races.then((r: Race[]) => (races = r)); let drivers: Driver[] | undefined = $state(undefined); data.drivers.then((d: Driver[]) => (drivers = d)); + let substitutions: Substitution[] | undefined = $state(undefined); + data.substitutions.then((s: Substitution[]) => (substitutions = s)); + // Constants const labelwidth: string = "70px"; @@ -79,27 +83,35 @@ let pxxs_ids: string[] = $state(result?.pxxs ?? []); let dnfs_ids: string[] = $state(result?.dnfs ?? []); - let pxxs_options: AutocompleteOption[] = $derived.by(() => - (drivers ?? []).map((driver: Driver) => { - return { - // NOTE: Because Skeleton displays the values inside the autocomplete input, - // we have to supply the driver code twice and manage a list of ids manually (ugh) - label: driver.code, - value: driver.code, - }; - }), - ); + let pxxs_options: AutocompleteOption[] = $derived.by(() => { + if (!race_select_value) return []; - let dnfs_options: AutocompleteOption[] = $derived.by(() => - (drivers ?? []).map((driver: Driver) => { + let active_and_substitutes: Driver[] = (drivers ?? []).filter( + (driver: Driver) => driver.active, + ); + + (substitutions ?? []) + .filter((substitution: Substitution) => substitution.race === race_select_value) + .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.map((driver: Driver) => { return { // NOTE: Because Skeleton displays the values inside the autocomplete input, // we have to supply the driver code twice and manage a list of ids manually (ugh) label: driver.code, value: driver.code, }; - }), - ); + }); + }); let pxxs_whitelist: string[] = $derived.by(() => (drivers ?? []).map((driver: Driver) => { @@ -207,74 +219,78 @@ - - {#await data.races then races} - - Race - - {/await} +
+ + {#await data.races then races} + + Race + + {/await} -
- - -
- + -
+
+ +
- - -
- + -
+
+ +
- -
- {#if result} - - - {:else} - - {/if} -
+ +
+ {#if result} + + + {:else} + + {/if} +
+ {/if}