-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathPeopleForm.js
More file actions
59 lines (48 loc) · 1.7 KB
/
PeopleForm.js
File metadata and controls
59 lines (48 loc) · 1.7 KB
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
import React, { Component } from 'react'
import { reduxForm, Field } from 'redux-form'
import ErrorField from '../common/ErrorField'
import emailValidator from 'email-validator'
import { moduleName, addUser} from '../../ducks/people'
import { connect } from 'react-redux'
class PeopleForm extends Component {
render() {
const { handleSubmit, submitting, reset } = this.props;
return (
<div>
<h2>
Add person
</h2>
<form onSubmit={handleSubmit(this.props.addUser)}>
<Field name="first_name" component={ErrorField} />
<Field name="last_name" component={ErrorField} />
<Field name="email" component={ErrorField} />
<div>
<input type="submit" disabled={submitting} onClick={reset} />
</div>
</form>
</div>
);
}
}
const validate = ({first_name, last_name, email}) => {
const errors = {}
if (!email) errors.email = 'email is required'
else if (!emailValidator.validate(email)) errors.email = 'invalid email'
if (!first_name) errors.first_name = 'first_name is required'
if(!last_name) errors.last_name = 'last_name is required'
return errors
}
const mapStateToProps = (state) => ({
loading: state[moduleName.loading]
});
const mapDispatchToProps = (dispatch) => ({
addUser: ({first_name, last_name, email}) => dispatch(addUser({first_name, last_name, email}))
});
PeopleForm = connect(
mapStateToProps,
mapDispatchToProps
)(PeopleForm);
export default reduxForm({
form: 'people',
validate,
})(PeopleForm);