-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCredentials.jsx
155 lines (145 loc) · 4.44 KB
/
Credentials.jsx
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import React from 'react';
import { Button, FormGroup, FormControl, FormLabel } from 'react-bootstrap';
const validEmailRegex = RegExp(/^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i);
class Credentials extends React.Component {
constructor(props) {
super(props)
this.state = {
errors: {}
};
this.handleChange = this.handleChange.bind(this);
}
saveAndContinue = (e) => {
e.preventDefault()
if (this.props.validateForm(this.state.errors)) {
this.setState({
errors: {}
})
this.props.nextStep()
}
}
handleChange(event) {
event.preventDefault();
const { name, value } = event.target;
this.props.handleChange(event);
let errors = this.state.errors;
switch (name) {
case 'first_name':
if (value.length == 0) {
errors.first_name = 'First name cannot be blank!';
} else {
delete errors.first_name;
}
break;
case 'last_name':
if (value.length == 0) {
errors.last_name = 'Last name cannot be blank!';
} else {
delete errors.last_name;
}
break;
case 'email':
if (!validEmailRegex.test(value)) {
errors.email = 'Email is not valid!';
} else {
delete errors.email;
}
break;
case 'password':
if (value.length < 8) {
errors.password = 'Password must be at least 8 characters long!';
} else {
delete errors.password;
}
break;
case 'confirm_password':
if (value != this.props.values['password']) {
errors.confirm_password = 'Passwords must match!';
} else {
delete errors.confirm_password;
}
break;
default:
break;
}
}
render() {
const { values } = this.props;
const { errors } = this.state;
const isButtonEnabled = Object.keys(errors).length == 0 &&
values.first_name.length > 0 &&
values.last_name.length > 0 &&
values.email.length > 0 &&
values.password.length > 0 &&
values.confirm_password.length > 0;
let alert;
if (Object.keys(errors).length > 0) {
alert =
<div className="alert alert-danger" role="alert">
{Object.keys(errors).map((key, index) => (
<div key={index}>{key}: {errors[key]}</div>
))}
</div>;
}
return (
<div className="signup">
<h1>Sign Up</h1>
<form>
{alert}
<FormGroup controlId="first_name" bsSize="large">
<FormLabel>First Name</FormLabel>
<FormControl
autoFocus
name="first_name"
type="name"
defaultValue={values.first_name}
onChange={this.handleChange}
/>
</FormGroup>
<FormGroup controlId="last_name" bsSize="large">
<FormLabel>Last Name</FormLabel>
<FormControl
name="last_name"
type="name"
defaultValue={values.last_name}
onChange={this.handleChange}
/>
</FormGroup>
<FormGroup controlId="email" bsSize="large">
<FormLabel>Email</FormLabel>
<FormControl
name="email"
type="email"
defaultValue={values.email}
onChange={this.handleChange}
/>
</FormGroup>
<FormGroup controlId="password" bsSize="large">
<FormLabel>Password</FormLabel>
<FormControl
name="password"
type="password"
defaultValue={values.password}
onChange={this.handleChange}
/>
</FormGroup>
<FormGroup controlId="confirm_password" bsSize="large">
<FormLabel>Confirm Password</FormLabel>
<FormControl
name="confirm_password"
type="password"
defaultValue={values.confirm_password}
onChange={this.handleChange}
/>
</FormGroup>
<Button
block
disabled={!isButtonEnabled}
onClick={this.saveAndContinue}
bsSize="large">Continue</Button>
</form>
</div>
);
}
}
export default Credentials