Data/Season: Split Teams/Drivers/Races/Substitutions into individual routes
This commit is contained in:
74
src/routes/data/season/teams/+page.server.ts
Normal file
74
src/routes/data/season/teams/+page.server.ts
Normal file
@ -0,0 +1,74 @@
|
||||
import type { Actions } from "./$types";
|
||||
import { form_data_clean, form_data_ensure_keys, form_data_get_and_remove_id } from "$lib/form";
|
||||
import { image_to_avif } from "$lib/server/image";
|
||||
import {
|
||||
TEAM_BANNER_HEIGHT,
|
||||
TEAM_BANNER_WIDTH,
|
||||
TEAM_LOGO_HEIGHT,
|
||||
TEAM_LOGO_WIDTH,
|
||||
} from "$lib/config";
|
||||
|
||||
export const actions = {
|
||||
create_team: async ({ request, locals }) => {
|
||||
if (!locals.admin) return { unauthorized: true };
|
||||
|
||||
const data: FormData = form_data_clean(await request.formData());
|
||||
form_data_ensure_keys(data, ["name", "banner", "logo", "color"]);
|
||||
|
||||
// Compress banner
|
||||
const banner_avif: Blob = await image_to_avif(
|
||||
await (data.get("banner") as File).arrayBuffer(),
|
||||
TEAM_BANNER_WIDTH,
|
||||
TEAM_BANNER_HEIGHT,
|
||||
);
|
||||
data.set("banner", banner_avif);
|
||||
|
||||
// Compress logo
|
||||
const logo_avif: Blob = await image_to_avif(
|
||||
await (data.get("logo") as File).arrayBuffer(),
|
||||
TEAM_LOGO_WIDTH,
|
||||
TEAM_LOGO_HEIGHT,
|
||||
);
|
||||
data.set("logo", logo_avif);
|
||||
|
||||
await locals.pb.collection("teams").create(data);
|
||||
},
|
||||
|
||||
update_team: async ({ request, locals }) => {
|
||||
if (!locals.admin) return { unauthorized: true };
|
||||
|
||||
const data: FormData = form_data_clean(await request.formData());
|
||||
const id: string = form_data_get_and_remove_id(data);
|
||||
|
||||
if (data.has("banner")) {
|
||||
// Compress banner
|
||||
const banner_avif: Blob = await image_to_avif(
|
||||
await (data.get("banner") as File).arrayBuffer(),
|
||||
TEAM_BANNER_WIDTH,
|
||||
TEAM_BANNER_HEIGHT,
|
||||
);
|
||||
data.set("banner", banner_avif);
|
||||
}
|
||||
|
||||
if (data.has("logo")) {
|
||||
// Compress logo
|
||||
const logo_avif: Blob = await image_to_avif(
|
||||
await (data.get("logo") as File).arrayBuffer(),
|
||||
TEAM_LOGO_WIDTH,
|
||||
TEAM_LOGO_HEIGHT,
|
||||
);
|
||||
data.set("logo", logo_avif);
|
||||
}
|
||||
|
||||
await locals.pb.collection("teams").update(id, data);
|
||||
},
|
||||
|
||||
delete_team: async ({ request, locals }) => {
|
||||
if (!locals.admin) return { unauthorized: true };
|
||||
|
||||
const data: FormData = form_data_clean(await request.formData());
|
||||
const id: string = form_data_get_and_remove_id(data);
|
||||
|
||||
await locals.pb.collection("teams").delete(id);
|
||||
},
|
||||
} satisfies Actions;
|
||||
66
src/routes/data/season/teams/+page.svelte
Normal file
66
src/routes/data/season/teams/+page.svelte
Normal file
@ -0,0 +1,66 @@
|
||||
<script lang="ts">
|
||||
import { Button, Table, type TableColumn } from "$lib/components";
|
||||
import type { Team } from "$lib/schema";
|
||||
import { getModalStore, type ModalSettings, type ModalStore } from "@skeletonlabs/skeleton";
|
||||
import type { PageData } from "./$types";
|
||||
import { get_by_value } from "$lib/database";
|
||||
|
||||
let { data }: { data: PageData } = $props();
|
||||
|
||||
const teams_columns: TableColumn[] = [
|
||||
{
|
||||
data_value_name: "name",
|
||||
label: "Name",
|
||||
valuefun: async (value: string): Promise<string> =>
|
||||
`<span class='badge variant-filled-surface'>${value}</span>`,
|
||||
},
|
||||
{
|
||||
data_value_name: "color",
|
||||
label: "Color",
|
||||
valuefun: async (value: string): Promise<string> =>
|
||||
`<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(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 = (event: Event) => {
|
||||
const modalSettings: ModalSettings = {
|
||||
type: "component",
|
||||
component: "teamCard",
|
||||
meta: {
|
||||
banner_template:
|
||||
get_by_value(data.graphics, "name", "team_banner_template")?.file_url ?? "Invalid",
|
||||
logo_template:
|
||||
get_by_value(data.graphics, "name", "team_logo_template")?.file_url ?? "Invalid",
|
||||
require_inputs: true,
|
||||
disable_inputs: !data.admin,
|
||||
},
|
||||
};
|
||||
|
||||
modalStore.trigger(modalSettings);
|
||||
};
|
||||
</script>
|
||||
|
||||
<div class="pb-2">
|
||||
<Button width="w-full" color="surface" onclick={create_team_handler}>
|
||||
<b>Create New Team</b>
|
||||
</Button>
|
||||
</div>
|
||||
<Table data={data.teams} columns={teams_columns} handler={teams_handler} />
|
||||
Reference in New Issue
Block a user