generated from DTS-STN/next-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprofile.ts
308 lines (299 loc) · 8.62 KB
/
profile.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import { buildAemUri, buildLink } from '../../lib/links'
import { cachified } from 'cachified'
import { lruCache as cache, defaultTtl as ttl } from '../../lib/cache-utils'
interface GetSchProfileV2 {
data: {
schPageV1List: {
items: Array<{
_path: string
scTitleEn: string
scTitleFr: string
scBreadcrumbParentPages: Array<{
scId: string
scTitleEn: string
scTitleFr: string
scPageNameEn: string
scPageNameFr: string
}>
scFragments: Array<{
scId: string
scContentEn?: {
json: Array<{
nodeType: string
content: Array<{
nodeType: string
value: string
data?: {
href: string
}
}>
style?: string
}>
}
scContentFr?: {
json: Array<{
nodeType: string
content: Array<{
nodeType: string
value: string
data?: {
href: string
}
}>
style?: string
}>
}
scTitleEn?: string
scTitleFr?: string
scItems?: Array<{
scId: string
scLinkTextEn: string
scLinkTextFr: string
scLinkTextAssistiveEn: string
scLinkTextAssistiveFr: string
scIconCSS: string
schURLType: string
scDestinationURLEn: string
scDestinationURLFr: string
schBetaPopUp: boolean
}>
scDestinationURLEn?: string
scDestinationURLFr?: string
}>
}>
}
}
}
const getCachedContent = () => {
return cachified({
key: `content-profile`,
cache,
getFreshValue: async (): Promise<GetSchProfileV2 | null> => {
const targetUri = buildAemUri('getSchProfileV2')
const response = await fetch(targetUri)
if (!response.ok) return null
return await response.json()
},
ttl,
})
}
export async function getProfileContent(): Promise<ProfileContent> {
const response = await getCachedContent()
// LookingFor Fragment
const enLookingForFragment =
findFragmentByScId(response, 'looking-for-security-settings')
?.scContentEn ?? null
const frLookingForFragment =
findFragmentByScId(response, 'looking-for-security-settings')
?.scContentFr ?? null
// BackToDashboard Fragment
const backToDashboardFragment = findFragmentByScId(
response,
'back-to-my-dashboard',
)
// ProfileIntro Fragment
const profileIntroFragment = findFragmentByScId(response, 'profile-intro')
const mappedProfile = {
en: {
breadcrumb:
response?.data.schPageV1List.items[0].scBreadcrumbParentPages.map(
(level) => {
return {
link: level.scPageNameEn,
text: level.scTitleEn,
}
},
),
pageName: response?.data.schPageV1List.items[0].scTitleEn,
heading: profileIntroFragment?.scContentEn?.json[0].content[0].value,
list: response?.data.schPageV1List.items[0].scFragments
.map((element) => {
if (
element.scId === 'ei-profile-list' ||
element.scId === 'cpp-profile-list' ||
element.scId === 'oas-profile-list' ||
element.scId === 'sin-profile-list'
) {
return {
id: element.scId,
title: element.scTitleEn,
tasks: element.scItems?.map((item) => {
return {
id: item.scId,
title: item.scLinkTextEn,
areaLabel: item.scLinkTextAssistiveEn,
link: buildLink(item.schURLType, item.scDestinationURLEn),
icon: item.scIconCSS,
betaPopUp: item.schBetaPopUp,
}
}),
}
}
})
.filter((e) => e),
lookingFor: {
title: enLookingForFragment?.json[0].content[0].value,
subText: [
enLookingForFragment?.json[1].content[0].value,
enLookingForFragment?.json[1].content[1].value,
],
link: 'security-settings',
id: 'security-settings',
},
backToDashboard: {
id: backToDashboardFragment?.scId,
btnText: backToDashboardFragment?.scTitleEn,
btnLink: backToDashboardFragment?.scDestinationURLEn,
},
},
fr: {
breadcrumb:
response?.data.schPageV1List.items[0].scBreadcrumbParentPages.map(
(level) => {
return {
link: level.scPageNameFr,
text: level.scTitleFr,
}
},
),
pageName: response?.data.schPageV1List.items[0].scTitleFr,
heading: profileIntroFragment?.scContentFr?.json[0].content[0].value,
list: response?.data.schPageV1List.items[0].scFragments
.map((element) => {
if (
element.scId === 'ei-profile-list' ||
element.scId === 'cpp-profile-list' ||
element.scId === 'oas-profile-list' ||
element.scId === 'sin-profile-list'
) {
return {
id: element.scId,
title: element.scTitleFr,
tasks: element.scItems?.map((item) => {
return {
id: item.scId,
title: item.scLinkTextFr,
areaLabel: item.scLinkTextAssistiveFr,
link: buildLink(item.schURLType, item.scDestinationURLFr),
icon: item.scIconCSS,
betaPopUp: item.schBetaPopUp,
}
}),
}
}
})
.filter((e) => e),
lookingFor: {
title: frLookingForFragment?.json[0].content[0].value,
subText: [
frLookingForFragment?.json[1].content[0].value,
frLookingForFragment?.json[1].content[1].value,
],
link: 'parametres-securite',
id: 'security-settings',
},
backToDashboard: {
id: backToDashboardFragment?.scId,
btnText: backToDashboardFragment?.scTitleFr,
btnLink: backToDashboardFragment?.scDestinationURLFr,
},
},
}
return mappedProfile
}
const findFragmentByScId = (res: GetSchProfileV2 | null, id: string) => {
return (
res?.data.schPageV1List.items[0].scFragments.find(
({ scId }) => scId === id,
) ?? null
)
}
// TODO: Check which of these properties should actually be optional and switch to using a question mark instead
export interface ProfileContent {
err?: string
en?:
| {
breadcrumb:
| {
link: string
text: string
}[]
| undefined
pageName: string | undefined
heading: string | undefined
list:
| (
| {
id: string
title: string | undefined
tasks:
| {
id: string
title: string
areaLabel: string
link: string
icon: string
betaPopUp: boolean
}[]
| undefined
}
| undefined
)[]
| undefined
lookingFor: {
title: string | undefined
subText: (string | undefined)[]
link: string
id: string
}
backToDashboard: {
id: string | undefined
btnText: string | undefined
btnLink: string | undefined
}
title?: string | undefined
}
| undefined
fr?: {
breadcrumb:
| {
link: string
text: string
}[]
| undefined
pageName: string | undefined
heading: string | undefined
list:
| (
| {
id: string
title: string | undefined
tasks:
| {
id: string
title: string
areaLabel: string
link: string
icon: string
betaPopUp: boolean
}[]
| undefined
}
| undefined
)[]
| undefined
lookingFor: {
title: string | undefined
subText: (string | undefined)[]
link: string
id: string
}
backToDashboard:
| {
id: string | undefined
btnText: string | undefined
btnLink: string | undefined
}
| undefined
}
}