Login.tsx
1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import React, { useCallback, useContext, useState } from 'react';
import { useHistory } from 'react-router';
import { Main } from '../components/common/Main';
import { MessageResponse, MessageType, RawMessage } from '../components/common/types';
import SocketContext from '../contexts/SocketContext';
export const Login: React.FC = () => {
const history = useHistory();
const socket = useContext(SocketContext);
const [ username, setUsername ] = useState("");
const login = useCallback(() => {
const rawMessage: RawMessage = {
type: MessageType.LOGIN,
message: { username }
}
socket.emit('msg', rawMessage, (response : MessageResponse<undefined>) => {
if (response.ok) {
history.push('/rooms');
} else {
console.error('login error!'); // TODO: 팝업 에러?
}
});
}, [username]);
return (
<Main>
<div className="mt-auto flex flex-col items-center">
<img className="m-7" src="./logo192.png"/>
<div>
<input type="text"
placeholder="Username"
className="px-3 py-2 bg-white
placeholder-gray-400 text-gray-700 text-sm
rounded shadow outline-none focus:outline-none"
value={username}
onChange={e => setUsername(e.target.value)}
/>
<button className="bg-green-500 active:bg-green-600
text-white font-bold uppercase text-sm
px-5 py-2 ml-3 rounded shadow
outline-none focus:outline-none hover:shadow-md
ease-linear transition-all duration-100"
type="button"
onClick={() => login()}>Login</button>
</div>
</div>
</Main>
)
}