This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signupForm.tsx
136 lines (130 loc) · 4.63 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
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 {
FAILED,
SUCCESS,
UNDEFINED,
}
enum Errors {
FORBIDDEN = 403,
USER_EXISTS = 409,
}
interface State {
name: string;
password: string;
status: Status;
httpCode: number;
}
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 = () => {
// 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">
<span
style={{
display:
this.state.status === Status.FAILED
? "block"
: "none",
}}
>{`${strings.signup_msg}: ${message}`}</span>
</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;