even more changes
parent
2c84fc74b4
commit
2ad01a11a9
@ -1,27 +1,59 @@
|
||||
import { LiveLeaderboard } from '@/components/LiveLeaderboard'
|
||||
import { SubmitAction } from '@/components/SubmitAction'
|
||||
import { ActionRegistry } from '@/components/ActionRegistry'
|
||||
import { useRef, useState } from 'preact/hooks'
|
||||
import { useEffect, useRef, useState } from 'preact/hooks'
|
||||
import type { Room } from '@/db/model'
|
||||
import { requestJSON } from '@/client/utils'
|
||||
import { computeScoreboardState } from '@/ggwp'
|
||||
import { Leaderboard } from './Leaderboard'
|
||||
|
||||
export const AdminPage = ({ room }: { room: string }) => {
|
||||
export const AdminPage = ({ roomId }: { roomId: string }) => {
|
||||
const listeners = useRef<Record<string, (teamQuestion: { team: string; question: string }) => void>>({})
|
||||
|
||||
const [room, setRoom] = useState<Room | null>(null)
|
||||
const fetchRoom = async () => {
|
||||
await requestJSON(`/api/room/${roomId}`).then(room => {
|
||||
setRoom({ id: roomId, ...room })
|
||||
})
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
fetchRoom()
|
||||
}, [])
|
||||
|
||||
if (!room) {
|
||||
return <div>Loading...</div>
|
||||
}
|
||||
|
||||
const scoreboard = computeScoreboardState(
|
||||
{
|
||||
teamIds: room.teams,
|
||||
questions: room.questions,
|
||||
},
|
||||
room.actions
|
||||
)
|
||||
|
||||
return (
|
||||
<>
|
||||
<h1>Admin</h1>
|
||||
<h2>{room}</h2>
|
||||
<LiveLeaderboard
|
||||
roomId={room}
|
||||
<h2>{room.id}</h2>
|
||||
<Leaderboard
|
||||
questions={room.questions}
|
||||
scoreboard={scoreboard}
|
||||
selectTeamQuestion={teamQuestion =>
|
||||
Object.values(listeners.current).forEach(listener => listener(teamQuestion))
|
||||
}
|
||||
/>
|
||||
|
||||
<h2>Azioni</h2>
|
||||
<SubmitAction roomId={room} onTeamQuestionIndex={(key, listener) => (listeners.current[key] = listener)} />
|
||||
<SubmitAction
|
||||
room={room}
|
||||
refreshRoom={fetchRoom}
|
||||
onTeamQuestionIndex={(key, listener) => (listeners.current[key] = listener)}
|
||||
/>
|
||||
|
||||
<h2>Registro Azioni</h2>
|
||||
<ActionRegistry roomId={room} />
|
||||
<ActionRegistry room={room} refreshRoom={fetchRoom} />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
@ -1,35 +1,17 @@
|
||||
import type { RoomData } from '@/db/model'
|
||||
import type { Room } from '@/db/model'
|
||||
import { Leaderboard } from './Leaderboard'
|
||||
import { useEffect, useState } from 'preact/hooks'
|
||||
import { requestJSON, sendJSON } from '@/client/utils'
|
||||
import { type Dispatch, type StateUpdater } from 'preact/hooks'
|
||||
import { computeScoreboardState } from '@/ggwp'
|
||||
|
||||
type Props = {
|
||||
roomId: string
|
||||
room: Room
|
||||
setRoom: Dispatch<StateUpdater<Room>>
|
||||
|
||||
selectTeamQuestion?: ({ team, question }: { team: string; question: string }) => void
|
||||
}
|
||||
|
||||
export const LiveLeaderboard = ({ roomId, selectTeamQuestion }: Props) => {
|
||||
const [room, setRoom] = useState<RoomData | null>(null)
|
||||
useEffect(() => {
|
||||
requestJSON(`/api/room/${roomId}`).then(room => {
|
||||
setRoom(room)
|
||||
})
|
||||
}, [])
|
||||
|
||||
console.log(room)
|
||||
|
||||
export const LiveLeaderboard = ({ room, selectTeamQuestion }: Props) => {
|
||||
if (!room) {
|
||||
return <div>Loading...</div>
|
||||
}
|
||||
|
||||
const scoreboard = computeScoreboardState(
|
||||
{
|
||||
teamIds: room.teams,
|
||||
questions: room.questions,
|
||||
},
|
||||
room.actions
|
||||
)
|
||||
|
||||
return <Leaderboard questions={room.questions} scoreboard={scoreboard} selectTeamQuestion={selectTeamQuestion} />
|
||||
}
|
||||
|
@ -0,0 +1,24 @@
|
||||
import { getRoom, updateRoom } from '@/db'
|
||||
import type { Action } from '@/ggwp'
|
||||
import type { APIRoute } from 'astro'
|
||||
|
||||
export const POST: APIRoute = async ({ params, request }) => {
|
||||
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 })
|
||||
}
|
||||
|
||||
const action = (await request.json()) as Action
|
||||
room.actions.push(action)
|
||||
|
||||
updateRoom(roomId, room)
|
||||
|
||||
return new Response(JSON.stringify('ok'), {
|
||||
headers: { 'content-type': 'application/json' },
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue