This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ReviewPage.tsx
204 lines (191 loc) · 5.87 KB
/
ReviewPage.tsx
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
import React, { useContext } from 'react';
import { useFormikContext } from 'formik';
import { Link } from 'react-router-dom-v5-compat';
import { VaOnThisPage } from '@department-of-veterans-affairs/component-library/dist/react-bindings';
import { Page } from '../routing';
import { FieldObject } from './types';
import { CheckboxField } from '../form-builder';
import { parseDate } from '../utils/helpers';
import { PageContext } from './PageContext';
const TruthAndFalseLabels = [
{
label: 'Yes',
values: [true, 1, 'true'],
},
{
label: 'No',
values: [false, 0, 'false'],
},
];
/**
* Transforms fields value into value that is more readable
* @param {boolean | object | string} field object value
* @param {string} key object key name
*
* @beta
*/
const transformFieldValue = (key: number, field: FieldObject) => {
if (
typeof field?.value === 'undefined' ||
field.value === null ||
typeof field?.value === 'object'
)
return;
// check for a 0,false,1,or true value
const truthOrFalseIndex = TruthAndFalseLabels.findIndex((labelValue) => {
return !!labelValue.values.find(
(fieldValue) => field?.value === fieldValue
);
});
if (truthOrFalseIndex >= 0) {
return TruthAndFalseLabels[truthOrFalseIndex].label;
}
// Add field options
if (field?.options) {
const fieldIndex = field.options.findIndex(
(fieldOption) => fieldOption.value === field?.value
);
if (fieldIndex >= 0) {
return field.options[fieldIndex].label;
}
}
// Parse date
if (field?.type === 'date') {
const date = parseDate(field.value as string);
return date.toLocaleDateString('en-US', {
day: 'numeric',
month: 'long',
year: 'numeric',
});
}
// Number fields
if (field?.type === 'accounting') {
return `$${field.value as string}`;
}
// Telephone fields
if (field?.type === 'tel') {
return `${(field.value as string).replace(
/(\d{3})(\d{3})(\d{4})/,
'($1) $2-$3'
)}`;
}
return field.value;
};
/**
* Loops through field objects and adds JSX to a buffer
* to be rendered later
* @param {objects} fields object value
* @param {number} rank level number of recursion
*
* @beta
*/
const bufferFields = (fields: FieldObject[], rank = 0) => {
const fieldBuffer: JSX.Element[] = [];
fields.forEach((field, key) => {
// Revisit this in future ticket
const fieldJSX = recurseField(key, field, rank);
if (fieldJSX) fieldBuffer.push(fieldJSX);
});
if (fieldBuffer.length > 0) return fieldBuffer;
};
/**
* Recurses through field and either returns value or
* uses bufferField to loop through object value
* @param {string} key object key name
* @param {boolean | object | string} field object value
* @param {number} rank level number of recursion
*
* @beta
*/
const recurseField = (
key: number,
field: FieldObject,
rank = 0
): JSX.Element | undefined => {
if (
field.value === '' ||
field.value === 0 ||
field.value === null ||
field.value === undefined
)
return;
const fieldLabel = field.label && (
<label className="vads-u-margin-top--1 vads-u-color--gray">
{field.label}
</label>
);
if (field?.value) {
return (
<div
className={`level-${rank}-field-${key} vads-u-margin-bottom--1p5`}
key={`level-${rank}-field-${key}`}
>
{fieldLabel}
<span
className={`review-page__page-info__value-text field-value field-value-level-${rank} vads-u-color--gray-dark vads-u-font-size--md`}
key={`child-level-${rank}-field-${key}`}
>
{' '}
{transformFieldValue(key, field)}
</span>
</div>
);
}
};
export default function ReviewPage(props: { title: string }) {
const state = useFormikContext();
const { listOfPages, setListOfPages } = useContext(PageContext);
return (
<Page {...props} nextButtonCustomText="Submit" hidePreviousButton={false}>
<article>
<h1>{props.title}</h1>
<VaOnThisPage></VaOnThisPage>
{listOfPages.map((page) => {
return (
<section key={page.id} className="review-page__page-info">
<div className="review-page__page-heading vads-u-justify-content--space-between vads-l-row vads-u-border-bottom--1px vads-u-border-color--link-default">
<h2 className="vads-u-font-size--h3 vads-u-flex--1 review-page__page-heading__text vads-u-color--primary-darker">
{page.title}
</h2>
<Link
to={page.path + '?edit=true&source=' + page.id}
className="vads-u-margin-top--4 vads-u-text-align--right review-page__page-heading__link"
id={`edit${page.id}`}
aria-label={`Edit ${page.title}`}
>
Edit
</Link>
</div>
{bufferFields(page.fields)}
</section>
);
})}
<div>
<p className="vads-u-padding-y--2">
<strong>Note:</strong> According to federal law, there are criminal
penalties, including a fine and/or imprisonment for up to 5 years,
for withholding information or for providing incorrect information.
(See 18 U.S.C. 1001)
</p>
<CheckboxField
required="You must accept the privacy policy before continuing."
name="privacyAgreementAccepted"
label="I have read and accept the privacy policy"
description={null}
>
<p slot="description">
Please read and accept the{' '}
<a
aria-label="Privacy policy, will open in new tab"
target="_blank"
href="/privacy-policy/"
>
privacy policy
</a>
</p>
</CheckboxField>
</div>
</article>
</Page>
);
}