Locaft/src/components/Register.js

149 lines
4.4 KiB
JavaScript
Raw Normal View History

2021-03-17 18:44:15 -07:00
import React, { useState, useContext } from "react";
import { useHistory, withRouter } from "react-router-dom";
import UserContext from "../context/UserContext";
import Axios from "axios";
import { Link } from "react-router-dom";
import ErrorNotice from "./ErrorNotice";
2021-03-28 11:24:40 -07:00
import styled from 'styled-components';
import {Button, FormCenter, FormField, FormLabel, FormInput, FormLink } from './miscellaneous/Styles'
2021-03-17 18:44:15 -07:00
2021-03-28 11:24:40 -07:00
const CheckBoxLabel = styled.label`
color: #646F7D;
font-size: .9em;
`;
const CheckBox = styled.input`
position: relative;
top: 1.5px;
`;
const TermsLink = styled.a`
color: #646F7D;
border-bottom: 1px solid #199087;
text-decoration: none;
display: inline-block;
padding-bottom: 2px;
margin-left: 5px;
`;
2021-03-17 18:44:15 -07:00
const Register = () => {
const [email, setEmail] = useState();
const [password, setPassword] = useState();
const [phonenumber, setPhonenumber] = useState();
const [username, setUsername] = useState();
const [error, setError] = useState();
2021-03-31 11:24:24 -07:00
const [contains8C, setContains8C] = useState(false);
2021-03-17 18:44:15 -07:00
const { setUserData } = useContext(UserContext);
const history = useHistory();
const submit = async (e) => {
e.preventDefault();
try {
const newUser = { username,email,phonenumber,password};
2021-03-31 11:24:24 -07:00
const isEmpty = !Object.values(newUser).some(x => (x !== null && x !== ''));
console.log("isempty: " + isEmpty )
if(isEmpty){
setError("Not all Fields are entered")
return;
}
2021-03-24 22:58:04 -07:00
await Axios.post("https://server-locaft.herokuapp.com/users/register", newUser);
const loginRes = await Axios.post("https://server-locaft.herokuapp.com/users/login", {
2021-03-17 18:44:15 -07:00
email,
password,
});
setUserData({
token: loginRes.data.token,
user: loginRes.data.user,
});
localStorage.setItem("auth-token", loginRes.data.token);
history.push("/");
} catch (err) {
return err.response.data.msg && setError(err.response.data.msg);
}
};
2021-03-31 11:24:24 -07:00
const validatePassword = () => {
if(password.length >= 8) {
setContains8C(true)
setError(null)
}
else { setContains8C(false); setError("The Password needs to be atleast 8 characters") };
}
2021-03-17 18:44:15 -07:00
return (
2021-03-28 11:24:40 -07:00
<FormCenter>
2021-03-17 18:44:15 -07:00
{error && (
<ErrorNotice message={error} clearError={() => setError(undefined)} />
)}
<form className="FormFields" onSubmit={submit}>
2021-03-28 11:24:40 -07:00
<FormField>
<FormLabel htmlFor="name">UserName</FormLabel>
2021-03-31 11:24:24 -07:00
<FormInput
type="text"
id="name"
placeholder="Enter your full name"
onChange={(e) => setUsername(e.target.value)}
2021-03-17 18:44:15 -07:00
/>
2021-03-28 11:24:40 -07:00
</FormField>
<FormField>
<FormLabel htmlFor="password">Password</FormLabel>
2021-03-31 11:24:24 -07:00
<FormInput
type="password"
id="password"
className="FormField__Input"
placeholder="Enter your password"
onChange={(e) => setPassword(e.target.value)}
onKeyUp={validatePassword}
2021-03-17 18:44:15 -07:00
/>
2021-03-28 11:24:40 -07:00
</FormField>
<FormField>
<FormLabel htmlFor="email">E-Mail Address</FormLabel>
<FormInput
2021-03-17 18:44:15 -07:00
type="email"
id="email"
className="FormField__Input"
placeholder="Enter your email"
onChange= { (e) => setEmail(e.target.value)}
/>
2021-03-28 11:24:40 -07:00
</FormField>
<FormField>
<FormLabel htmlFor="phone">Phone number</FormLabel>
<FormInput
2021-03-17 18:44:15 -07:00
type="number"
id="phonenumber"
2021-03-31 11:24:24 -07:00
className="FormField__Input"
placeholder="Enter your Phone no. (+91)"
onChange={(e) => setPhonenumber(parseInt(e.target.value, 10))}
/>
2021-03-28 11:24:40 -07:00
</FormField>
2021-03-17 18:44:15 -07:00
2021-03-28 11:24:40 -07:00
<FormField>
<CheckBoxLabel>
2021-03-31 11:24:24 -07:00
<CheckBox type="checkbox" name="hasAgreed" /> I agree all statements in <a href="/" className="FormField__TermsLink">terms of service</a>
2021-03-28 11:24:40 -07:00
</CheckBoxLabel>
</FormField>
2021-03-17 18:44:15 -07:00
2021-03-28 11:24:40 -07:00
<FormField>
<Button
type="submit"
radiuscolor="#009578"
textcolor="#009578"
hovercolor="#009578"
hovertextcolor="white"
>Sign Up</Button>
<FormLink to="/login">already a member?</FormLink>
</FormField>
2021-03-17 18:44:15 -07:00
</form>
2021-03-28 11:24:40 -07:00
</FormCenter>
2021-03-17 18:44:15 -07:00
);
}
export default withRouter( Register );