-
Notifications
You must be signed in to change notification settings - Fork 12
/
Signup.jsx
139 lines (128 loc) · 4.97 KB
/
Signup.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
import React from 'react';
import { Link } from 'react-router-dom';
import alertifyjs from 'alertifyjs';
import { TextField, Button, Paper } from '@material-ui/core';
import {set, get, isEmpty, cloneDeep, startCase, map, includes} from 'lodash';
import APIService from '../../services/APIService';
import VerifyEmailMessage from './VerifyEmailMessage';
import PasswordFields from '../common/PasswordFields';
class Signup extends React.Component {
constructor(props) {
super(props);
this.state = {
fieldErrors: {},
serverError: null,
successMsg: null,
fields: {
username: '',
first_name: '',
last_name: '',
email: '',
password: '',
confirm_password: '',
}
}
}
setFieldValue(id, value) {
const newState = {...this.state}
set(newState, id, value)
const fieldName = get(id.split('fields.'), '1')
if(fieldName && !isEmpty(value) && get(newState.fieldErrors, fieldName))
newState.fieldErrors[fieldName] = null
this.setState(newState)
}
isPasswordValid() {
const { password } = this.state.fields;
return Boolean(password && password.length >=8 && password.match(new RegExp(/[a-zA-Z]/g)))
}
onSubmit = event => {
event.preventDefault();
event.stopPropagation();
const form = document.getElementsByTagName('form')[0];
form.reportValidity()
const isFormValid = form.checkValidity()
const fields = cloneDeep(this.state.fields);
const isConfirmPasswordSameAsPassword = fields.confirm_password === fields.password;
if(!isConfirmPasswordSameAsPassword) {
this.setState({serverError: ['Password and Confirm Password must match.']})
return
}
if(!this.isPasswordValid()) {
this.setState({serverError: ['Invalid Password']})
return
}
if(isFormValid && isConfirmPasswordSameAsPassword) {
this.setState({serverError: null}, () => {
APIService.users().signup().post(fields).then(response => {
if(get(response, 'status') === 201) {
alertifyjs.success('Successfully created.')
this.setState({successMsg: response.data.detail})
} else {
this.setState({fieldErrors: response})
}
})
})
}
}
render() {
const { fields, fieldErrors, serverError, successMsg } = this.state;
return (
<div className='col-md-12' style={{marginTop: '25px'}}>
<div className='col-md-3' />
<div className='col-md-6'>
<Paper style={{padding: '10px'}} className='login-paper'>
{
successMsg ?
<VerifyEmailMessage email={fields.email} message={successMsg} /> :
<React.Fragment>
<h1>Sign Up</h1>
{
serverError &&
<div className='col-md-12 alert-danger'>
<ul> {map(serverError, error => (<li key={error}>{error}</li>))} </ul>
</div>
}
<div className='col-md-12 no-side-padding'>
<form>
{
map(['first_name', 'last_name', 'username', 'email'], (attr, index) => (
<div style={index !== 0 ? {marginTop: '10px'} : {}} key={attr}>
<TextField
required
error={Boolean(get(fieldErrors, attr))}
helperText={get(fieldErrors, `${attr}.0`)}
id={`fields.${attr}`}
label={startCase(attr)}
variant="outlined"
onChange={event => this.setFieldValue(`fields.${attr}`, event.target.value)}
type={attr === "email" ? 'email' : (includes(['password', 'confirm_password'], attr) ? 'password' : 'text') }
fullWidth
/>
</div>
))
}
<PasswordFields
onChange={event => this.setFieldValue(event.target.id, event.target.value)}
passwordErrors={get(fieldErrors, 'password')}
passwordFieldId="fields.password"
confirmPasswordFieldId="fields.confirm_password"
password={fields.password}
/>
<div style={{marginTop: '20px', textAlign: 'center', marginBottom: '20px'}}>
<Button onClick={this.onSubmit} type='submit' color='primary' variant='contained'>Sign Up</Button>
<div style={{marginTop: '15px'}}>
Already have an account? <Link to="/accounts/login">Sign In</Link>
</div>
</div>
</form>
</div>
</React.Fragment>
}
</Paper>
</div>
<div className='col-md-3' />
</div>
)
}
}
export default Signup;