LoginContainer.tsx
1.63 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React, { useState, useEffect } from 'react';
import { RouteComponentProps } from 'react-router-dom';
import { useRecoilState } from 'recoil';
import * as recoilUtil from '../../util/recoilUtil';
import LoginPresenter from './LoginPresenter';
import { authApi } from '../../api';
type LoginProps = RouteComponentProps
const LoginContainer = (props : LoginProps) => {
const [loginForm, setLoginForm] = useState({
userId : '',
password : '',
});
const [token, setToken] = useRecoilState(recoilUtil.token);
const onSetUserId = (e : React.ChangeEvent<HTMLInputElement>) => {
setLoginForm({
...loginForm,
userId : e.target.value,
});
};
const onSetPassword = (e : React.ChangeEvent<HTMLInputElement>) => {
setLoginForm({
...loginForm,
password : e.target.value,
});
};
const onLogin = async () => {
const data : FormData = new FormData();
data.append('userId', loginForm.userId);
data.append('password', loginForm.password);
try {
const result : any = await authApi.login(data);
if(result.token) {
setToken(result.token);
props.history.push('/');
}
} catch(e) {
console.log(e);
}
};
useEffect(() => {
console.log('loginPage');
}, []);
return (
<LoginPresenter
loginForm = {loginForm}
onSetUserId = {onSetUserId}
onSetPassword = {onSetPassword}
onLogin = {onLogin}
/>
)
};
export default LoginContainer;