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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
import React from "react";
import { useHistory } from "react-router";
import { useTranslation } from "react-i18next";
import { Form } from "react-bootstrap";
import { useUser, userService } from "@/services/user";
import AppBar from "../common/AppBar";
import LoadingButton from "../common/LoadingButton";
const LoginPage: React.FC = (_) => {
const { t } = useTranslation();
const history = useHistory();
const [username, setUsername] = React.useState<string>("");
const [usernameDirty, setUsernameDirty] = React.useState<boolean>(false);
const [password, setPassword] = React.useState<string>("");
const [passwordDirty, setPasswordDirty] = React.useState<boolean>(false);
const [rememberMe, setRememberMe] = React.useState<boolean>(true);
const [process, setProcess] = React.useState<boolean>(false);
const [error, setError] = React.useState<string | null>(null);
const user = useUser();
React.useEffect(() => {
if (user != null) {
const id = setTimeout(() => history.push("/"), 3000);
return () => {
clearTimeout(id);
};
}
}, [history, user]);
if (user != null) {
return (
<>
<AppBar />
<p className="mt-appbar">{t("login.alreadyLogin")}</p>
</>
);
}
function onSubmit(event: React.SyntheticEvent): void {
if (username === "" || password === "") {
setUsernameDirty(true);
setPasswordDirty(true);
return;
}
setProcess(true);
userService
.login(
{
username: username,
password: password,
},
rememberMe
)
.then(
() => {
if (history.length === 0) {
history.push("/");
} else {
history.goBack();
}
},
(e: Error) => {
setProcess(false);
setError(e.message);
}
);
event.preventDefault();
}
return (
<div className="container login-container mt-appbar">
<h1>{t("welcome")}</h1>
<Form>
<Form.Group>
<Form.Label htmlFor="username">{t("user.username")}</Form.Label>
<Form.Control
id="username"
disabled={process}
onChange={(e) => {
setUsername(e.target.value);
setUsernameDirty(true);
}}
value={username}
isInvalid={usernameDirty && username === ""}
/>
{usernameDirty && username === "" && (
<Form.Control.Feedback type="invalid">
{t("login.emptyUsername")}
</Form.Control.Feedback>
)}
</Form.Group>
<Form.Group>
<Form.Label htmlFor="password">{t("user.password")}</Form.Label>
<Form.Control
id="password"
type="password"
disabled={process}
onChange={(e) => {
setPassword(e.target.value);
setPasswordDirty(true);
}}
value={password}
isInvalid={passwordDirty && password === ""}
/>
{passwordDirty && password === "" && (
<Form.Control.Feedback type="invalid">
{t("login.emptyPassword")}
</Form.Control.Feedback>
)}
</Form.Group>
<Form.Group>
<Form.Check<"input">
id="remember-me"
type="checkbox"
checked={rememberMe}
onChange={(e) => {
setRememberMe(e.target.checked);
}}
label={t("user.rememberMe")}
/>
</Form.Group>
{error ? <p className="text-danger">{t(error)}</p> : null}
<div className="text-right">
<LoadingButton
loading={process}
variant="primary"
onClick={onSubmit}
disabled={username === "" || password === "" ? true : undefined}
>
{t("user.login")}
</LoadingButton>
</div>
</Form>
</div>
);
};
export default LoginPage;
|