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"; import styled from 'styled-components'; import {Button, FormCenter, FormField, FormLabel, FormInput, FormLink } from './miscellaneous/Styles' 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; `; const Register = () => { const [email, setEmail] = useState(); const [password, setPassword] = useState(); const [phonenumber, setPhonenumber] = useState(); const [username, setUsername] = useState(); const [error, setError] = useState(); const [contains8C, setContains8C] = useState(false); const { setUserData } = useContext(UserContext); const history = useHistory(); const submit = async (e) => { e.preventDefault(); try { const newUser = { username,email,phonenumber,password}; const isEmpty = !Object.values(newUser).some(x => (x !== null && x !== '')); console.log("isempty: " + isEmpty ) if(isEmpty){ setError("Not all Fields are entered") return; } await Axios.post("https://server-locaft.herokuapp.com/users/register", newUser); const loginRes = await Axios.post("https://server-locaft.herokuapp.com/users/login", { 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); } }; const validatePassword = () => { if(password.length >= 8) { setContains8C(true) setError(null) } else { setContains8C(false); setError("The Password needs to be atleast 8 characters") }; } return ( {error && ( setError(undefined)} /> )}
UserName setUsername(e.target.value)} /> Password setPassword(e.target.value)} onKeyUp={validatePassword} /> E-Mail Address setEmail(e.target.value)} /> Phone number setPhonenumber(parseInt(e.target.value, 10))} /> I agree all statements in terms of service already a member?
); } export default withRouter( Register );