-
Notifications
You must be signed in to change notification settings - Fork 125
/
ReportModal.jsx
264 lines (245 loc) · 7.44 KB
/
ReportModal.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* eslint-disable camelcase */
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import {
VaModal,
VaCheckboxGroup,
} from '@department-of-veterans-affairs/component-library/dist/react-bindings';
const ReportModal = ({
representativeName,
representativeId,
address,
phone,
email,
existingReports,
onCloseModal,
submitRepresentativeReport,
handleOtherInputChangeTestId,
testReportObject,
}) => {
const [reportObject, setReportObject] = useState({
phone: null,
email: null,
address: null,
other: null,
});
const [otherIsChecked, setOtherIsChecked] = useState(false);
const [otherIsBlankError, setOtherIsBlankError] = useState(false);
const [reportIsBlankError, setReportIsBlankError] = useState(false);
// render conditions
const totalReportableItems =
(address !== null) + (phone !== null) + (email !== null) + 1;
const someItemsReported = existingReports;
const notAllItemsReported =
!someItemsReported ||
Object.keys(existingReports).length < totalReportableItems;
const addressReportable = address && !existingReports?.address;
const emailReportable = email && !existingReports?.email;
const phoneReportable = phone && !existingReports?.phone;
const otherReportable = !existingReports?.other;
const handleOtherInputChange = event => {
setOtherIsBlankError(false);
const newState = { ...reportObject };
newState.other = event.target.value;
setReportObject(newState);
};
const handleCheckboxChange = event => {
setReportIsBlankError(false);
const {
target: { id, checked },
} = event;
const newState = { ...reportObject };
switch (id) {
case '1':
newState.address = checked ? address : null;
break;
case '2':
newState.email = checked ? email : null;
break;
case '3':
newState.phone = checked ? phone : null;
break;
case '4':
setOtherIsBlankError(false);
setOtherIsChecked(checked);
break;
default:
break;
}
setReportObject(newState);
};
const onSubmitModal = () => {
const formattedReportObject = {
representativeId,
reports: {},
};
// push non-null items to reports object
Object.keys(reportObject).forEach(prop => {
if (reportObject[prop] !== null) {
formattedReportObject.reports[prop] = reportObject[prop];
}
});
if (otherIsChecked && !formattedReportObject.reports.other) {
setOtherIsBlankError(true);
return;
}
if (!Object.keys(formattedReportObject.reports).length) {
setReportIsBlankError(true);
return;
}
submitRepresentativeReport(formattedReportObject);
setReportObject({
phone: null,
email: null,
address: null,
other: null,
});
onCloseModal();
};
return (
<>
<VaModal
onCloseEvent={onCloseModal}
onPrimaryButtonClick={onSubmitModal}
onSecondaryButtonClick={onCloseModal}
primaryButtonText="Submit"
secondaryButtonText="Cancel"
visible
uswds
>
{/* These buttons trigger methods for unit testing - temporary workaround for shadow root issues with va checkboxes */}
{handleOtherInputChangeTestId ? (
<>
<button
id="handle-checkbox-change-test-button"
type="button"
onClick={() =>
handleCheckboxChange({
target: { id: handleOtherInputChangeTestId, checked: 'true' },
})
}
/>
<button
id="handle-other-input-change-test-button"
type="button"
onClick={() =>
handleOtherInputChange({
target: {
id: handleOtherInputChangeTestId,
value: 'test comment',
},
})
}
/>
</>
) : null}
{testReportObject ? (
<>
<button
id="set-report-object-button"
type="button"
onClick={() => setReportObject({ ...testReportObject })}
/>
<button
id="submit-modal-test-button"
type="button"
onClick={() => onSubmitModal()}
/>
</>
) : null}
<h2
className="report-modal-header"
style={{ fontSize: 20, marginTop: 10 }}
>
Report outdated information for {representativeName}
</h2>
{someItemsReported && (
<>
<h3 style={{ fontSize: 17, marginTop: 20 }}>
You reported this information
</h3>
<ul>
{existingReports.address && <li>Outdated address</li>}
{existingReports.email && <li>Outdated email</li>}
{existingReports.phone && <li>Outdated phone number</li>}
{existingReports.other && (
<li>Other: "{existingReports.other}"</li>
)}
</ul>
</>
)}
{someItemsReported &&
notAllItemsReported && (
<>
<h3 style={{ fontSize: 17, marginBottom: 0 }}>
You can add to your report
</h3>
</>
)}
{notAllItemsReported && (
<>
<VaCheckboxGroup
error={reportIsBlankError ? 'Please select an item' : null}
hint={null}
onVaChange={handleCheckboxChange}
required
label="Select the information we need to update"
label-header-level=""
uswds
>
{addressReportable && (
<va-checkbox label="Address" name="address" uswds id="1" />
)}
{emailReportable && (
<va-checkbox label="Email" name="email" uswds id="2" />
)}
{phoneReportable && (
<va-checkbox label="Phone number" name="phone" uswds id="3" />
)}
{otherReportable && (
<va-checkbox label="Other" name="other" uswds id="4" />
)}
</VaCheckboxGroup>
</>
)}
{otherIsChecked && (
<div className="report-other-text-input">
<div
className={`${
!otherIsBlankError ? 'form-expanding-group-open' : null
} form-expanding-group-inner-enter-done`}
>
<va-textarea
hint={null}
label="Describe the other information we need to update"
error={otherIsBlankError ? 'This field is required' : null}
value={reportObject.other}
name="Other comment input"
maxlength={250}
onInput={e => handleOtherInputChange(e)}
uswds
charcount
/>
</div>
</div>
)}
</VaModal>
</>
);
};
export default ReportModal;
ReportModal.propTypes = {
address: PropTypes.string,
email: PropTypes.string,
existingReports: PropTypes.shape({
address: PropTypes.string,
email: PropTypes.string,
other: PropTypes.string,
phone: PropTypes.string,
}),
phone: PropTypes.string,
representativeId: PropTypes.string,
representativeName: PropTypes.string,
submitRepresentativeReport: PropTypes.func,
onCloseModal: PropTypes.func,
};