McCarthyism/front/components/CreateRoomForm.tsx

47 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-04-30 01:56:25 +00:00
import {useState} from 'react';
export default function CreateRoomForm({socket} : {socket: SocketIOClient.Socket}) {
const [name, setName] = useState('');
const [password, setPassword] = useState('');
const [waiting, setWaiting] = useState(false);
const [error, setError] = useState('');
return (
<>
<p>Create Room</p>
<input
type="text"
maxLength={15}
placeholder="Room name..."
value={name}
onChange={(e) => setName(e.target.value)}
disabled={waiting}
/>
<input
type="text"
maxLength={15}
placeholder="Password..."
value={password}
onChange={(e) => setPassword(e.target.value)}
disabled={waiting}
/>
<button
onClick={() => {
setWaiting(true);
socket.emit('createRoom', name, password);
socket.once('createRoomRes', (data: {success: boolean; error?: string}) => {
setWaiting(false);
if (!data.success)
setError(data.error!);
});
}}
disabled={!name || waiting}
>
Create
</button>
{waiting && 'Creating room...'}
<p style={{color: 'red'}}>{error}</p>
</>
);
};