-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathSubmitController.jsx
161 lines (140 loc) · 4.42 KB
/
SubmitController.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
156
157
158
159
160
161
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { withRouter } from 'react-router';
import SubmitButtons from './SubmitButtons';
import { PreSubmitSection } from '../components/PreSubmitSection';
import { isValidForm } from '../validation';
import {
createPageListByChapter,
getActiveExpandedPages
} from '../helpers';
import {
setPreSubmit,
setSubmission,
submitForm
} from '../actions';
class SubmitController extends React.Component {
componentWillReceiveProps(nextProps) {
const nextStatus = nextProps.form.submission.status;
const previousStatus = this.props.form.submission.status;
if (nextStatus !== previousStatus && nextStatus === 'applicationSubmitted') {
const newRoute = `${nextProps.formConfig.urlPrefix}confirmation`;
this.props.router.push(newRoute);
}
}
getPreSubmit = formConfig => {
return {
required: false,
field: 'AGREED',
label: 'I agree to the terms and conditions.',
error: 'You must accept the agreement before submitting.',
...formConfig.preSubmitInfo
};
}
goBack = () => {
const {
form,
pageList,
router
} = this.props;
const expandedPageList = getActiveExpandedPages(pageList, form.data);
// TODO: Fix this bug that assumes there is a confirmation page.
// Actually, it assumes the app also doesn't add routes at the end!
// A component at this level should not need to know these things!
router.push(expandedPageList[expandedPageList.length - 2].path);
}
handleSubmit = () => {
const {
form,
formConfig,
pagesByChapter
} = this.props;
// If a pre-submit agreement is required, make sure it was accepted
const preSubmit = this.getPreSubmit(formConfig);
if (preSubmit.required && !form.data[preSubmit.field]) {
this.props.setSubmission('hasAttemptedSubmit', true);
// <PreSubmitSection/> is displaying an error for this case
return;
}
// Validation errors in this situation are not visible, so we’d
// like to know if they’re common
const { isValid, errors } = isValidForm(form, pagesByChapter);
if (!isValid) {
const recordEvent = formConfig.recordEvent ?
formConfig.recordEvent :
console.log.bind(console); // eslint-disable-line no-console
recordEvent({ event: 'validation-failed-on-submit', errors });
this.props.setSubmission('status', 'validationError');
this.props.setSubmission('hasAttemptedSubmit', true);
return;
}
// User accepted if required, and no errors, so submit
this.props.submitForm(formConfig, form);
}
render() {
const {
form,
formConfig,
showPreSubmitError,
renderErrorMessage
} = this.props;
const preSubmit = this.getPreSubmit(formConfig);
return (
<div>
<PreSubmitSection
preSubmitInfo={preSubmit}
onChange={(event) => this.props.setPreSubmit(preSubmit.field, event.target.value)}
checked={form.data[preSubmit.field]}
showError={showPreSubmitError}/>
<SubmitButtons
onBack={this.goBack}
onSubmit={this.handleSubmit}
submission={form.submission}
renderErrorMessage={renderErrorMessage}/>
</div>
);
}
}
function mapStateToProps(state, ownProps) {
const {
formConfig,
pageList,
renderErrorMessage
} = ownProps;
const router = ownProps.router;
const form = state.form;
const pagesByChapter = createPageListByChapter(formConfig);
const submission = form.submission;
const showPreSubmitError = submission.hasAttemptedSubmit;
return {
form,
formConfig,
pagesByChapter,
pageList,
renderErrorMessage,
router,
submission,
showPreSubmitError
};
}
const mapDispatchToProps = {
setPreSubmit,
setSubmission,
submitForm
};
SubmitController.propTypes = {
form: PropTypes.object.isRequired,
formConfig: PropTypes.object.isRequired,
pagesByChapter: PropTypes.object.isRequired,
pageList: PropTypes.array.isRequired,
renderErrorMessage: PropTypes.func,
router: PropTypes.object.isRequired,
setPreSubmit: PropTypes.func.isRequired,
setSubmission: PropTypes.func.isRequired,
submitForm: PropTypes.func.isRequired,
submission: PropTypes.object.isRequired
};
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(SubmitController));
// for tests
export { SubmitController };