diff --git a/src/lib/api/index.ts b/src/lib/api/index.ts index ea339df..31fa33f 100644 --- a/src/lib/api/index.ts +++ b/src/lib/api/index.ts @@ -30,6 +30,9 @@ export const apiGetHunt = (huntId: string) => export const apiGetUnstartedHunts = () => client.get('/hunt/unstarted') +export const apiGetOngoingHunts = () => + client.get('/hunt/ongoing') + export const apiGetAllHunts = (status?: 'UNSTARTED' | 'ONGOING' | 'CLOSED') => client.get(`/hunt${status ? `?status=${status}` : ''}`) @@ -41,9 +44,6 @@ export const apiUpdateHunt = (huntId: string, title: string, startDateTime: stri // ── Hunter ──────────────────────────────────────────────────────────────────── -export const apiGetOngoingHunts = () => - client.get('/hunter/hunt/ongoing') - export const apiGetHunterTeam = (huntId: string) => client.get(`/hunter/hunt/${huntId}/team`) diff --git a/src/routes/hunter/HuntList.svelte b/src/routes/hunter/HuntList.svelte index ed32eb8..a7f01e6 100644 --- a/src/routes/hunter/HuntList.svelte +++ b/src/routes/hunter/HuntList.svelte @@ -4,8 +4,8 @@ import {huntStatus, formatDateTime} from '../../lib/utils' import { apiCreateTeam, - apiGetHunterTeam, apiGetOngoingHunts, + apiGetHunterTeam, apiGetUnstartedHunts, apiGetTeamHunters, apiJoinTeam, @@ -27,6 +27,7 @@ let tab = $state<'active' | 'upcoming'>('active') // huntId → team the hunter has joined (null = not joined) + let ongoingTeams = $state>({}) let upcomingTeams = $state>({}) $effect(() => { @@ -36,14 +37,20 @@ ]).then(async ([o, u]) => { ongoing = o upcoming = u - const teamEntries = await Promise.all( - u.map(hunt => + const [ongoingEntries, upcomingEntries] = await Promise.all([ + Promise.all(o.map(hunt => apiGetHunterTeam(hunt.id) .then(t => [hunt.id, t] as const) .catch(() => [hunt.id, null] as const) - ) - ) - upcomingTeams = Object.fromEntries(teamEntries) + )), + Promise.all(u.map(hunt => + apiGetHunterTeam(hunt.id) + .then(t => [hunt.id, t] as const) + .catch(() => [hunt.id, null] as const) + )), + ]) + ongoingTeams = Object.fromEntries(ongoingEntries) + upcomingTeams = Object.fromEntries(upcomingEntries) }).catch(e => { error = e instanceof Error ? e.message : 'Failed to load hunts' }).finally(() => { @@ -51,25 +58,31 @@ }) }) - // ── Upcoming hunt sheet ────────────────────────────────────────────────────── + // ── Hunt sheet (shared for active + upcoming) ───────────────────────────────── - let sheetHunt = $state(null) - let sheetTeams = $state([]) - let sheetMembers = $state([]) - let sheetLoading = $state(false) - let newTeamName = $state('') - let creatingTeam = $state(false) - let joiningTeamId = $state(null) - let sheetError = $state('') + let sheetHunt = $state(null) + let sheetIsActive = $state(false) + let sheetTeams = $state([]) + let sheetMembers = $state([]) + let sheetLoading = $state(false) + let newTeamName = $state('') + let creatingTeam = $state(false) + let joiningTeamId = $state(null) + let sheetError = $state('') - async function openSheet(hunt: HuntResponse) { - sheetHunt = hunt - sheetError = '' - newTeamName = '' - sheetMembers = [] - sheetLoading = true + function currentTeam(hunt: HuntResponse) { + return sheetIsActive ? ongoingTeams[hunt.id] : upcomingTeams[hunt.id] + } + + async function openSheet(hunt: HuntResponse, isActive: boolean) { + sheetHunt = hunt + sheetIsActive = isActive + sheetError = '' + newTeamName = '' + sheetMembers = [] + sheetLoading = true try { - const myTeam = upcomingTeams[hunt.id] + const myTeam = currentTeam(hunt) if (myTeam) { const [teams, members] = await Promise.all([ apiListTeams(hunt.id), @@ -88,7 +101,7 @@ } function closeSheet() { - sheetHunt = null + sheetHunt = null sheetTeams = [] } @@ -116,18 +129,20 @@ try { await apiJoinTeam(sheetHunt.id, teamId) const team = sheetTeams.find(t => t.id === teamId) ?? null - upcomingTeams = { ...upcomingTeams, [sheetHunt.id]: team } - newTeamName = '' - closeSheet() + if (sheetIsActive) { + ongoingTeams = { ...ongoingTeams, [sheetHunt.id]: team } + push(`/hunt/${sheetHunt.id}`) + } else { + upcomingTeams = { ...upcomingTeams, [sheetHunt.id]: team } + newTeamName = '' + closeSheet() + } } catch (e: unknown) { sheetError = e instanceof Error ? e.message : 'Failed to join team' } finally { joiningTeamId = null } } - - // ── Helpers ────────────────────────────────────────────────────────────────── -
@@ -162,9 +177,10 @@ {:else}
{#each ongoing as hunt} + {@const myTeam = ongoingTeams[hunt.id]} {/each} @@ -192,7 +213,7 @@ {@const myTeam = upcomingTeams[hunt.id]}
@@ -254,8 +276,16 @@
{/if} + {#if sheetIsActive} + + {/if} {:else} + {#if sheetIsActive} +
This hunt is already in progress — join a team to start playing!
+ {/if}
{ e.preventDefault(); createTeam() }} class="flex gap-2">