| author | ymh <ymh.work@gmail.com> |
| Thu, 03 Aug 2017 19:12:25 +0200 | |
| changeset 136 | 1a3be12af395 |
| parent 129 | d48946d164c6 |
| child 143 | cfcbf4bc66f1 |
| permissions | -rw-r--r-- |
| 89 | 1 |
import React, { Component } from 'react'; |
2 |
import { connect } from 'react-redux'; |
|
3 |
import { bindActionCreators } from 'redux'; |
|
| 90 | 4 |
import { Grid, Row, Col, Panel, FormGroup, ControlLabel, FormControl, Button, HelpBlock } from 'react-bootstrap'; |
| 89 | 5 |
import '../App.css'; |
6 |
import Navbar from './Navbar'; |
|
7 |
import * as authActions from '../actions/authActions'; |
|
8 |
||
9 |
class Register extends Component { |
|
10 |
||
11 |
register = () => { |
|
12 |
const username = this.username.value; |
|
13 |
const email = this.email.value; |
|
14 |
const password1 = this.password1.value; |
|
15 |
const password2 = this.password2.value; |
|
16 |
||
17 |
this.props.authActions.registerSubmit(username, email, password1, password2); |
|
18 |
} |
|
19 |
||
| 94 | 20 |
submit = (e) => { |
21 |
e.preventDefault(); |
|
22 |
||
23 |
this.register(); |
|
24 |
} |
|
25 |
||
| 89 | 26 |
onClickLogin = (e) => { |
27 |
e.preventDefault(); |
|
28 |
this.props.history.push('/login'); |
|
29 |
} |
|
30 |
||
31 |
renderErrorMessage(errorMessages, fieldname) { |
|
32 |
if (errorMessages && errorMessages.has(fieldname)) { |
|
33 |
return errorMessages.get(fieldname).map((message, key) => |
|
34 |
<HelpBlock key={ key }>{ message }</HelpBlock> |
|
35 |
); |
|
36 |
} |
|
37 |
} |
|
38 |
||
39 |
render() { |
|
40 |
||
41 |
const panelHeader = ( |
|
42 |
<h4 className="text-uppercase text-center">Register</h4> |
|
43 |
) |
|
44 |
||
45 |
const errorMessages = this.props.register.get('errorMessages'); |
|
46 |
||
47 |
return ( |
|
48 |
<div> |
|
49 |
<Navbar history={this.props.history} /> |
|
50 |
<Grid fluid> |
|
51 |
<Row> |
|
52 |
<Col md={6} mdOffset={3}> |
|
53 |
<Panel header={ panelHeader } className="panel-login"> |
|
| 94 | 54 |
<form onSubmit={this.submit}> |
| 89 | 55 |
<FormGroup validationState={ errorMessages && errorMessages.has('username') ? 'error' : null }> |
56 |
<ControlLabel>Username</ControlLabel> |
|
57 |
<FormControl componentClass="input" type="text" inputRef={ref => { this.username = ref; }} /> |
|
58 |
{ this.renderErrorMessage(errorMessages, 'username') } |
|
59 |
</FormGroup> |
|
60 |
<FormGroup validationState={ errorMessages && errorMessages.has('email') ? 'error' : null }> |
|
61 |
<ControlLabel>Email</ControlLabel> |
|
62 |
<FormControl componentClass="input" type="email" inputRef={ref => { this.email = ref; }} /> |
|
63 |
{ this.renderErrorMessage(errorMessages, 'email') } |
|
64 |
</FormGroup> |
|
65 |
<FormGroup validationState={ errorMessages && errorMessages.has('password1') ? 'error' : null }> |
|
66 |
<ControlLabel>Password</ControlLabel> |
|
67 |
<FormControl componentClass="input" type="password" inputRef={ref => { this.password1 = ref; }} /> |
|
68 |
{ this.renderErrorMessage(errorMessages, 'password1') } |
|
69 |
</FormGroup> |
|
70 |
<FormGroup validationState={ errorMessages && errorMessages.has('password2') ? 'error' : null }> |
|
71 |
<ControlLabel>Confirm password</ControlLabel> |
|
72 |
<FormControl componentClass="input" type="password" inputRef={ref => { this.password2 = ref; }} /> |
|
73 |
{ this.renderErrorMessage(errorMessages, 'password2') } |
|
74 |
</FormGroup> |
|
| 94 | 75 |
<Button type="submit" block bsStyle="primary" onClick={this.register}>Register</Button> |
| 89 | 76 |
</form> |
77 |
</Panel> |
|
78 |
<p className="text-center"> |
|
79 |
<a className="text-muted" href="/login" onClick={ this.onClickLogin }>Already registered? Sign in.</a> |
|
80 |
</p> |
|
81 |
</Col> |
|
82 |
</Row> |
|
83 |
</Grid> |
|
84 |
</div> |
|
85 |
); |
|
86 |
} |
|
87 |
} |
|
88 |
||
89 |
function mapStateToProps(state, props) { |
|
90 |
return { |
|
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
94
diff
changeset
|
91 |
register: state.get('register') |
| 89 | 92 |
}; |
93 |
} |
|
94 |
||
95 |
function mapDispatchToProps(dispatch) { |
|
96 |
return { |
|
97 |
authActions: bindActionCreators(authActions, dispatch) |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
export default connect(mapStateToProps, mapDispatchToProps)(Register); |