From e0780f421736e7eb45135c91ec6ecf3f7a0236d6 Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Fri, 22 Nov 2024 16:01:02 +0100 Subject: [PATCH] some changes --- src/client/utils.ts | 19 ++++++++++++++ src/components/ActionRegistry.tsx | 4 +-- src/components/AdminPage.tsx | 19 +++++++++++++- src/components/SubmitAction.tsx | 4 +-- src/db/index.ts | 43 +++++++++++++++++++++++-------- src/pages/api/room/[id]/index.ts | 31 +++++++++++++++++++++- src/styles.css | 5 ++++ 7 files changed, 108 insertions(+), 17 deletions(-) diff --git a/src/client/utils.ts b/src/client/utils.ts index 7c833cd..36f3386 100644 --- a/src/client/utils.ts +++ b/src/client/utils.ts @@ -27,3 +27,22 @@ export async function sendJSON(url: string, data: any): Promise> { + try { + const res = await fetch(url, { + method: 'DELETE', + credentials: 'include', + }) + + if (!res.ok) { + console.error(res) + return { status: 'error', value: await res.text() } + } + + return { status: 'ok', value: await res.json() } + } catch (e) { + console.error(e) + return { status: 'error', value: e!.toString() } + } +} diff --git a/src/components/ActionRegistry.tsx b/src/components/ActionRegistry.tsx index d4eed9b..c37d82b 100644 --- a/src/components/ActionRegistry.tsx +++ b/src/components/ActionRegistry.tsx @@ -47,7 +47,7 @@ const ActionCardAnswer = ({ action, ...props }: { action: ActionAnswer } & Actio
schedule
- {action.timestamp && format(new Date(action.timestamp), 'dd/MM/yyyy HH:mm:ss')} + {action.timestamp && format(new Date(action.timestamp), 'HH:mm:ss')} {action.timestamp && ' ' + format(new Date(action.timestamp), 'SSS')} @@ -79,7 +79,7 @@ const ActionCardJolly = ({ action, ...props }: { action: ActionJolly } & ActionC
schedule
- {action.timestamp && format(new Date(action.timestamp), 'dd/MM/yyyy HH:mm:ss')} + {action.timestamp && format(new Date(action.timestamp), 'HH:mm:ss')} {action.timestamp && ' ' + format(new Date(action.timestamp), 'SSS')} diff --git a/src/components/AdminPage.tsx b/src/components/AdminPage.tsx index 8f311be..68441aa 100644 --- a/src/components/AdminPage.tsx +++ b/src/components/AdminPage.tsx @@ -2,7 +2,7 @@ import { SubmitAction } from '@/components/SubmitAction' import { ActionRegistry } from '@/components/ActionRegistry' import { useEffect, useRef, useState } from 'preact/hooks' import type { Room } from '@/db/model' -import { requestJSON } from '@/client/utils' +import { requestJSON, sendDelete, sendJSON } from '@/client/utils' import { computeScoreboardState } from '@/ggwp' import { Leaderboard } from './Leaderboard' @@ -86,6 +86,23 @@ export const AdminPage = ({ roomId }: { roomId: string }) => { />

Registro Azioni

+

Zona Pericolosa

+
+ +
) } diff --git a/src/components/SubmitAction.tsx b/src/components/SubmitAction.tsx index e27908d..844faad 100644 --- a/src/components/SubmitAction.tsx +++ b/src/components/SubmitAction.tsx @@ -26,7 +26,7 @@ export const SubmitActionAnswer = ({ }) return ( -
+

Invia Risposta

( + ` + INSERT INTO deleted_rooms (id, deleted_at) + VALUES (?, ?); + ` + ).run(id, new Date().toISOString()) +} + export function getRoom(id: string): RoomData | null { const row = db .prepare< @@ -77,6 +88,9 @@ export function getRoom(id: string): RoomData | null { } >('SELECT * FROM rooms WHERE id = ?') .get(id) + + console.log(row) + if (!row) { return null } @@ -108,7 +122,14 @@ export function getRooms(): Room[] { id: string data: string } - >('SELECT id, data FROM rooms') + >( + ` + SELECT rooms.id, rooms.data + FROM rooms + LEFT JOIN deleted_rooms ON rooms.id = deleted_rooms.id + WHERE deleted_rooms.id IS NULL + ` + ) .all() return rows.map(row => ({ diff --git a/src/pages/api/room/[id]/index.ts b/src/pages/api/room/[id]/index.ts index 192beec..085ef4b 100644 --- a/src/pages/api/room/[id]/index.ts +++ b/src/pages/api/room/[id]/index.ts @@ -1,4 +1,4 @@ -import { getRoom, updateRoom } from '@/db' +import { deleteRoom, getRoom, updateRoom } from '@/db' import { addRoomUpdateListener, removeRoomUpdateListener } from '@/db/events' import type { RoomData } from '@/db/model' import { getSession } from '@/db/sessions' @@ -88,3 +88,32 @@ export const POST: APIRoute = async ({ params, request, cookies }) => { headers: { 'content-type': 'application/json' }, }) } + +export const DELETE: APIRoute = async ({ params, cookies }) => { + const { id: roomId } = params + if (!roomId) { + return new Response('Invalid room id', { status: 400 }) + } + + const room = getRoom(roomId) + if (!room) { + return new Response('Room not found', { status: 404 }) + } + + // check auth + const sid = cookies.get('sid') + if (!sid) { + return new Response('Unauthorized', { status: 401 }) + } + + const sessionRoom = getSession(sid.value) + if (sessionRoom !== roomId) { + return new Response('Unauthorized', { status: 401 }) + } + + deleteRoom(roomId) + + return new Response(JSON.stringify('ok'), { + headers: { 'content-type': 'application/json' }, + }) +} diff --git a/src/styles.css b/src/styles.css index c8321ce..159110e 100644 --- a/src/styles.css +++ b/src/styles.css @@ -28,6 +28,7 @@ img { --red: oklch(63.11% 0.2387 32.0573); --green: oklch(60.3% 0.3 134.48); --orange: oklch(76.96% 0.1692 67.6368); + --gold: oklch(from royalblue calc(l + 0.1) calc(c - 0.025) 90); } body { @@ -169,6 +170,10 @@ a { --tint: var(--orange); } +.tint-gold { + --tint: var(--gold); +} + .stack-v { display: flex; flex-direction: column;