This repository has been archived on 2026-07-13. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
servicepoint-tanks/tank-frontend/src/JoinForm.tsx
2024-05-06 01:20:54 +02:00

47 lines
1.5 KiB
TypeScript

import {useState} from 'react';
import {useMutation} from '@tanstack/react-query';
import {makeApiUrl} from './serverCalls';
import Button from './components/Button.tsx';
import TextInput from './components/TextInput.tsx';
import Dialog from './components/Dialog.tsx';
import './JoinForm.css';
export default function JoinForm({onDone}: {
onDone: (name: string) => void;
}) {
const postPlayer = useMutation({
retry: false,
mutationFn: async ({name}: { name: string }) => {
const url = makeApiUrl('/player');
url.searchParams.set('name', name);
const response = await fetch(url, {method: 'POST'});
if (!response.ok)
throw new Error(`${response.status} (${response.statusText}): ${await response.text()}`);
onDone((await response.json()).trim());
}
});
const [name, setName] = useState('');
const disableButtons = postPlayer.isPending || name.trim() === '';
const confirm = () => postPlayer.mutate({name});
return <Dialog className="JoinForm">
<h3> Enter your name to play </h3>
<TextInput
value={name}
placeholder="player name"
onChange={e => setName(e.target.value)}
onEnter={confirm}
/>
<Button
onClick={confirm}
disabled={disableButtons}
text="¢ INSERT COIN"/>
{postPlayer.isError && <p>{postPlayer.error.message}</p>}
</Dialog>;
}