-
Notifications
You must be signed in to change notification settings - Fork 126
/
EmploymentRecord.jsx
227 lines (205 loc) · 6.28 KB
/
EmploymentRecord.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
222
223
224
225
226
227
import React, { useState } from 'react';
import { connect } from 'react-redux';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import { setData } from 'platform/forms-system/src/js/actions';
import {
VaSelect,
VaDate,
VaTextInput,
VaCheckbox,
} from '@department-of-veterans-affairs/component-library/dist/react-bindings';
import { parseISODate } from 'platform/forms-system/src/js/helpers';
const defaultRecord = [
{
type: '',
from: '',
to: '',
isCurrent: false,
employerName: '',
},
];
const EmploymentRecord = ({
idSchema,
uiSchema,
formData,
setFormData,
employmentHistory,
formContext,
}) => {
const [fromDateError, setFromDateError] = useState();
const [toDateError, setToDateError] = useState();
const index = Number(idSchema.$id.slice(-1));
const { userType, userArray } = uiSchema['ui:options'];
const { employmentRecords } = employmentHistory[`${userType}`];
const employment = employmentRecords || defaultRecord;
const { from, to } = employment ? employment[index] : [];
const { month: fromMonth, year: fromYear } = parseISODate(from);
const { month: toMonth, year: toYear } = parseISODate(to);
const { submitted } = formContext;
const [employerName, setEmployerName] = useState(
employment[index].employerName || null,
);
const startError = 'Please enter your employment start date.';
const endError = 'Please enter your employment end date.';
const employerError = 'Please enter your employer name.';
const updateFormData = updated => {
setFormData({
...formData,
[`${userArray}`]: updated.filter(record => record.isCurrent),
personalData: {
...formData.personalData,
employmentHistory: {
...employmentHistory,
[`${userType}`]: {
...employmentHistory[`${userType}`],
employmentRecords: [...updated],
},
},
},
});
};
const handleChange = (key, val) => {
const updated = employment.map((item, i) => {
return i === index ? { ...item, [key]: val } : item;
});
updateFormData(updated);
};
const handleEmployerNameChange = event => {
handleChange('employerName', event.target.value);
setEmployerName(event.target.value);
};
const handleCheckboxChange = (key, val) => {
const updated = employment.map((item, i) => {
return i === index ? { ...item, [key]: val, to: '' } : item;
});
updateFormData(updated);
};
const validateYear = (monthYear, errorSetter, requiredMessage) => {
const [year] = monthYear.split('-');
const todayYear = new Date().getFullYear();
const isComplete = /\d{4}-\d{1,2}/.test(monthYear);
if (!isComplete) {
// This allows a custom required error message to be used
errorSetter(requiredMessage);
} else if (
!!year &&
(parseInt(year, 10) > todayYear || parseInt(year, 10) < 1900)
) {
errorSetter(`Please enter a year between 1900 and ${todayYear}`);
} else {
errorSetter(null);
}
};
const handleDateChange = (key, monthYear) => {
const dateString = `${monthYear}-XX`;
const updated = employment.map((item, i) => {
return i === index ? { ...item, [key]: dateString } : item;
});
updateFormData(updated);
};
// check field for validation errors only if field is dirty or form has been submitted
const showError = () => {
return submitted && !employment[index].type
? 'Please select a type of employment'
: false;
};
return (
<>
<div className="input-size-5">
<VaSelect
name="type"
data-test-id="employment-type"
label="Type of work"
required
value={employment[index].type || []}
onVaSelect={e => handleChange('type', e.detail.value)}
error={showError() || null}
uswds
>
<option value=""> </option>
<option value="Full time">Full time</option>
<option value="Part time">Part time</option>
<option value="Seasonal">Seasonal</option>
<option value="Temporary">Temporary</option>
</VaSelect>
</div>
<div className="vads-u-margin-top--3">
<VaDate
monthYearOnly
value={`${fromYear}-${fromMonth}`}
label="Date you started work at this job?"
name="from"
onDateChange={e => handleDateChange('from', e.target.value)}
onDateBlur={e =>
validateYear(e.target.value || '', setFromDateError, startError)
}
required
error={fromDateError}
/>
</div>
<div
className={classNames('vads-u-margin-top--3', {
'field-disabled': employment[index].isCurrent,
})}
>
<VaDate
monthYearOnly
value={`${toYear}-${toMonth}`}
label="Date you stopped work at this job?"
name="to"
onDateChange={e => handleDateChange('to', e.target.value)}
onDateBlur={e =>
validateYear(e.target.value || '', setToDateError, endError)
}
required
error={toDateError}
/>
</div>
<VaCheckbox
name="current-employment"
label="I currently work here"
checked={employment[index].isCurrent || false}
onVaChange={event =>
handleCheckboxChange('isCurrent', event.detail.checked)
}
uswds
/>
<div className="vads-u-margin-bottom--2">
<VaTextInput
error={(submitted && employerError) || null}
label="Employer name"
name="employerName"
onInput={handleEmployerNameChange}
required
type="text"
value={employerName || ''}
width="xl"
uswds
/>
</div>
</>
);
};
EmploymentRecord.propTypes = {
children: PropTypes.object,
employmentHistory: PropTypes.object,
formContext: PropTypes.object,
formData: PropTypes.object,
idSchema: PropTypes.object,
setFormData: PropTypes.func,
uiSchema: PropTypes.object,
};
const mapStateToProps = ({ form }) => {
return {
formData: form.data,
employmentHistory: form.data.personalData.employmentHistory,
};
};
const mapDispatchToProps = {
setFormData: setData,
};
export default connect(
mapStateToProps,
mapDispatchToProps,
)(EmploymentRecord);