Skip to content

Commit

Permalink
use modal for editing match results
Browse files Browse the repository at this point in the history
  • Loading branch information
craigkai committed Feb 14, 2024
1 parent d19e35a commit 2114ded
Show file tree
Hide file tree
Showing 17 changed files with 164 additions and 115 deletions.
7 changes: 7 additions & 0 deletions src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,26 @@ declare global {
supabase: SupabaseClient<Database>;
getSession(): Promise<Session | null>;
}

interface PageData {
eventName?: string;
eventId?: string;
supabase: SupabaseClient<Database>;
session: Session | null;
}

namespace App {
// interface Error {}
interface Locals {
supabase: SupabaseClient;
getSession(): Promise<Session | null>;
}
// interface Platform {}

interface PageState {
matchId?: number;
showModal?: boolean;
}
}
type supabaseClient = SupabaseClient<Database>;
}
Expand Down
File renamed without changes.
69 changes: 69 additions & 0 deletions src/components/EditMatch.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<script lang="ts">
import { Label, Input } from 'flowbite-svelte';
import type { HttpError } from '@sveltejs/kit';
import { error, success } from '$lib/toast';
import { Matches } from '$lib/matches';
import { pushState } from '$app/navigation';
export let matchId: number;
export let matches: Matches;
let match = matches?.matches?.find((m) => m.id === matchId);
async function updateMatch() {
if (match) {
try {
// Need to convert string inputs to numbers
match.team1_score = Number(match.team1_score);
match.team2_score = Number(match.team2_score);
match = await matches.put(match);
success(
`Match ${match.matches_team1_fkey.name} vs ${match.matches_team2_fkey.name} updated`
);
} catch (err) {
error((err as HttpError).toString());
}
}
}
function closeModal() {
pushState('', {
showModal: false
});
}
</script>

