| author | ymh <ymh.work@gmail.com> |
| Tue, 29 Mar 2022 11:23:56 +0200 | |
| changeset 211 | 244a90638e80 |
| parent 199 | c78d579f4b55 |
| permissions | -rw-r--r-- |
| 89 | 1 |
import React, { Component } from 'react'; |
2 |
import { connect } from 'react-redux'; |
|
3 |
import { bindActionCreators } from 'redux'; |
|
4 |
import * as authActions from '../actions/authActions'; |
|
| 199 | 5 |
import { Trans } from 'react-i18next'; |
6 |
import * as R from 'ramda'; |
|
| 89 | 7 |
|
8 |
class Register extends Component { |
|
9 |
||
| 199 | 10 |
constructor(props) { |
11 |
super(props); |
|
12 |
||
13 |
this.state = { |
|
14 |
username: '', |
|
15 |
email: '', |
|
16 |
password1: '', |
|
17 |
password2: '' |
|
18 |
} |
|
19 |
||
20 |
||
21 |
} |
|
22 |
||
| 89 | 23 |
register = () => { |
| 199 | 24 |
const { |
25 |
username, |
|
26 |
email, |
|
27 |
password1, |
|
28 |
password2 } = this.state; |
|
| 89 | 29 |
|
30 |
this.props.authActions.registerSubmit(username, email, password1, password2); |
|
31 |
} |
|
32 |
||
| 199 | 33 |
handleChange = (event) => { |
34 |
const newState = {}; |
|
35 |
newState[event.target.name] = event.target.value; |
|
36 |
this.setState(newState); |
|
37 |
} |
|
38 |
||
| 94 | 39 |
submit = (e) => { |
40 |
e.preventDefault(); |
|
41 |
||
42 |
this.register(); |
|
43 |
} |
|
44 |
||
| 89 | 45 |
onClickLogin = (e) => { |
46 |
e.preventDefault(); |
|
47 |
this.props.history.push('/login'); |
|
48 |
} |
|
49 |
||
50 |
renderErrorMessage(errorMessages, fieldname) { |
|
| 199 | 51 |
if (errorMessages && fieldname in errorMessages) { |
52 |
return errorMessages[fieldname].map((message, key) => |
|
53 |
<p key={ key } className="form-text alert alert-danger mt-4" role="alert" >{ message }</p> |
|
| 89 | 54 |
); |
55 |
} |
|
56 |
} |
|
57 |
||
| 199 | 58 |
renderNonFieldErrors(errorMessages) { |
59 |
||
60 |
if (errorMessages && errorMessages.error) { |
|
61 |
return ( |
|
62 |
<div className="alert alert-danger mt-4" role="alert"> |
|
63 |
<Trans i18nKey="login.login_error">Unable to log in.</Trans> |
|
64 |
</div> |
|
65 |
) |
|
66 |
} |
|
67 |
||
68 |
const errors = R.reduce( |
|
69 |
(acc, p) => R.concat(acc, R.ifElse(Array.isArray, R.identity, v => [v,])(R.pathOr([], ['data', p], errorMessages))), |
|
70 |
[], |
|
71 |
['non_field_errors', 'detail'] |
|
72 |
); |
|
73 |
if (errors && errors.length > 0 ) { |
|
74 |
return ( |
|
75 |
<div className="alert alert-danger mt-4" role="alert"> |
|
76 |
{ errors.map((message, key) => |
|
77 |
<p key={ key }><Trans i18nKey="register.register_error">{ message }</Trans></p> |
|
78 |
) } |
|
79 |
</div> |
|
80 |
) |
|
81 |
} |
|
82 |
||
83 |
} |
|
84 |
||
85 |
||
| 89 | 86 |
render() { |
87 |
||
| 199 | 88 |
const errorMessages = this.props.register.error ? this.props.register.errorMessages : false ; |
| 89 | 89 |
|
90 |
return ( |
|
91 |
<div> |
|
| 151 | 92 |
{/* <Navbar history={this.props.history} /> */} |
|
143
cfcbf4bc66f1
Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents:
129
diff
changeset
|
93 |
<div className="container-fluid"> |
|
cfcbf4bc66f1
Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents:
129
diff
changeset
|
94 |
<div className="row"> |
| 151 | 95 |
<div className="col-lg-6 offset-md-3"> |
|
143
cfcbf4bc66f1
Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents:
129
diff
changeset
|
96 |
<div className="panel-login panel panel-default"> |
|
154
a28361bda28c
Adapt all css classes with Bootstrap 4 Utilities
salimr <riwad.salim@yahoo.fr>
parents:
151
diff
changeset
|
97 |
<div className="card-header bg-secondary border-0 mt-5 pt-5"> |
|
a28361bda28c
Adapt all css classes with Bootstrap 4 Utilities
salimr <riwad.salim@yahoo.fr>
parents:
151
diff
changeset
|
98 |
<h4 className="text-center card-title font-weight-bold text-lg" onClick={this.onClickHome}>IRI Notes</h4> |
|
a28361bda28c
Adapt all css classes with Bootstrap 4 Utilities
salimr <riwad.salim@yahoo.fr>
parents:
151
diff
changeset
|
99 |
<form className="pt-3 ml-5 pl-5" onSubmit={this.submit}> |
| 199 | 100 |
<div className="form-group mb-2 ml-3 w-75"> |
101 |
<label className="col-form-label text-primary font-weight-bold mt-2" htmlFor="username"><Trans i18nKey="common.username">Nom d'utilisateur</Trans></label> |
|
102 |
<input className="form-control bg-irinotes-form border-0 text-muted" type="text" onChange={this.handleChange} value={this.state.username} name="username" /> |
|
103 |
{ errorMessages && this.renderErrorMessage(errorMessages.data, 'username') } |
|
|
143
cfcbf4bc66f1
Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents:
129
diff
changeset
|
104 |
</div> |
| 199 | 105 |
<div className="form-group mb-2 ml-3 w-75"> |
106 |
<label className="col-form-label text-primary font-weight-bold mt-2" htmlFor="email"><Trans i18nKey="common.email">Email</Trans></label> |
|
107 |
<input className="form-control bg-irinotes-form border-0 text-muted" type="email" onChange={this.handleChange} value={this.state.email} name="email" /> |
|
108 |
{ errorMessages && this.renderErrorMessage(errorMessages.data, 'email') } |
|
|
143
cfcbf4bc66f1
Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents:
129
diff
changeset
|
109 |
</div> |
| 199 | 110 |
<div className="form-group mb-2 ml-3 w-75"> |
111 |
<label className="col-form-label text-primary font-weight-bold mt-2" htmlFor="password1"><Trans i18nKey="common.password">Mot de passe</Trans></label> |
|
112 |
<input className="form-control bg-irinotes-form border-0 text-muted" type="password" onChange={this.handleChange} value={this.state.password1} name="password1" /> |
|
113 |
{ errorMessages && this.renderErrorMessage(errorMessages.data, 'password1') } |
|
|
143
cfcbf4bc66f1
Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents:
129
diff
changeset
|
114 |
</div> |
| 199 | 115 |
<div className="form-group mb-2 ml-3 w-75"> |
116 |
<label className="col-form-label text-primary font-weight-bold mt-2" htmlFor="password2"><Trans i18nKey="register.password_confirmation">Confirmer le mot de passe</Trans></label> |
|
117 |
<input className="form-control bg-irinotes-form border-0 text-muted" type="password" onChange={this.handleChange} value={this.state.password2} name="password2" /> |
|
118 |
{ errorMessages && this.renderErrorMessage(errorMessages.data, 'password2') } |
|
|
143
cfcbf4bc66f1
Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents:
129
diff
changeset
|
119 |
</div> |
| 199 | 120 |
{ this.renderNonFieldErrors(errorMessages) } |
|
154
a28361bda28c
Adapt all css classes with Bootstrap 4 Utilities
salimr <riwad.salim@yahoo.fr>
parents:
151
diff
changeset
|
121 |
<div className="text-center mr-5 pr-5"> |
| 199 | 122 |
<button type="submit" onClick={this.submit} className="btn btn-primary btn-lg text-secondary font-weight-bold mt-3"><Trans i18nKey="register.register">S'inscrire</Trans></button> |
|
154
a28361bda28c
Adapt all css classes with Bootstrap 4 Utilities
salimr <riwad.salim@yahoo.fr>
parents:
151
diff
changeset
|
123 |
</div> |
|
143
cfcbf4bc66f1
Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents:
129
diff
changeset
|
124 |
</form> |
|
cfcbf4bc66f1
Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents:
129
diff
changeset
|
125 |
</div> |
|
cfcbf4bc66f1
Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents:
129
diff
changeset
|
126 |
</div> |
| 89 | 127 |
<p className="text-center"> |
| 199 | 128 |
<a className="text-muted" href="/login" onClick={ this.onClickLogin }><Trans i18nKey="register.already_registered">Déjà inscrit ? Se connecter</Trans>.</a> |
| 89 | 129 |
</p> |
|
143
cfcbf4bc66f1
Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents:
129
diff
changeset
|
130 |
</div> |
|
cfcbf4bc66f1
Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents:
129
diff
changeset
|
131 |
</div> |
|
cfcbf4bc66f1
Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents:
129
diff
changeset
|
132 |
</div> |
| 89 | 133 |
</div> |
134 |
); |
|
135 |
} |
|
136 |
} |
|
137 |
||
138 |
function mapStateToProps(state, props) { |
|
139 |
return { |
|
|
168
ea92f4fe783d
- move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents:
161
diff
changeset
|
140 |
register: state.register |
| 89 | 141 |
}; |
142 |
} |
|
143 |
||
144 |
function mapDispatchToProps(dispatch) { |
|
145 |
return { |
|
146 |
authActions: bindActionCreators(authActions, dispatch) |
|
147 |
} |
|
148 |
} |
|
149 |
||
150 |
export default connect(mapStateToProps, mapDispatchToProps)(Register); |