File size: 2,280 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import React, { useState } from "react";
import { Link } from "react-router-dom";

const Register = props => {
	const [email, setEmail] = useState("");
	const [password, setPassword] = useState("");
	const [username, setUsername] = useState("");
	const [errors, setErrors] = useState([]);

	const handleSubmit = event => {
		event.preventDefault();
		if (isValid()) {
			props.register(email, password, username);
		} else {
		}
	};

	const isValid = () => {
		const emailRegex = /[a-zA-Z0-9]{4,}@[a-zA-Z0-9]{3,}\.[a-zA-Z]{1,10}/gi;
		const passwordRegex = /[a-zA-Z0-9*%#@!]{6,32}/gi;
		const userNameRegex = /^[a-z0-9_-]{3,16}$/gi;
		const errors = [];
		if (!emailRegex.exec(email)) errors.push("email");
		if (!passwordRegex.exec(password)) errors.push("password");
		if (!userNameRegex.exec(username)) errors.push("username");
		setErrors(errors);
		return errors.length === 0;
	};

	return (
		<div className="register">
			<h2>Create an account</h2>
			<form onSubmit={handleSubmit}>
				<label
					style={{ color: errors.includes("email") ? "#d72323" : "white" }}
				>
					EMAIL
				</label>
				<input
					type="email"
					autoComplete="email"
					value={email}
					onChange={e => setEmail(e.target.value)}
					style={{
						borderColor: errors.includes("email")
							? "#d72323"
							: "rgba(0,0,0,0.2)"
					}}
				/>

				<label
					style={{ color: errors.includes("username") ? "#d72323" : "white" }}
				>
					USERNAME
				</label>
				<input
					type="text"
					value={username}
					onChange={e => setUsername(e.target.value)}
					style={{
						borderColor: errors.includes("username")
							? "#d72323"
							: "rgba(0,0,0,0.2)"
					}}
				/>

				<label
					style={{ color: errors.includes("password") ? "#d72323" : "white" }}
				>
					PASSWORD
				</label>
				<input
					type="password"
					autoComplete="password"
					value={password}
					onChange={e => setPassword(e.target.value)}
					style={{
						borderColor: errors.includes("password")
							? "#d72323"
							: "rgba(0,0,0,0.2)"
					}}
				/>

				<button type="submit">Continue</button>
			</form>
			{props.error ? <div className="error">{props.error.message}</div> : ""}
			<Link to="/login">Already have an account?</Link>
		</div>
	);
};

export default Register;