Skip to content

Commit

Permalink
prep teams
Browse files Browse the repository at this point in the history
  • Loading branch information
craigkai committed Mar 9, 2024
1 parent 3852182 commit 659661d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/components/Settings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import { Calendar } from '$components/ui/calendar';
import * as Popover from '$components/ui/popover';
import CalendarIcon from 'lucide-svelte/icons/calendar';
import { goto } from '$app/navigation';
export let data: SuperValidated<Infer<FormSchema>>;
export let event_id: number | 'create';
Expand Down Expand Up @@ -59,7 +58,7 @@
: undefined;
</script>

<form method="POST" action="?/{event_id === 'create' ? 'create' : 'settings'}" use:enhance>
<form method="POST" action="?/{event_id === 'create' ? 'createEvent' : 'updateEvent'}" use:enhance>
<Form.Field {form} name="name">
<Form.Control let:attrs>
<Form.Label>Name</Form.Label>
Expand Down Expand Up @@ -178,7 +177,7 @@
<Form.Button>Submit</Form.Button>

{#if event_id !== 'create'}
<form method="POST" action="?/delete" use:enhance>
<form method="POST" action="?/deleteEvent" use:enhance>
<Form.Button>Delete</Form.Button>
</form>
{/if}
Expand Down
43 changes: 39 additions & 4 deletions src/routes/protected-routes/events/[slug]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { superValidate } from 'sveltekit-superforms';
import { formSchema as settingsSchema } from '$schemas/settingsSchema';
import { eventsInsertSchema, eventsUpdateSchema } from '$schemas/supabase';
import { formSchema as teamsSchema } from '$schemas/teamsSchema';
import { eventsInsertSchema, eventsUpdateSchema, teamsInsertSchema } from '$schemas/supabase';
import { zod } from 'sveltekit-superforms/adapters';
import type { PageServerLoad, Actions } from './$types';
import { fail, redirect } from '@sveltejs/kit';
import { Event } from '$lib/event';
import { EventSupabaseDatabaseService } from '$lib/database/event';
import type { ZodError } from 'zod';
import { fromZodError } from 'zod-validation-error';
import { TeamsSupabaseDatabaseService } from '$lib/database/teams';
import { Teams } from '$lib/teams';

export const load: PageServerLoad = async ({ params }) => {
return {
Expand All @@ -17,7 +20,7 @@ export const load: PageServerLoad = async ({ params }) => {
};

export const actions: Actions = {
settings: async (event) => {
updateEvent: async (event) => {
const form = await superValidate(event, zod(settingsSchema));
if (!form.valid) {
return fail(400, {
Expand Down Expand Up @@ -52,7 +55,7 @@ export const actions: Actions = {
}
},

create: async (event) => {
createEvent: async (event) => {
const form = await superValidate(event, zod(settingsSchema));
if (!form.valid) {
return fail(400, {
Expand Down Expand Up @@ -85,7 +88,7 @@ export const actions: Actions = {
redirect(303, `/protected-routes/events/${newId}`);
},

delete: async (event) => {
deleteEvent: async (event) => {
const event_id = Number(event.params.slug);

const eventSupabaseDatabaseService = new EventSupabaseDatabaseService(event.locals.supabase);
Expand All @@ -95,5 +98,37 @@ export const actions: Actions = {
await tournament.delete();

redirect(303, '/protected-routes/dashboard');
},

createTeam: async (event) => {
const form = await superValidate(event, zod(teamsSchema));
if (!form.valid) {
return fail(400, {
form
});
}

const event_id = Number(event.params.slug);

const teamsSupabaseDatabaseService = new TeamsSupabaseDatabaseService(event.locals.supabase);
let teams = new Teams(event_id, teamsSupabaseDatabaseService);

try {
const newTeam: Partial<TeamRow> = {
name: form.data.name,
event_id
};
await teams.create(newTeam);
return { form };
} catch (error) {
form.valid = false;

const validationError = fromZodError(error as ZodError<typeof teamsInsertSchema>);
form.message = validationError.message;

return fail(400, {
form
});
}
}
};
2 changes: 1 addition & 1 deletion src/schemas/settingsSchema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { string, z } from 'zod';
import { z } from 'zod';

export const formSchema = z.object({
name: z.string().refine((v) => v, { message: 'A event name is required.' }),
Expand Down
9 changes: 9 additions & 0 deletions src/schemas/teamsSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from 'zod';

export const formSchema = z.object({
name: z.string().refine((v) => v, { message: 'A team name is required.' }),
event_id: z.coerce.number().refine((v) => v, { message: 'An event id is required.' }),
id: z.coerce.number().optional()
});

export type FormSchema = typeof formSchema;

0 comments on commit 659661d

Please sign in to comment.