From b51c78aed4db17e5a349327058b658dd84f87e8f Mon Sep 17 00:00:00 2001 From: Craig Kaiser Date: Sun, 17 Dec 2023 14:49:03 -0500 Subject: [PATCH] allow editing for match scores --- src/lib/SupabaseDatabaseService.ts | 14 ++-- src/lib/components/tournament/Match.svelte | 71 +++++++++---------- src/lib/components/tournament/Matches.svelte | 28 ++++---- src/lib/tournament.ts | 6 +- .../events/[slug]/+page.svelte | 7 +- 5 files changed, 65 insertions(+), 61 deletions(-) diff --git a/src/lib/SupabaseDatabaseService.ts b/src/lib/SupabaseDatabaseService.ts index aab58ea..160f456 100644 --- a/src/lib/SupabaseDatabaseService.ts +++ b/src/lib/SupabaseDatabaseService.ts @@ -15,6 +15,7 @@ export interface DatabaseService { createTeam(team: TeamRow): Promise; deleteMatchesByEvent(eventId: string): Promise; insertMatches(matches: MatchRow[]): Promise; + updateMatch(match: MatchRow): Promise; } export class SupabaseDatabaseService implements DatabaseService { @@ -165,12 +166,17 @@ export class SupabaseDatabaseService implements DatabaseService { return response.data ?? []; } - async updateMatch(match: MatchRow): Promise { + async updateMatch(match: MatchRow): Promise> { const response: MatchRow = await this.supabaseClient.from('matches') - .update(match) + .update({ + team1_score: match.team1_score, + team2_score: match.team2_score, + }) .eq('id', match.id) - .select('*'); + .select('*, matches_team1_fkey(name), matches_team2_fkey(name)') + .single(); + this.handleDatabaseError(response); - return response.data; + return response; } } diff --git a/src/lib/components/tournament/Match.svelte b/src/lib/components/tournament/Match.svelte index 0dc3a54..71b7ed4 100644 --- a/src/lib/components/tournament/Match.svelte +++ b/src/lib/components/tournament/Match.svelte @@ -1,7 +1,7 @@ @@ -21,51 +21,50 @@
(editing = true)}> {#if editing} - {match.matches_team1_fkey.name} vs {match.matches_team2_fkey.name} - - (editing = false)} - on:keydown={(e) => { - if (e?.key === 'Enter') { - updateMatch(); - editing = false; - } - }} - /> - - - (editing = false)} - on:keydown={(e) => { - if (e?.key === 'Enter') { - updateMatch(); - editing = false; - } - }} - /> - + + (editing = false)} + on:keydown={(e) => { + if (e?.key === 'Enter') { + updateMatch(); + editing = false; + } + }} + /> + + + (editing = false)} + on:keydown={(e) => { + if (e?.key === 'Enter') { + updateMatch(); + editing = false; + } + }} + /> {:else} - +
match?.team2_score} class:bg-red-300={match?.team2_score > match?.team1_score} > - {match.matches_team1_fkey.name} vs match?.team1_score} class:bg-red-300={match?.team1_score > match?.team2_score} - >{match.matches_team2_fkey.name} + >{match?.matches_team2_fkey?.name} +
{#if match?.team1_score && match?.team2_score} {match?.team1_score} to {match?.team2_score} {/if} diff --git a/src/lib/components/tournament/Matches.svelte b/src/lib/components/tournament/Matches.svelte index b4d57ee..b70ae43 100644 --- a/src/lib/components/tournament/Matches.svelte +++ b/src/lib/components/tournament/Matches.svelte @@ -16,13 +16,10 @@ export let tournament: Tournament; async function generateMatches() { - tournament - .createMatches() - .catch((err: HttpError) => { - console.log(err); - error(err?.body?.message); - }) - .then((res: Tournament) => (tournament = res)); + tournament.createMatches().catch((err: HttpError) => { + error(err?.body?.message); + }); + .then((res: Tournament) => (tournament = res)); } const matchesPromise = tournament.loadMatches(); @@ -52,7 +49,6 @@ Round - Ref {#each Array(tournament.settings.courts) as _, i} Court {i + 1} {/each} @@ -63,13 +59,15 @@ {@const matchesForRound = matchesForEachRound[round].sort( (a, b) => a.round - b.round || a.court - b.court )} - {#each matchesForRound as match} - - {round} - Some Ref - - - {/each} + + {round} + + {#each matchesForRound as match} + + + + {/each} + {/each}
diff --git a/src/lib/tournament.ts b/src/lib/tournament.ts index 0424255..f6dd1c9 100644 --- a/src/lib/tournament.ts +++ b/src/lib/tournament.ts @@ -1,6 +1,7 @@ import { error } from '@sveltejs/kit'; import { RoundRobin } from './roundRobin'; import type { DatabaseService } from './SupabaseDatabaseService'; +import type { PostgrestSingleResponse } from '@supabase/supabase-js'; export class Tournament { private databaseService: DatabaseService; @@ -142,8 +143,9 @@ export class Tournament { } } - async updateMatch(match: MatchRow): Promise { - const res: MatchRow = await this.databaseService.updateMatch(match); + async updateMatch(match: MatchRow): Promise> { + const res = await this.databaseService.updateMatch(match); + match = res.data; return res; } diff --git a/src/routes/protected-routes/events/[slug]/+page.svelte b/src/routes/protected-routes/events/[slug]/+page.svelte index 0f41b1e..d1f6d61 100644 --- a/src/routes/protected-routes/events/[slug]/+page.svelte +++ b/src/routes/protected-routes/events/[slug]/+page.svelte @@ -15,7 +15,6 @@ export let data: PageData; const databaseService = new SupabaseDatabaseService(data?.supabase); let tournament = new Tournament(databaseService, data?.supabase); - console.log('Tournament is ' + tournament); // Load our event or if creating we just load the edit component async function loadEvent() { @@ -76,9 +75,9 @@ let loadingEventPromise = loadEvent(); - $: if (tournament?.settings && tournament.settings?.teams?.length > 0) { - tournament.createMatches().catch((err: HttpError) => error(err?.body?.message)); - } + // $: if (tournament?.settings && tournament.settings?.teams?.length > 0) { + // tournament.createMatches().catch((err: HttpError) => error(err?.body?.message)); + // } {#await loadingEventPromise}