This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
/
signupForm.tsx
161 lines (154 loc) · 5.36 KB
/
signupForm.tsx
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import React from "react";
import { RouteComponentProps as RCP } from "react-router-dom";
import { AxiosError, AxiosResponse } from "axios";
import req from "../axios";
import strings from "./assets/locales";
import "./styles/signupForm.scss";
enum Status {
UNDEFINED,
FAILED,
SUCCESS,
IN_PROCESS,
}
enum Errors {
FORBIDDEN = 403,
USER_EXISTS = 409,
}
interface State {
name: string;
password: string;
status: Status;
httpCode: number;
}
interface MsgProps {
message: string;
code: Status;
matchCode: Status;
}
/**
* Used for displaying messages on matching status code.
*/
const Msg: React.FC<MsgProps> = (props) => (
<span
style={{ display: props.code === props.matchCode ? "block" : "none" }}
>
{props.message}
</span>
);
class SignupForm extends React.PureComponent<RCP, State> {
state = {
name: "",
password: "",
status: Status.UNDEFINED,
httpCode: 0,
};
componentDidUpdate = () => {
if (this.state.status === Status.SUCCESS) {
setTimeout(() => {
this.props.history.push("/");
}, 5000);
}
};
signup = () => {
this.setState({ status: Status.IN_PROCESS });
// prettier-ignore
req.post("/signup", {
name: this.state.name,
password: this.state.password,
})
.then(
() => this.setState({ status: Status.SUCCESS }),
(e: AxiosError) => this.setState({
status: Status.FAILED,
httpCode: (e.response as AxiosResponse).status
})
);
};
goBack = () => this.props.history.push("/");
updateName = (e: React.FormEvent<HTMLInputElement>) =>
this.setState({ name: e.currentTarget.value });
updatePassword = (e: React.FormEvent<HTMLInputElement>) =>
this.setState({ password: e.currentTarget.value });
render = () => {
let message: string;
switch (this.state.httpCode) {
case Errors.FORBIDDEN:
message = strings.signup_disabled;
case Errors.USER_EXISTS:
message = strings.signup_userExists;
default:
message = strings.signup_unknownError;
}
return (
<div className="container signup_form">
<form className="signupForm">
<div className="field">
<label className="label">
{strings.login_userNameTp}
</label>
<div className="control">
<input
type="text"
autoFocus
className="input"
onChange={this.updateName}
/>
</div>
</div>
<div className="field">
<label className="label">
{strings.login_passwordTp}
</label>
<div className="control">
<input
className="input"
type="password"
onChange={this.updatePassword}
/>
</div>
</div>
<div className="level is-mobile">
<div className="level-item level-left">
<div className="control">
<input
type="button"
className="button"
value={strings.login_signupBtn}
onClick={this.signup}
/>
</div>
</div>
<div className="level-item">
<Msg
code={this.state.status}
matchCode={Status.FAILED}
message={`${strings.signup_failMsg}: ${message}`}
/>
<Msg
code={this.state.status}
matchCode={Status.IN_PROCESS}
message={strings.signup_processMsg}
/>
<Msg
code={this.state.status}
matchCode={Status.SUCCESS}
message={strings.signup_successMsg}
/>
</div>
<div className="level-item level-right">
<div className="control">
<input
type="button"
className="button"
value={strings.login_goBackBtn}
onClick={this.goBack}
/>
</div>
</div>
</div>
</form>
</div>
);
};
}
export default SignupForm;