generated from DTS-STN/next-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsecurity-settings.ts
250 lines (242 loc) · 7 KB
/
security-settings.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
import { buildAemUri, buildLink } from '../../lib/links'
import { cachified } from 'cachified'
import { lruCache as cache, defaultTtl as ttl } from '../../lib/cache-utils'
interface GetSchSecuritySettingsV2 {
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
scContentFr?: {
json: Array<{
nodeType: string
content: Array<{
nodeType: string
value: string
data?: {
href: string
}
}>
style?: string
}>
}
scContentEn?: {
json: Array<{
nodeType: string
content: Array<{
nodeType: string
value: string
data?: {
href: string
}
content?: Array<null>
}>
style?: string
}>
}
scFragments?: Array<{
scId: string
scLinkTextEn: string
scLinkTextFr: string
scLinkTextAssistiveEn: string
scLinkTextAssistiveFr: string
schURLType: string
scDestinationURLEn: string
scDestinationURLFr: string
scDescriptionEn: {
json: Array<{
nodeType: string
content: Array<{
nodeType: string
value: string
}>
}>
}
scDescriptionFr: {
json: Array<{
nodeType: string
content: Array<{
nodeType: string
value: string
}>
}>
}
scButtonType: null
scIconCSS: null
scShortTitleEn: null
scShortTitleFr: null
}>
scDestinationURLEn?: string
scDestinationURLFr?: string
scButtonType?: Array<string>
scTitleEn?: string
scTitleFr?: string
}>
}>
}
}
}
const getCachedContent = () => {
return cachified({
key: `content-security-settings`,
cache,
getFreshValue: async (): Promise<GetSchSecuritySettingsV2 | null> => {
const targetUri = buildAemUri('getSchSecuritySettingsV2')
const response = await fetch(targetUri)
if (!response.ok) return null
return await response.json()
},
ttl,
})
}
export async function getSecuritySettingsContent(): Promise<SecuritySettingsContent> {
const response = await getCachedContent()
const enLookingForFragment =
findFragmentByScId(response, 'looking-for-profile-settings')?.scContentEn ??
null
const enContentFragment =
findFragmentByScId(response, 'security-settings-main-content')
?.scContentEn ?? null
const frLookingForFragment =
findFragmentByScId(response, 'looking-for-profile-settings')?.scContentFr ??
null
const frContentFragment =
findFragmentByScId(response, 'security-settings-main-content')
?.scContentFr ?? null
const securityQuestions =
findFragmentByScId(
response,
'security-settings-main-content',
)?.scFragments?.find(
({ scId }: { scId: string }) => scId === 'security-questions',
) ?? null
const mappedSecurity = {
en: {
breadcrumb:
response?.data.schPageV1List.items[0].scBreadcrumbParentPages.map(
(level) => {
return {
link: level.scPageNameEn,
text: level.scTitleEn,
}
},
),
pageName: response?.data.schPageV1List.items[0].scPageNameEn,
heading: response?.data.schPageV1List.items[0].scTitleEn,
subHeading: enContentFragment?.json[0].content[0].value,
lookingFor: {
title: enLookingForFragment?.json[0].content[0].value,
subText: enLookingForFragment?.json[1].content.map(
({ value }: { value: string }) => {
return value || null
},
),
link: '/profile',
id: 'profile',
},
securityQuestions: {
linkTitle: {
text: securityQuestions?.scLinkTextEn,
link: buildLink(
securityQuestions?.schURLType,
securityQuestions?.scDestinationURLEn ?? '',
),
},
subTitle: securityQuestions?.scDescriptionEn.json[0].content[0].value,
},
},
fr: {
breadcrumb:
response?.data.schPageV1List.items[0].scBreadcrumbParentPages.map(
(level) => {
return {
link: level.scPageNameFr,
text: level.scTitleFr,
}
},
),
pageName: response?.data.schPageV1List.items[0].scPageNameFr,
heading: response?.data.schPageV1List.items[0].scTitleFr,
subHeading: frContentFragment?.json[0].content[0].value,
lookingFor: {
title: frLookingForFragment?.json[0].content[0].value,
subText: frLookingForFragment?.json[1].content.map(
({ value }: { value: string }) => {
return value || null
},
),
link: '/fr/profil',
id: 'profile',
},
securityQuestions: {
linkTitle: {
text: securityQuestions?.scLinkTextFr,
link: buildLink(
securityQuestions?.schURLType,
securityQuestions?.scDestinationURLFr ?? '',
),
},
subTitle: securityQuestions?.scDescriptionFr.json[0].content[0].value,
},
},
}
return mappedSecurity
}
const findFragmentByScId = (
res: GetSchSecuritySettingsV2 | 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 SecuritySettingsContent {
err?: string
en?: {
breadcrumb: { link: string; text: string }[] | undefined
pageName: string | undefined
heading: string | undefined
subHeading: string | undefined
lookingFor: {
title: string | undefined
subText: (string | null)[] | undefined
link: string
id: string
}
securityQuestions: {
linkTitle: { text: string | undefined; link: string }
subTitle: string | undefined
}
}
fr?: {
breadcrumb: { link: string; text: string }[] | undefined
pageName: string | undefined
heading: string | undefined
subHeading: string | undefined
lookingFor: {
title: string | undefined
subText: (string | null)[] | undefined
link: string
id: string
}
securityQuestions: {
linkTitle: { text: string | undefined; link: string }
subTitle: string | undefined
}
}
}