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
|
import React from "react";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { HttpBadRequestError } from "@/http/common";
import { getHttpTokenClient } from "@/http/token";
import { userService, useUser } from "@/services/user";
import { LoadingButton } from "../common/button";
import InputPanel, {
hasError,
InputPanelError,
} from "../common/input/InputPanel";
import "./index.css";
const validate = (values: string[], dirties: boolean[]): InputPanelError => {
const e: InputPanelError = {};
if (dirties[0] && values[0].length === 0) {
e[0] = "register.error.usernameEmpty";
}
if (dirties[1] && values[1].length === 0) {
e[1] = "register.error.passwordEmpty";
}
if (dirties[2] && values[2] !== values[1]) {
e[2] = "register.error.confirmPasswordWrong";
}
if (dirties[3] && values[3].length === 0) {
e[3] = "register.error.registerCodeEmpty";
}
return e;
};
const RegisterPage: React.FC = () => {
const navigate = useNavigate();
const { t } = useTranslation();
const [username, setUsername] = React.useState<string>("");
const [password, setPassword] = React.useState<string>("");
const [confirmPassword, setConfirmPassword] = React.useState<string>("");
const [registerCode, setRegisterCode] = React.useState<string>("");
const [dirty, setDirty] = React.useState<boolean[]>(new Array(4).fill(false));
const [process, setProcess] = React.useState<boolean>(false);
const [inputError, setInputError] = React.useState<InputPanelError>();
const [resultError, setResultError] = React.useState<string | null>(null);
const user = useUser();
React.useEffect(() => {
if (user != null) {
navigate("/");
}
});
return (
<div className="container register-page">
<InputPanel
scheme={[
{
type: "text",
label: "register.username",
},
{
type: "text",
label: "register.password",
password: true,
},
{
type: "text",
label: "register.confirmPassword",
password: true,
},
{ type: "text", label: "register.registerCode" },
]}
values={[username, password, confirmPassword, registerCode]}
onChange={(values, index) => {
setUsername(values[0]);
setPassword(values[1]);
setConfirmPassword(values[2]);
setRegisterCode(values[3]);
const newDirty = dirty.slice();
newDirty[index] = true;
setDirty(newDirty);
setInputError(validate(values, newDirty));
}}
error={inputError}
disable={process}
/>
{resultError && <div className="cru-color-danger">{t(resultError)}</div>}
<LoadingButton
text="register.register"
loading={process}
disabled={hasError(inputError)}
onClick={() => {
const newDirty = dirty.slice().fill(true);
setDirty(newDirty);
const e = validate(
[username, password, confirmPassword, registerCode],
newDirty
);
if (hasError(e)) {
setInputError(e);
} else {
setProcess(true);
void getHttpTokenClient()
.register({
username,
password,
registerCode,
})
.then(
() => {
void userService
.login({ username, password }, true)
.then(() => {
navigate("/");
});
},
(error) => {
if (error instanceof HttpBadRequestError) {
setResultError("register.error.registerCodeInvalid");
} else {
setResultError("error.network");
}
setProcess(false);
}
);
}
}}
/>
</div>
);
};
export default RegisterPage;
|