Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Display error messages on signup fail
Browse files Browse the repository at this point in the history
  • Loading branch information
tdemin committed Nov 10, 2019
1 parent 22141db commit 0a342fc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/views/assets/locales.ts
Expand Up @@ -8,6 +8,10 @@ export default new LocalizedStrings({
login_userNameTp: "User name",
login_passwordTp: "Password",
login_wrongPassTp: "Login failed. Check username and password.",
signup_msg: "Signup failed",
signup_unknownError: "Unknown error",
signup_disabled: "Signup disabled",
signup_userExists: "User with this name exists",
login_signupBtn: "Sign up",
login_goBackBtn: "Go back",
main_loggedInMsg: "Logged in as",
Expand Down
31 changes: 30 additions & 1 deletion src/views/signupForm.tsx
@@ -1,5 +1,6 @@
import React from "react";
import { RouteComponentProps as RCP } from "react-router-dom";
import { AxiosError, AxiosResponse } from "axios";

import req from "../axios";

Expand All @@ -12,17 +13,23 @@ enum Status {
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) {
Expand All @@ -39,7 +46,10 @@ class SignupForm extends React.PureComponent<RCP, State> {
})
.then(
() => this.setState({ status: Status.SUCCESS }),
() => this.setState({ status: Status.FAILED })
(e: AxiosError) => this.setState({
status: Status.FAILED,
httpCode: (e.response as AxiosResponse).status
})
);
};
goBack = () => this.props.history.push("/");
Expand All @@ -48,6 +58,15 @@ class SignupForm extends React.PureComponent<RCP, State> {
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">
Expand Down Expand Up @@ -87,6 +106,16 @@ class SignupForm extends React.PureComponent<RCP, State> {
/>
</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
Expand Down

0 comments on commit 0a342fc

Please sign in to comment.