-
Notifications
You must be signed in to change notification settings - Fork 480
/
Teacher2021Application.jsx
221 lines (193 loc) · 5.42 KB
/
Teacher2021Application.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import React from 'react';
import PropTypes from 'prop-types';
import FormController from '../../form_components/FormController';
import AboutYou from './AboutYou';
import TeachingBackground from './TeachingBackground';
import ChooseYourProgram from './ChooseYourProgram';
import ProfessionalLearningProgramRequirements from './ProfessionalLearningProgramRequirements';
import AdditionalDemographicInformation from './AdditionalDemographicInformation';
import firehoseClient from '@cdo/apps/lib/util/firehose';
import queryString from 'query-string';
/* global ga */
export default class Teacher2021Application extends FormController {
static propTypes = {
...FormController.propTypes,
accountEmail: PropTypes.string.isRequired,
userId: PropTypes.number.isRequired,
schoolId: PropTypes.string
};
static submitButtonText = 'Complete and Send';
static sessionStorageKey = 'Teacher2021Application';
/**
* @override
*/
componentWillMount() {
super.componentWillMount();
// Extract school info saved in sessionStorage, if any
let reloadedSchoolId = undefined;
if (
this.constructor.sessionStorageKey &&
sessionStorage[this.constructor.sessionStorageKey]
) {
const reloadedState = JSON.parse(
sessionStorage[this.constructor.sessionStorageKey]
);
reloadedSchoolId = reloadedState.data.school;
}
// Populate data from server only if it doesn't override data in sessionStorage
// (even if value in sessionStorage is null)
if (reloadedSchoolId === undefined && this.props.schoolId) {
const schoolAutoFill = {school: this.props.schoolId};
this.setState({data: {...this.state.data, ...schoolAutoFill}});
}
}
componentDidMount() {
window.addEventListener('beforeunload', event => {
if (!this.state.submitting) {
event.preventDefault();
event.returnValue = 'Are you sure? Your application may not be saved.';
}
});
}
/**
* @override
*/
getPageComponents() {
return [
AboutYou,
TeachingBackground,
ChooseYourProgram,
ProfessionalLearningProgramRequirements,
AdditionalDemographicInformation
];
}
/**
* @override
*/
getPageProps() {
return {
...super.getPageProps(),
accountEmail: this.props.accountEmail
};
}
/**
* @override
*/
onInitialize() {
// Log the user ID to firehose.
firehoseClient.putRecord(
{
user_id: this.props.userId,
study: 'application-funnel',
event: 'started-teacher2021-application'
},
{includeUserId: false}
);
}
/**
* @override
*/
onSuccessfulSubmit() {
// Let the server display a confirmation page as appropriate
window.location.reload(true);
}
/**
* @override
*/
onSetPage(newPage) {
if (this.triedToSubmit) {
// If errors exist, create a summary header containing
// clickable links to pages that have errors.
let invalidPages = this.validateForm();
if (invalidPages.length) {
this.setState({
globalError: true,
errorHeader: (
<InvalidPagesSummary
pages={invalidPages}
setPage={this.setPage.bind(this)}
/>
)
});
} else {
this.setState({errorHeader: null, globalError: false});
}
}
const nominated = queryString.parse(window.location.search).nominated;
// Report a unique page view to GA.
let url = '/pd/application/teacher/';
url += newPage + 1;
if (nominated) {
url += '?nominated=true';
}
ga('set', 'page', url);
ga('send', 'pageview');
}
/**
* @override
*/
setPage(i) {
const newPage = Math.min(
Math.max(i, 0),
this.getPageComponents().length - 1
);
this.setState({
currentPage: newPage
});
this.saveToSessionStorage({currentPage: newPage});
}
/**
* Find all pages that have errors.
* @returns {number[]} array of page indexes
*/
validateForm() {
let invalidPages = [];
// Validating page in reversed order because the last page being validated will overwrite
// values in this form's state, and only errors found in that page will be highlighted.
for (let i = this.getPageComponents().length - 1; i >= 0; i--) {
if (!this.validatePageRequiredFields(i)) {
invalidPages.unshift(i);
}
}
return invalidPages;
}
/**
* @override
* @param {Event} event
*/
handleSubmit(event) {
event.preventDefault();
this.triedToSubmit = true;
let invalidPages = this.validateForm();
if (invalidPages.length > 0) {
// go to page with the smallest index
this.setPage(invalidPages[0]);
} else {
super.handleSubmit(event);
}
}
/**
* @override
*/
renderControlButtons() {
return [super.renderControlButtons(), this.renderErrorFeedback()];
}
}
const InvalidPagesSummary = ({pages, setPage}) => (
<span>
Please fill out all required fields on {pages.length > 1 ? 'pages' : 'page'}{' '}
{pages
.map(p => (
<a key={p} onClick={() => setPage(p)} style={{cursor: 'pointer'}}>
{p + 1}
</a>
))
.reduce((prev, curr) => [prev, ', ', curr])}
. Once you are done, head to the last page to confirm and submit your
application.
</span>
);
InvalidPagesSummary.propTypes = {
pages: PropTypes.arrayOf(PropTypes.number).isRequired,
setPage: PropTypes.func.isRequired
};