Lib: Simplify SubstitutionCard
This commit is contained in:
@ -1,28 +1,35 @@
|
||||
<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 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";
|
||||
|
||||
let { data }: { data: PageData } = $props();
|
||||
|
||||
// TODO: Cleanup
|
||||
const update_substitution_substitute_select_values: { [key: string]: string } = $state({});
|
||||
const update_substitution_for_select_values: { [key: string]: string } = $state({});
|
||||
const update_substitution_race_select_values: { [key: string]: string } = $state({});
|
||||
data.substitutions.then((substitutions: Substitution[]) =>
|
||||
substitutions.forEach((substitution: Substitution) => {
|
||||
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 modalStore: ModalStore = getModalStore();
|
||||
const substitution_handler = async (event: Event, id?: string) => {
|
||||
const substitution: Substitution | undefined = get_by_value(
|
||||
await data.substitutions,
|
||||
"id",
|
||||
id ?? "Invalid",
|
||||
);
|
||||
|
||||
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",
|
||||
label: "Step",
|
||||
@ -49,56 +56,14 @@
|
||||
valuefun: async (value: string): Promise<string> =>
|
||||
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>
|
||||
|
||||
<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>
|
||||
</Button>
|
||||
</div>
|
||||
{#await data.substitutions then substitutions}
|
||||
<Table data={substitutions} columns={substitutions_columns} handler={substitutions_handler} />
|
||||
<Table data={substitutions} columns={substitutions_columns} handler={substitution_handler} />
|
||||
{/await}
|
||||
|
||||
Reference in New Issue
Block a user