import React from "react";
import { Alert, Button, Card, Container, Form } from "react-bootstrap";
import { Link } from "react-router-dom";
import validator from "validator";
import { apiClient } from "../utils/httpClients";
export default class Signup extends React.Component {
constructor(props) {
super(props);
this.state = {
user: {
email: null,
firstName: null,
lastName: null,
phone: null,
password: null
},
alert: {
show: false,
variant: null,
headerMsg: null,
content: null
}
};
this.registerUser = this.registerUser.bind(this);
this.setUserState = this.setUserState.bind(this);
}
async registerUser(event) {
event.preventDefault(); // We need this so that the page doesn't refresh.
let formIssues = this.validateCurrentForm();
if (formIssues.length > 0) {
this.notifyUser("Oops there were issue(s)", (
{formIssues.map((issue) => {
return (
- {issue}
);
})}
), "danger");
return;
}
const res = await apiClient.post("/user", this.state.user);
console.log(res.data);
if (res.status === 201) {
this.notifyUser("Success!", You are successfully signed up! You can now go log in.
, "success");
} else if (res.status === 409) {
this.notifyUser("User exists!", This user already exists. Try logging in instead.
, "danger");
} else if (res.status === 400) {
this.notifyUser("There were errors in the submitted info.", Double check to see if everything is inputted is valid.
, "danger");
} else {
this.notifyUser("Error", Internal server error. Please try again later.
, "danger");
}
}
validateCurrentForm() {
console.log(this.state);
let formIssues = [];
if (!validator.isEmail(this.state.user.email)) {
formIssues.push("The email submitted is invalid.");
}
if (this.state.user.password.length < 8) {
formIssues.push("The password submitted must have a minimum length of 8 characters.");
}
return formIssues;
}
setUserState(event) {
this.setState((state) => {
state.user[event.target.id] = event.target.value;
return state;
});
}
notifyUser(headerMsg, content, key) {
this.setState((state) => {
state.alert.show = true;
state.alert.headerMsg = headerMsg;
state.alert.content = content;
state.alert.key = key;
return state;
});
}
componentDidMount() {
if (this.context.user) {
this.context.navigate("/dashboard");
}
}
render() {
return (
this.setState((state) => { state.alert.show = false; return state; })} dismissible>
{this.state.alert.headerMsg}
{this.state.alert.content}
Login
Welcome to Sports Matcher!
First name
Last name
E-mail
Phone number
Password
);
}
}