-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSummaryViewModel.ts
More file actions
240 lines (214 loc) · 6.17 KB
/
SummaryViewModel.ts
File metadata and controls
240 lines (214 loc) · 6.17 KB
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
import { SchemaVersion, type Section } from '@defra/forms-model'
import { PaymentField } from '~/src/server/plugins/engine/components/PaymentField.js'
import { type PaymentState } from '~/src/server/plugins/engine/components/PaymentField.types.js'
import {
getAnswer,
type Field
} from '~/src/server/plugins/engine/components/helpers/components.js'
import {
type BackLink,
type ComponentViewModel
} from '~/src/server/plugins/engine/components/types.js'
import {
evaluateTemplate,
getError,
getPageHref
} from '~/src/server/plugins/engine/helpers.js'
import {
type Detail,
type DetailItem,
type DetailItemField,
type DetailItemRepeat
} from '~/src/server/plugins/engine/models/types.js'
import { RepeatPageController } from '~/src/server/plugins/engine/pageControllers/RepeatPageController.js'
import { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers/pages.js'
import { validationOptions as opts } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'
import {
type CheckAnswers,
type FormContext,
type FormContextRequest,
type FormState,
type FormSubmissionError,
type SummaryListAction,
type SummaryListRow
} from '~/src/server/plugins/engine/types.js'
export class SummaryViewModel {
/**
* Responsible for parsing state values to the govuk-frontend summary list template
*/
page: PageControllerClass
pageTitle: string
declaration?: string
details: Detail[]
checkAnswers: CheckAnswers[]
context: FormContext
name: string | undefined
backLink?: BackLink
feedbackLink?: string
phaseTag?: string
errors?: FormSubmissionError[]
serviceUrl: string
hasMissingNotificationEmail?: boolean
components?: ComponentViewModel[]
allowSaveAndExit = false
paymentState?: PaymentState
paymentDetails?: CheckAnswers
constructor(
request: FormContextRequest,
page: PageControllerClass,
context: FormContext
) {
const { model } = page
const { basePath, def, sections } = model
const { isForceAccess } = context
this.page = page
this.pageTitle = page.title
if (def.schema === SchemaVersion.V2 && !page.title) {
this.pageTitle = 'Check your answers before sending your form'
}
this.serviceUrl = `/${basePath}`
this.name = def.name
this.declaration = def.declaration
this.context = context
const result = model
.makeFilteredSchema(this.context.relevantPages)
.validate(this.context.relevantState, { ...opts, stripUnknown: true })
// Format errors
this.errors = result.error?.details.map(getError)
this.details = this.summaryDetails(request, sections)
// Format check answers
this.checkAnswers = this.details.map((detail): CheckAnswers => {
const { title } = detail
const rows = detail.items.map((item): SummaryListRow => {
const items: SummaryListAction[] = []
// Remove summary list actions from previews
if (!isForceAccess) {
items.push({
href: item.href,
text: 'Change',
classes: 'govuk-link--no-visited-state',
visuallyHiddenText: item.label
})
}
return {
key: {
text: evaluateTemplate(item.title, context)
},
value: {
classes: 'app-prose-scope',
html: item.value || 'Not provided'
},
actions: {
items
}
}
})
return {
title: title ? { text: title } : undefined,
summaryList: { rows }
}
})
}
private summaryDetails(request: FormContextRequest, sections: Section[]) {
const { context, errors } = this
const { relevantPages, state } = context
const details: Detail[] = []
;[...sections, undefined].forEach((section) => {
const items: DetailItem[] = []
const sectionPages = relevantPages.filter(
(page) => page.section === section
)
sectionPages.forEach((page) => {
const { collection, path } = page
if (page instanceof RepeatPageController) {
items.push(
ItemRepeat(page, state, {
path: page.getSummaryPath(request),
errors
})
)
} else {
for (const field of collection.fields) {
// PaymentField is rendered in its own section, skip it here
if (field instanceof PaymentField) {
continue
}
items.push(ItemField(page, state, field, { path, errors }))
}
}
})
if (items.length) {
details.push({
name: section?.name,
title: section?.title,
items
})
}
})
return details
}
}
/**
* Creates a repeater detail item
* @see {@link DetailItemField}
*/
function ItemRepeat(
page: RepeatPageController,
state: FormState,
options: {
path: string
errors?: FormSubmissionError[]
}
): DetailItemRepeat {
const { collection, repeat } = page
const { name, title } = repeat.options
const values = page.getListFromState(state)
const unit = values.length === 1 ? 'answer' : 'answers'
return {
name,
label: title,
title,
value: values.length ? `You have added ${values.length} ${unit}` : '',
href: getPageHref(page, options.path, {
returnUrl: getPageHref(page, page.getSummaryPath())
}),
state,
page,
// Repeater field detail items
subItems: values.map((repeatState) =>
collection.fields.map((field) =>
ItemField(page, repeatState, field, options)
)
)
}
}
/**
* Creates a form field detail item
* @see {@link DetailItemField}
*/
export function ItemField(
page: PageControllerClass,
state: FormState,
field: Field,
options: {
path: string
errors?: FormSubmissionError[]
}
): DetailItemField {
return {
name: field.name,
label: field.title,
title:
field.options.required === false
? `${field.label} (optional)`
: field.label,
error: field.getFirstError(options.errors),
value: getAnswer(field, state),
href: getPageHref(page, options.path, {
returnUrl: getPageHref(page, page.getSummaryPath())
}),
state,
page,
field
}
}