mirror of
https://github.com/randyjc/Rahoot.git
synced 2026-03-13 20:15:35 +01:00
51 lines
1.2 KiB
TypeScript
51 lines
1.2 KiB
TypeScript
"use client"
|
|
|
|
import Button from "@rahoot/web/components/Button"
|
|
import Form from "@rahoot/web/components/Form"
|
|
import Input from "@rahoot/web/components/Input"
|
|
import { useEvent, useSocket } from "@rahoot/web/contexts/socketProvider"
|
|
import { usePlayerStore } from "@rahoot/web/stores/player"
|
|
|
|
import { useRouter } from "next/navigation"
|
|
import { KeyboardEvent, useState } from "react"
|
|
|
|
const Username = () => {
|
|
const { socket } = useSocket()
|
|
const { gameId, login } = usePlayerStore()
|
|
const router = useRouter()
|
|
const [username, setUsername] = useState("")
|
|
|
|
const handleLogin = () => {
|
|
if (!gameId) {
|
|
return
|
|
}
|
|
|
|
socket?.emit("player:login", { gameId, data: { username } })
|
|
}
|
|
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
|
if (event.key === "Enter") {
|
|
handleLogin()
|
|
}
|
|
}
|
|
|
|
useEvent("game:successJoin", (gameId) => {
|
|
login(username)
|
|
|
|
router.replace(`/game/${gameId}`)
|
|
})
|
|
|
|
return (
|
|
<Form>
|
|
<Input
|
|
onChange={(e) => setUsername(e.target.value)}
|
|
onKeyDown={handleKeyDown}
|
|
placeholder="Username here"
|
|
/>
|
|
<Button onClick={handleLogin}>Submit</Button>
|
|
</Form>
|
|
)
|
|
}
|
|
|
|
export default Username
|