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();
|
|
|
|
|
|
|
|
const { setUserData } = useContext(UserContext);
|
|
|
|
const history = useHistory();
|
|
|
|
|
|
|
|
const submit = async (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
try {
|
|
|
|
const newUser = { username,email,phonenumber,password};
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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>
|
|
|
|
<FormInput
|
2021-03-17 18:44:15 -07:00
|
|
|
type="text"
|
|
|
|
id="name"
|
|
|
|
placeholder="Enter your full name"
|
|
|
|
onChange= { (e) => setUsername(e.target.value)}
|
|
|
|
/>
|
2021-03-28 11:24:40 -07:00
|
|
|
</FormField>
|
|
|
|
<FormField>
|
|
|
|
<FormLabel htmlFor="password">Password</FormLabel>
|
|
|
|
<FormInput
|
2021-03-17 18:44:15 -07:00
|
|
|
type="password"
|
|
|
|
id="password"
|
|
|
|
className="FormField__Input"
|
|
|
|
placeholder="Enter your password"
|
|
|
|
onChange= { (e) => setPassword(e.target.value)}
|
|
|
|
/>
|
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"
|
|
|
|
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>
|
|
|
|
<CheckBox type="checkbox" name="hasAgreed" required="true" /> I agree all statements in <a href="/" className="FormField__TermsLink">terms of service</a>
|
|
|
|
</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 );
|