{#if match}
<div class="flex flex-col">
<Label for="team1-score-input">Team `{match.matches_team1_fkey.name}`:</Label>
<Input
id="team1-score-input"
size="md"
type="number"
bind:value={match.team1_score}
on:keydown={(e) => {
if (e?.key === 'Enter') {
updateMatch();
} else if (e?.key === 'Escape') {
closeModal();
}
}}
/>

<Label for="team2-score-input">Team `{match.matches_team2_fkey.name}`:</Label>
<Input
id="team2-score-input"
size="sm"
type="number"
bind:value={match.team2_score}
on:keydown={(e) => {
if (e?.key === 'Enter') {
updateMatch();
} else if (e?.key === 'Escape') {
closeModal();
}
}}
/>
</div>
{/if}
File renamed without changes.
File renamed without changes.
58 changes: 58 additions & 0 deletions src/components/Match.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<script lang="ts">
import { Tooltip } from 'flowbite-svelte';
import { page } from '$app/stores';
import { pushState } from '$app/navigation';
export let match: MatchRow;
export let readOnly: boolean = false;
export let showWinLoss: boolean = true;
function showModal() {
pushState('', {
showModal: true,
matchId: match.id
});
}
</script>

<!-- svelte-ignore a11y-no-static-element-interactions -->
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore missing-declaration -->
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div
class="flex justify-center place-items-center"
on:click={() => {
if (!readOnly && !$page.state.showModal) {
showModal();
}
}}
>
<span
class="p-1 rounded"
class:bg-green-300={showWinLoss &&
match?.team1_score &&
match?.team2_score &&
match.team1_score > match.team2_score}
class:bg-red-300={showWinLoss &&
match?.team1_score &&
match?.team2_score &&
match.team2_score > match.team1_score}
>
{match?.matches_team1_fkey?.name}</span
>
vs
<span
class="p-1 rounded"
class:bg-green-300={showWinLoss &&
match?.team1_score &&
match?.team2_score &&
match?.team2_score > match?.team1_score}
class:bg-red-300={showWinLoss &&
match?.team1_score &&
match?.team2_score &&
match?.team1_score > match?.team2_score}>{match?.matches_team2_fkey?.name}</span
>
</div>
{#if match?.team1_score && match?.team2_score}
<Tooltip>{match?.team1_score} to {match?.team2_score}</Tooltip>
{/if}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
: defaultTdClass + 'border-solid border-2 border-yellow-300 bg-yellow-200'
: defaultTdClass}
>
<ViewMatch {matches} {match} {readOnly} showWinLoss={!hasDefaultTeam} />
<ViewMatch {match} {readOnly} showWinLoss={!hasDefaultTeam} />
</TableBodyCell>
{/each}
{#if tournament.refs === 'teams'}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
103 changes: 0 additions & 103 deletions src/lib/components/tournament/Match.svelte

This file was deleted.

4 changes: 2 additions & 2 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<script lang="ts">
import '../app.postcss';
import Header from '$lib/components/Header.svelte';
import Footer from '$lib/components/Footer.svelte';
import Header from '$components/Header.svelte';
import Footer from '$components/Footer.svelte';
import { invalidate } from '$app/navigation';
import { onMount } from 'svelte';
import type { LayoutData } from './$types';
Expand Down
6 changes: 3 additions & 3 deletions src/routes/events/[slug]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import { Event } from '$lib/event';
import { Matches as MatchesInstance } from '$lib/matches';
import { Teams } from '$lib/teams';
import Matches from '$lib/components/tournament/Matches.svelte';
import Bracket from '$lib/components/tournament/Bracket.svelte';
import Matches from '$components/Matches.svelte';
import Bracket from '$components/Bracket.svelte';
import { EventSupabaseDatabaseService } from '$lib/database/event';
import { MatchesSupabaseDatabaseService } from '$lib/database/matches';
import { TeamsSupabaseDatabaseService } from '$lib/database/teams';
import { loadInitialData } from '$lib/helper';
import Standings from '$lib/components/tournament/Standings.svelte';
import Standings from '$components/Standings.svelte';
import { Tabs, TabItem, Label, Select, Spinner } from 'flowbite-svelte';
import { page } from '$app/stores';
import { browser } from '$app/environment';
Expand Down
19 changes: 14 additions & 5 deletions src/routes/protected-routes/events/[slug]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<script lang="ts">
import type { PageData } from '$types';
import { Event as EventInstance } from '$lib/event';
import Settings from '$lib/components/tournament/Settings.svelte';
import Bracket from '$lib/components/tournament/Bracket.svelte';
import Settings from '$components/Settings.svelte';
import Bracket from '$components/Bracket.svelte';
import { Matches as MatchesInstance } from '$lib/matches';
import { Teams as TeamsInstance } from '$lib/teams';
import { EventSupabaseDatabaseService } from '$lib/database/event';
import { MatchesSupabaseDatabaseService } from '$lib/database/matches';
import { TeamsSupabaseDatabaseService } from '$lib/database/teams';
import Standings from '$lib/components/tournament/Standings.svelte';
import Matches from '$lib/components/tournament/Matches.svelte';
import Teams from '$lib/components/tournament/Teams.svelte';
import Standings from '$components/Standings.svelte';
import Matches from '$components/Matches.svelte';
import Teams from '$components/Teams.svelte';
import { loadInitialData } from '$lib/helper';
import { Tabs, TabItem, Spinner } from 'flowbite-svelte';
import EditMatch from '$components/EditMatch.svelte';
import { page } from '$app/stores';
import { Modal } from 'flowbite-svelte';
export let data: PageData;
const eventSupabaseDatabaseService = new EventSupabaseDatabaseService(data?.supabase);
Expand All @@ -27,6 +30,12 @@
const loadingInitialDataPromise = loadInitialData(tournament, $matches, teams);
</script>

{#if $page.state.showModal}
<Modal size="xs" on:close={() => history.back()} open={true} outsideclose autoclose>
<EditMatch matchId={$page?.state?.matchId} bind:matches />
</Modal>
{/if}

<div class="flex flex-col items-center">
{#await loadingInitialDataPromise}
<Spinner />
Expand Down
1 change: 1 addition & 0 deletions svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const config = {
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
adapter: adapter(),
alias: {
$components: path.resolve('src/components'),
$supabaseTypes: path.resolve('src/types/supabase')
}
}
Expand Down
10 changes: 9 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"types": ["vitest/importMeta"]
"types": ["vitest/importMeta"],
"paths": {
"$src": ["./src"],
"$src/*": ["./src/*"],
"$lib": ["./src/lib"],
"$lib/*": ["./src/lib/*"],
"$components": ["./src/lib"],
"$components/*": ["./src/lib/*"]
}
}
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
//
Expand Down

0 comments on commit 2114ded

Please sign in to comment.