From 9fbb9f5e83a1ccc5ce83e26494862dc57dda6d44 Mon Sep 17 00:00:00 2001 From: 180079995 <101566121+180079995@users.noreply.github.com> Date: Tue, 24 Dec 2024 17:33:44 +0800 Subject: [PATCH] Recent session (#63) * Recent host session * Recent participant sessions * fix lint What --- src/lib/components/session/HostView.svelte | 36 +++--- .../session/[id]/action/[action]/+server.ts | 5 - src/routes/dashboard/+page.svelte | 120 +++++++++++++++++- 3 files changed, 139 insertions(+), 22 deletions(-) diff --git a/src/lib/components/session/HostView.svelte b/src/lib/components/session/HostView.svelte index 8cf7fb1..50987c3 100644 --- a/src/lib/components/session/HostView.svelte +++ b/src/lib/components/session/HostView.svelte @@ -9,7 +9,15 @@ import { Alert } from 'flowbite-svelte'; import type { Group } from '$lib/schema/group'; import { onMount } from 'svelte'; - import { collection, getDocs, onSnapshot, getDoc, doc, Timestamp } from 'firebase/firestore'; + import { + collection, + getDocs, + onSnapshot, + Timestamp, + query, + where, + limit + } from 'firebase/firestore'; import { db } from '$lib/firebase'; import { writable } from 'svelte/store'; import { getUser } from '$lib/utils/getUser'; @@ -122,37 +130,33 @@ }; }); + // 生成代碼 async function genCode() { const response = await fetch(`/api/session/${$page.params.id}/action/generate-code`, { method: 'POST' }); - if (!response.ok) { const data = await response.json(); notifications.error(data.error || '無法生成代碼'); return ''; } - const data = await response.json(); return data.code.toString(); } async function getCode() { - const codeCollection = doc(db, 'temp_codes', $page.params.id); - const codeDoc = await getDoc(codeCollection); - if ( - !codeDoc.exists() || - Timestamp.now().toMillis() - codeDoc.data()?.createTime.toMillis() > 3600000 - ) { + const codeQuery = query( + collection(db, 'temp_codes'), + where('sessionId', '==', $page.params.id), + limit(1) + ); + const codeDoc = (await getDocs(codeQuery)).docs[0]; + console.log(codeDoc.data()); + + if (!codeDoc || Timestamp.now().toMillis() - codeDoc.data()?.createTime.toMillis() > 3600000) { code = await genCode(); } else { - const checkValid = doc(db, 'temp_codes', codeDoc.data()?.code.toString()); - const checkDoc = await getDoc(checkValid); - if (!checkDoc.exists() || checkDoc.data()?.sessionId !== $page.params.id) { - code = await genCode(); - } else { - code = codeDoc.data()?.code; - } + code = codeDoc.id; } } diff --git a/src/routes/api/session/[id]/action/[action]/+server.ts b/src/routes/api/session/[id]/action/[action]/+server.ts index 30aadd6..48b642a 100644 --- a/src/routes/api/session/[id]/action/[action]/+server.ts +++ b/src/routes/api/session/[id]/action/[action]/+server.ts @@ -51,11 +51,6 @@ export const POST: RequestHandler = async ({ params, locals }) => { sessionId: sessionRef.id, createTime: now }); - Codes = adminDb.collection('temp_codes').doc(sessionRef.id); - await Codes.set({ - code: code, - createTime: now - }); } return json({ code: code?.toString() }); } else { diff --git a/src/routes/dashboard/+page.svelte b/src/routes/dashboard/+page.svelte index b3ea259..97727ff 100644 --- a/src/routes/dashboard/+page.svelte +++ b/src/routes/dashboard/+page.svelte @@ -1,13 +1,26 @@ @@ -214,4 +259,77 @@ {/if} + + +
{session.task}
+Now is in {session.status} stage.
+No sessions created yet
+Create a new session with template
+{session.task}
+Now is in {session.status} stage.
+Host by: {hoster}
+No sessions joined yet
+ +