generated from DTS-STN/next-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecision-reviews.ts
210 lines (202 loc) · 6.09 KB
/
decision-reviews.ts
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
import { buildAemUri, buildLink } from '../../lib/links'
import { cachified } from 'cachified'
import { lruCache as cache, defaultTtl as ttl } from '../../lib/cache-utils'
interface GetSchDecisionReviewsV2 {
data: {
schPageV1List: {
items: Array<{
_path: string
scPageNameEn: string
scPageNameFr: string
scTitleEn: string
scTitleFr: string
scBreadcrumbParentPages: Array<{
scId: string
scTitleEn: string
scTitleFr: string
scPageNameEn: string
scPageNameFr: string
}>
scFragments: Array<{
scId: string
scContentEn: {
markdown: string
}
scContentFr: {
markdown: string
}
scFragments: Array<{
scId: string
scLinkTextEn: string
scLinkTextFr: string
scLinkTextAssistiveEn: string
scLinkTextAssistiveFr: string
scDestinationURLEn: string
scDestinationURLFr: string
schBetaPopUp: boolean
schURLType?: string
}>
}>
}>
}
}
}
const getCachedContent = () => {
return cachified({
key: `content-decision-reviews`,
cache,
getFreshValue: async (): Promise<GetSchDecisionReviewsV2 | null> => {
const targetUri = buildAemUri('getSchDecisionReviewsV2')
const response = await fetch(targetUri)
if (!response.ok) return null
return await response.json()
},
ttl,
})
}
export async function getDecisionReviewsContent(): Promise<DecisionReviewContent> {
const response = await getCachedContent()
const askFragment = findFragmentByScId(
response,
'decision-review-ask-service-canada',
)
const appealFragment = findFragmentByScId(
response,
'decision-review-appeal-to-sst',
)
const mappedDecisionReviews = {
en: {
id: 'request-review-decision',
breadcrumb:
response?.data.schPageV1List.items[0].scBreadcrumbParentPages.map(
(level) => {
return {
link: level.scPageNameEn,
text: level.scTitleEn,
id: level.scId,
}
},
),
pageName: response?.data.schPageV1List.items[0].scPageNameEn,
heading: response?.data.schPageV1List.items[0].scTitleEn,
content: [
{
content: askFragment?.scContentEn.markdown,
button: {
id: askFragment?.scFragments[0].scId,
text: askFragment?.scFragments[0].scLinkTextEn,
areaLabel: askFragment?.scFragments[0].scLinkTextAssistiveEn,
link: buildLink(
askFragment?.scFragments[0].schURLType,
askFragment?.scFragments[0].scDestinationURLEn ?? '',
),
betaPopUp: askFragment?.scFragments[0].schBetaPopUp,
},
},
{
content: appealFragment?.scContentEn.markdown,
button: {
id: appealFragment?.scFragments[0].scId,
text: appealFragment?.scFragments[0].scLinkTextEn,
areaLabel: appealFragment?.scFragments[0].scLinkTextAssistiveEn,
link: buildLink(
appealFragment?.scFragments[0].schURLType,
appealFragment?.scFragments[0].scDestinationURLEn ?? '',
),
betaPopUp: appealFragment?.scFragments[0].schBetaPopUp,
},
},
],
},
fr: {
id: 'demande-revision',
breadcrumb:
response?.data.schPageV1List.items[0].scBreadcrumbParentPages.map(
(level) => {
return {
link: level.scPageNameFr,
text: level.scTitleFr,
id: level.scId,
}
},
),
pageName: response?.data.schPageV1List.items[0].scPageNameFr,
heading: response?.data.schPageV1List.items[0].scTitleFr,
content: [
{
content: askFragment?.scContentFr.markdown,
button: {
id: askFragment?.scFragments[0].scId,
text: askFragment?.scFragments[0].scLinkTextFr,
areaLabel: askFragment?.scFragments[0].scLinkTextAssistiveFr,
link: buildLink(
askFragment?.scFragments[0].schURLType,
askFragment?.scFragments[0].scDestinationURLFr ?? '',
),
betaPopUp: askFragment?.scFragments[0].schBetaPopUp,
},
},
{
content: appealFragment?.scContentFr.markdown,
button: {
id: appealFragment?.scFragments[0].scId,
text: appealFragment?.scFragments[0].scLinkTextFr,
areaLabel: appealFragment?.scFragments[0].scLinkTextAssistiveFr,
link: buildLink(
appealFragment?.scFragments[0].schURLType,
appealFragment?.scFragments[0].scDestinationURLFr ?? '',
),
betaPopUp: appealFragment?.scFragments[0].schBetaPopUp,
},
},
],
},
}
return mappedDecisionReviews
}
const findFragmentByScId = (
res: GetSchDecisionReviewsV2 | null,
id: string,
) => {
return (
res?.data.schPageV1List.items[0].scFragments.find(
({ scId }) => scId === id,
) ?? null
)
}
// TODO: Figure out which of these actually need to be optional
export interface DecisionReviewContent {
err?: string
en?: {
id: string
breadcrumb: { link: string; text: string; id: string }[] | undefined
pageName: string | undefined
heading: string | undefined
content: {
content: string | undefined
button: {
id: string | undefined
text: string | undefined
areaLabel: string | undefined
link: string | undefined
betaPopUp: boolean | undefined
}
}[]
}
fr?: {
id: string
breadcrumb: { link: string; text: string; id: string }[] | undefined
pageName: string | undefined
heading: string | undefined
content: {
content: string | undefined
button: {
id: string | undefined
text: string | undefined
areaLabel: string | undefined
link: string | undefined
betaPopUp: boolean | undefined
}
}[]
}
}