diff --git a/tank-frontend/src/JoinForm.css b/tank-frontend/src/JoinForm.css index a4bad2b..d7bef0d 100644 --- a/tank-frontend/src/JoinForm.css +++ b/tank-frontend/src/JoinForm.css @@ -13,5 +13,4 @@ .JoinElems { padding: 8px 8px; margin: 8px 8px; - color: rgb(255, 255, 255); } \ No newline at end of file diff --git a/tank-frontend/src/JoinForm.tsx b/tank-frontend/src/JoinForm.tsx index be88789..a62533c 100644 --- a/tank-frontend/src/JoinForm.tsx +++ b/tank-frontend/src/JoinForm.tsx @@ -1,22 +1,6 @@ import { useEffect, useState } from 'react'; import './JoinForm.css'; - -type PlayerResponse = { - readonly name: string; - readonly id: string; -}; - -export async function fetchPlayer(name: string, options: RequestInit) { - const url = new URL(import.meta.env.VITE_TANK_PLAYER_URL); - url.searchParams.set('name', name); - - const response = await fetch(url, options); - if (!response.ok) - return null; - - const json = await response.json() as PlayerResponse; - return json.id; -} +import { PlayerResponse, postPlayer } from './serverCalls'; export default function JoinForm({ onDone }: { onDone: (id: string) => void }) { const [name, setName] = useState(''); @@ -28,12 +12,13 @@ export default function JoinForm({ onDone }: { onDone: (id: string) => void }) { return; try { - fetchPlayer(name, {}).then((value: string | null) => { - if (value) - onDone(value); - else - setClicked(false); - }); + postPlayer(name) + .then((value: PlayerResponse | null) => { + if (value) + onDone(value.id); + else + setClicked(false); + }); } catch (e) { console.log(e); alert(e); @@ -47,7 +32,7 @@ export default function JoinForm({ onDone }: { onDone: (id: string) => void }) {
Welcome and have fun!
+
Enter your name to join the game!
(); + + const refresh = () => { + getPlayer(playerId).then(setPlayer); + }; + + useEffect(() => { + const timer = setInterval(refresh, 5000); + return () => clearInterval(timer); + }); + + return+ name +
++ {player?.name} +
++ {JSON.stringify(player)} +
+ + +