@salesforcedevs/dw-components 0.53.5-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lwc.config.json +16 -0
- package/package.json +20 -0
- package/src/modules/dw/cardBlogPostProvider/cardBlogPostProvider.css +3 -0
- package/src/modules/dw/cardBlogPostProvider/cardBlogPostProvider.html +16 -0
- package/src/modules/dw/cardBlogPostProvider/cardBlogPostProvider.ts +29 -0
- package/src/modules/dw/cardPodcastEpisodeProvider/cardPodcastEpisodeProvider.css +3 -0
- package/src/modules/dw/cardPodcastEpisodeProvider/cardPodcastEpisodeProvider.html +12 -0
- package/src/modules/dw/cardPodcastEpisodeProvider/cardPodcastEpisodeProvider.ts +29 -0
- package/src/modules/dw/contentArchive/altData.js +3 -0
- package/src/modules/dw/contentArchive/contentArchive.css +242 -0
- package/src/modules/dw/contentArchive/contentArchive.html +337 -0
- package/src/modules/dw/contentArchive/contentArchive.ts +457 -0
- package/src/modules/dw/contentArchive/contentArchive.types.ts +6 -0
- package/src/modules/dw/contentArchive/utils.ts +162 -0
- package/src/modules/dw/instrumentation/instrumentation.html +1 -0
- package/src/modules/dw/instrumentation/instrumentation.ts +97 -0
- package/src/modules/dw/newsletterForm/newsletterForm.css +73 -0
- package/src/modules/dw/newsletterForm/newsletterForm.html +73 -0
- package/src/modules/dw/newsletterForm/newsletterForm.ts +238 -0
- package/src/modules/dw/podcastSubscription/podcastSubscription.css +91 -0
- package/src/modules/dw/podcastSubscription/podcastSubscription.html +24 -0
- package/src/modules/dw/podcastSubscription/podcastSubscription.ts +52 -0
- package/src/modules/dw/searchResults/coveo.css +18989 -0
- package/src/modules/dw/searchResults/searchResults.css +387 -0
- package/src/modules/dw/searchResults/searchResults.html +104 -0
- package/src/modules/dw/searchResults/searchResults.ts +187 -0
- package/src/modules/dw/slackCta/slackCta.css +47 -0
- package/src/modules/dw/slackCta/slackCta.html +17 -0
- package/src/modules/dw/slackCta/slackCta.ts +12 -0
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import { LightningElement } from "lwc";
|
|
2
|
+
import cx from "classnames";
|
|
3
|
+
import { getArchivePosts, getArchiveFilterList } from "./utils";
|
|
4
|
+
import { sortSplittedPostsByMonth } from "utils/dates";
|
|
5
|
+
import { Filters } from "./contentArchive.types";
|
|
6
|
+
import { QueryCoordinator } from "utils/queryCoordinator";
|
|
7
|
+
|
|
8
|
+
const MOBILE_MATCH = "767px";
|
|
9
|
+
|
|
10
|
+
const QUERY_SITE_FILTER = "c__filter";
|
|
11
|
+
const PODCASTS = { id: 2, name: "Podcasts" };
|
|
12
|
+
const BLOGS = { id: 1, name: "Blogs" };
|
|
13
|
+
export default class ContentArchive extends LightningElement {
|
|
14
|
+
private _contentTypeList = [
|
|
15
|
+
{ ...BLOGS, checked: false },
|
|
16
|
+
{ ...PODCASTS, checked: false }
|
|
17
|
+
];
|
|
18
|
+
private totalResults: number | null = null;
|
|
19
|
+
private currentPage: number = 1;
|
|
20
|
+
private totalPages: number = 1;
|
|
21
|
+
private loading: boolean = true;
|
|
22
|
+
private filtersLoading: boolean = true;
|
|
23
|
+
private modalOpen: boolean = false;
|
|
24
|
+
private cardType: string = "expanded";
|
|
25
|
+
|
|
26
|
+
private postsByDate: {} = {};
|
|
27
|
+
private dateList: any[] = [];
|
|
28
|
+
private authorList: any[] = [];
|
|
29
|
+
private categoryList: any[] = [];
|
|
30
|
+
|
|
31
|
+
private isMobile: boolean = false;
|
|
32
|
+
private mobileMatchMedia!: MediaQueryList;
|
|
33
|
+
|
|
34
|
+
private selectedFilterMenuData: Filters = {
|
|
35
|
+
site: [],
|
|
36
|
+
dates: [],
|
|
37
|
+
authors: [],
|
|
38
|
+
categories: []
|
|
39
|
+
};
|
|
40
|
+
private posts: any[] = [];
|
|
41
|
+
|
|
42
|
+
get currentPageLength() {
|
|
43
|
+
if (this.loading) {
|
|
44
|
+
return "Loading Results...";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let currentFirstResultNumber = 0;
|
|
48
|
+
const currentLastResultNumber = this.currentPage * this.posts.length;
|
|
49
|
+
|
|
50
|
+
if (this.currentPage > 1) {
|
|
51
|
+
currentFirstResultNumber =
|
|
52
|
+
currentLastResultNumber - this.posts.length;
|
|
53
|
+
} else if (this.currentPage === 1 && !this.isEmptyState) {
|
|
54
|
+
currentFirstResultNumber = 1;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return `Showing ${currentFirstResultNumber} - ${currentLastResultNumber} of ${this.totalResults}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
get showPagination() {
|
|
61
|
+
return this.totalPages > 1;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
get resultsHeading() {
|
|
65
|
+
let pageInfo = "";
|
|
66
|
+
|
|
67
|
+
if (this.currentPage !== 1) {
|
|
68
|
+
pageInfo = `- Page ${this.currentPage}`;
|
|
69
|
+
}
|
|
70
|
+
return this.totalResults ? `Archive Results ${pageInfo}` : "No results";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
get isEmptyState() {
|
|
74
|
+
return this.totalResults === 0 && !this.loading;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
get spinnerSize() {
|
|
78
|
+
return this.isMobile ? "medium" : "large";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
get isMinimalCard() {
|
|
82
|
+
return this.cardType === "minimal";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get minimalCardClass() {
|
|
86
|
+
return cx("min-card-btn", this.cardType === "minimal" && "btn-active");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
get expandedCardClass() {
|
|
90
|
+
return cx("exp-card-btn", this.cardType === "expanded" && "btn-active");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
get postsByMonth() {
|
|
94
|
+
const posts = this.splitPostsByMonth(this.posts);
|
|
95
|
+
|
|
96
|
+
return Object.keys(posts).map((key) => [key, [...posts[key]]]);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
get contentTypeList() {
|
|
100
|
+
return this._contentTypeList;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
get hideBlogFilters() {
|
|
104
|
+
return (
|
|
105
|
+
this.selectedFilterMenuData.site.length === 1 &&
|
|
106
|
+
this.selectedFilterMenuData.site[0].id === 2
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
get contentTypeTitle() {
|
|
111
|
+
return `Content Type ${
|
|
112
|
+
this.selectedFilterMenuData.site.length > 0
|
|
113
|
+
? `(${this.selectedFilterMenuData.site.length})`
|
|
114
|
+
: ""
|
|
115
|
+
}`;
|
|
116
|
+
}
|
|
117
|
+
get dateTitle() {
|
|
118
|
+
return `Date ${
|
|
119
|
+
this.selectedFilterMenuData.dates.length > 0
|
|
120
|
+
? `(${this.selectedFilterMenuData.dates.length})`
|
|
121
|
+
: ""
|
|
122
|
+
}`;
|
|
123
|
+
}
|
|
124
|
+
get authorTitle() {
|
|
125
|
+
return `Blog Author ${
|
|
126
|
+
this.selectedFilterMenuData.authors.length > 0
|
|
127
|
+
? `(${this.selectedFilterMenuData.authors.length})`
|
|
128
|
+
: ""
|
|
129
|
+
}`;
|
|
130
|
+
}
|
|
131
|
+
get categoryTitle() {
|
|
132
|
+
return `Topic ${
|
|
133
|
+
this.selectedFilterMenuData.categories.length > 0
|
|
134
|
+
? `(${this.selectedFilterMenuData.categories.length})`
|
|
135
|
+
: ""
|
|
136
|
+
}`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private filterSelectedDates(
|
|
140
|
+
currentDateList: Array<{ name: string }>,
|
|
141
|
+
selectedDate: string,
|
|
142
|
+
toRemove: boolean = false
|
|
143
|
+
) {
|
|
144
|
+
return currentDateList.filter((currentDateItem: { name: string }) => {
|
|
145
|
+
const year = currentDateItem.name.slice(-4);
|
|
146
|
+
|
|
147
|
+
if (toRemove) {
|
|
148
|
+
return selectedDate.slice(-4) !== currentDateItem.name;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return year !== selectedDate;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private handleUpdateSiteFilter(e: CustomEvent) {
|
|
156
|
+
const { value, name, checked } = e.detail;
|
|
157
|
+
|
|
158
|
+
let selectedFilters: string[] | null = [];
|
|
159
|
+
this._contentTypeList.forEach((option) => {
|
|
160
|
+
if (option.name === value.name) {
|
|
161
|
+
option.checked = checked;
|
|
162
|
+
}
|
|
163
|
+
if (option.checked) {
|
|
164
|
+
selectedFilters?.push(option.name);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
if (selectedFilters?.length === 0) {
|
|
169
|
+
selectedFilters = null;
|
|
170
|
+
}
|
|
171
|
+
QueryCoordinator.setUrlParamValue(QUERY_SITE_FILTER, selectedFilters);
|
|
172
|
+
|
|
173
|
+
this.updateSelectedFilters(value, name, checked);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private handleFiltersEvent(e: CustomEvent) {
|
|
177
|
+
const { value, name, checked } = e.detail;
|
|
178
|
+
this.updateSelectedFilters(value, name, checked);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private updateSelectedFilters(
|
|
182
|
+
value: any,
|
|
183
|
+
name: keyof Filters,
|
|
184
|
+
checked: any
|
|
185
|
+
) {
|
|
186
|
+
let copy = {
|
|
187
|
+
...this.selectedFilterMenuData
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
if (!checked) {
|
|
191
|
+
copy[name] = copy[name].filter(
|
|
192
|
+
(selectedFilterItem: { name: string }) => {
|
|
193
|
+
return selectedFilterItem.name !== value.name;
|
|
194
|
+
}
|
|
195
|
+
);
|
|
196
|
+
} else if (!copy[name].includes(value)) {
|
|
197
|
+
const regex = /^(\d{4})$/g;
|
|
198
|
+
|
|
199
|
+
if (regex.test(value.name) && copy[name].length > 0) {
|
|
200
|
+
copy[name] = this.filterSelectedDates(copy[name], value.name);
|
|
201
|
+
} else if (regex.test(value.name.slice(-4))) {
|
|
202
|
+
copy[name] = this.filterSelectedDates(
|
|
203
|
+
copy[name],
|
|
204
|
+
value.name,
|
|
205
|
+
true
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
copy[name].push(value);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Remove filters when only podcast is selected
|
|
213
|
+
if (
|
|
214
|
+
name === "site" &&
|
|
215
|
+
copy[name].length === 1 &&
|
|
216
|
+
!copy[name].some((item: { id: number }) => item.id === 1)
|
|
217
|
+
) {
|
|
218
|
+
copy = {
|
|
219
|
+
site: [...copy.site],
|
|
220
|
+
categories: [...copy.categories],
|
|
221
|
+
dates: [...copy.dates],
|
|
222
|
+
authors: []
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Checkmark the blog type if value is a blog author
|
|
227
|
+
if (name === "authors" && checked) {
|
|
228
|
+
copy = {
|
|
229
|
+
site: [{ id: 1, name: "Blogs" }],
|
|
230
|
+
categories: [...copy.categories],
|
|
231
|
+
dates: [...copy.dates],
|
|
232
|
+
authors: [...copy.authors]
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
this.selectedFilterMenuData = copy;
|
|
237
|
+
this.currentPage = 1;
|
|
238
|
+
this.updateCheckedStatus();
|
|
239
|
+
this.fetchPostsByCardType();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private updateCheckedStatus() {
|
|
243
|
+
const filterMenuElements =
|
|
244
|
+
this.template.querySelectorAll("dx-filter-menu");
|
|
245
|
+
|
|
246
|
+
filterMenuElements.forEach((filterMenuElement: any) =>
|
|
247
|
+
filterMenuElement.updateCheckedStatus(this.selectedFilterMenuData)
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private removeFilterItem(e: any) {
|
|
252
|
+
const { name, value } = e.target;
|
|
253
|
+
|
|
254
|
+
this.handleUpdateSiteFilter(
|
|
255
|
+
new CustomEvent("updateselectedfilters", {
|
|
256
|
+
detail: {
|
|
257
|
+
name,
|
|
258
|
+
value,
|
|
259
|
+
checked: false
|
|
260
|
+
}
|
|
261
|
+
})
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
private toggleModal() {
|
|
266
|
+
this.modalOpen = !this.modalOpen;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// split posts by month for the minimal view date header
|
|
270
|
+
private splitPostsByMonth(posts: any[]) {
|
|
271
|
+
const postsByMonth: { [key: string]: Array<{}> } = {};
|
|
272
|
+
|
|
273
|
+
posts.forEach((post) => {
|
|
274
|
+
const date = new Date(post.date);
|
|
275
|
+
const month = new Intl.DateTimeFormat("en-US", {
|
|
276
|
+
month: "long"
|
|
277
|
+
}).format(date);
|
|
278
|
+
const year = date.getFullYear();
|
|
279
|
+
|
|
280
|
+
if (!postsByMonth[`${month} ${year}`]) {
|
|
281
|
+
postsByMonth[`${month} ${year}`] = [];
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
postsByMonth[`${month} ${year}`].push({ ...post });
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
return sortSplittedPostsByMonth(postsByMonth);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private handleCardTypeChange(e: any) {
|
|
291
|
+
const selectedCardType = e.target.value;
|
|
292
|
+
if (this.cardType !== selectedCardType) {
|
|
293
|
+
this.cardType = selectedCardType;
|
|
294
|
+
|
|
295
|
+
this.fetchPostsByCardType();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
private handleFilterClear() {
|
|
300
|
+
this.selectedFilterMenuData = {
|
|
301
|
+
site: [],
|
|
302
|
+
dates: [],
|
|
303
|
+
authors: [],
|
|
304
|
+
categories: []
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
this.updateCheckedStatus();
|
|
308
|
+
this.fetchPostsByCardType();
|
|
309
|
+
QueryCoordinator.setUrlParamValue(QUERY_SITE_FILTER, null);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private goToPage(e: CustomEvent) {
|
|
313
|
+
const page = e.detail;
|
|
314
|
+
this.currentPage = page;
|
|
315
|
+
this.totalResults = 0;
|
|
316
|
+
this.totalPages = 0;
|
|
317
|
+
|
|
318
|
+
this.fetchPostsByCardType();
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
private fetchPostsByCardType() {
|
|
322
|
+
if (this.cardType === "minimal") {
|
|
323
|
+
this.fetchPosts(20);
|
|
324
|
+
} else {
|
|
325
|
+
this.fetchPosts(10);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
private async fetchArchiveFilterList() {
|
|
330
|
+
if (!this.dateList.length) {
|
|
331
|
+
this.filtersLoading = true;
|
|
332
|
+
const { dates, authors, categories } = await getArchiveFilterList();
|
|
333
|
+
|
|
334
|
+
this.dateList = this.splitOptionsByDate(dates);
|
|
335
|
+
this.authorList = authors;
|
|
336
|
+
this.categoryList = categories;
|
|
337
|
+
this.filtersLoading = false;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
private async fetchPosts(numberOfPosts: number) {
|
|
342
|
+
this.loading = true;
|
|
343
|
+
|
|
344
|
+
const { posts, totalResults, totalPages } = await getArchivePosts(
|
|
345
|
+
numberOfPosts,
|
|
346
|
+
this.currentPage,
|
|
347
|
+
this.cardType,
|
|
348
|
+
this.selectedFilterMenuData
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
this.posts = posts;
|
|
352
|
+
this.totalResults = totalResults;
|
|
353
|
+
this.totalPages = totalPages;
|
|
354
|
+
this.loading = false;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
private paddedMonth(date: Date) {
|
|
358
|
+
return `${(date.getMonth() + 1).toString().padStart(2, "0")}`;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// split filter options by year/month for nested checkboxes
|
|
362
|
+
private splitOptionsByDate(dateOptions: any) {
|
|
363
|
+
return dateOptions?.reduce((acc: any, curr: any) => {
|
|
364
|
+
const date = new Date(curr.replace(/-/g, "/"));
|
|
365
|
+
const month = new Intl.DateTimeFormat("en-US", {
|
|
366
|
+
month: "long"
|
|
367
|
+
}).format(date);
|
|
368
|
+
const year = String(date.getFullYear());
|
|
369
|
+
|
|
370
|
+
const foundYear = acc.find((post: any) => post.name === year);
|
|
371
|
+
const foundMonth = foundYear?.months.find(
|
|
372
|
+
(m: any) => m.id === month
|
|
373
|
+
);
|
|
374
|
+
|
|
375
|
+
if (!foundYear) {
|
|
376
|
+
acc.push({
|
|
377
|
+
id: year,
|
|
378
|
+
name: year,
|
|
379
|
+
isYear: true,
|
|
380
|
+
months: [
|
|
381
|
+
{
|
|
382
|
+
id: month,
|
|
383
|
+
name: `${month} ${year}`,
|
|
384
|
+
shortName: `${year},${date.getMonth() + 1}`,
|
|
385
|
+
isYear: false,
|
|
386
|
+
numberOfPosts: 1
|
|
387
|
+
}
|
|
388
|
+
],
|
|
389
|
+
totalNumberOfPosts: 1
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
return acc;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (!foundMonth) {
|
|
396
|
+
foundYear.months.push({
|
|
397
|
+
id: month,
|
|
398
|
+
name: `${month} ${year}`,
|
|
399
|
+
shortName: `${year},${date.getMonth() + 1}`,
|
|
400
|
+
isYear: false,
|
|
401
|
+
numberOfPosts: 1
|
|
402
|
+
});
|
|
403
|
+
foundYear.totalNumberOfPosts += 1;
|
|
404
|
+
|
|
405
|
+
return acc;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
foundMonth.numberOfPosts += 1;
|
|
409
|
+
foundYear.totalNumberOfPosts += 1;
|
|
410
|
+
|
|
411
|
+
return acc;
|
|
412
|
+
}, []);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
private onMobileChange = (e: MediaQueryListEvent | MediaQueryList) => {
|
|
416
|
+
this.isMobile = e.matches;
|
|
417
|
+
|
|
418
|
+
requestAnimationFrame(() => this.updateCheckedStatus());
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
connectedCallback(): void {
|
|
422
|
+
this.mobileMatchMedia = window.matchMedia(
|
|
423
|
+
`(max-width: ${MOBILE_MATCH})`
|
|
424
|
+
);
|
|
425
|
+
this.onMobileChange(this.mobileMatchMedia);
|
|
426
|
+
this.mobileMatchMedia.addEventListener("change", this.onMobileChange);
|
|
427
|
+
|
|
428
|
+
const presetFilters: string[] =
|
|
429
|
+
QueryCoordinator.getUrlParamValue(QUERY_SITE_FILTER);
|
|
430
|
+
if (
|
|
431
|
+
presetFilters?.some((filterString) => filterString === BLOGS.name)
|
|
432
|
+
) {
|
|
433
|
+
this.presetSiteFilter(BLOGS);
|
|
434
|
+
}
|
|
435
|
+
if (
|
|
436
|
+
presetFilters?.some(
|
|
437
|
+
(filterString) => filterString === PODCASTS.name
|
|
438
|
+
)
|
|
439
|
+
) {
|
|
440
|
+
this.presetSiteFilter(PODCASTS);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (this.posts.length === 0) {
|
|
444
|
+
this.fetchArchiveFilterList();
|
|
445
|
+
this.fetchPostsByCardType();
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
presetSiteFilter(filter: any) {
|
|
450
|
+
this._contentTypeList.forEach((option) => {
|
|
451
|
+
if (option.name === filter.name) {
|
|
452
|
+
option.checked = true;
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
this.updateSelectedFilters(filter, "site", true);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import qs from "query-string";
|
|
2
|
+
import {
|
|
3
|
+
ArchivePost,
|
|
4
|
+
ArchivePostNormalized,
|
|
5
|
+
FilterMenuData
|
|
6
|
+
} from "typings/custom";
|
|
7
|
+
import altData from "./altData.js";
|
|
8
|
+
|
|
9
|
+
let controller: AbortController | null = null;
|
|
10
|
+
|
|
11
|
+
const ARCHIVE_BASE_URL = "https://developer.salesforce.com/blogs/wp-json/wp/v2";
|
|
12
|
+
const ARCHIVE_CONTENT_URL = `${ARCHIVE_BASE_URL}/latest-post-per-site`;
|
|
13
|
+
const ARCHIVE_FILTER_LIST_URL = `${ARCHIVE_BASE_URL}/archives-filters?order=DESC`;
|
|
14
|
+
|
|
15
|
+
async function postsFetchHandler(url: string) {
|
|
16
|
+
if (controller) {
|
|
17
|
+
controller.abort();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
controller = new AbortController();
|
|
21
|
+
const { signal } = controller;
|
|
22
|
+
|
|
23
|
+
const response = await fetch(url, { signal });
|
|
24
|
+
|
|
25
|
+
if (response.ok) {
|
|
26
|
+
return {
|
|
27
|
+
posts: await response.json(),
|
|
28
|
+
totalPages: Number(response.headers.get("x-wp-totalpages")),
|
|
29
|
+
totalResults: Number(response.headers.get("x-wp-total"))
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
console.error("Error fetching posts");
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
posts: [],
|
|
37
|
+
totalResults: 0,
|
|
38
|
+
totalPages: 0
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function getAudioDuration(duration: string) {
|
|
43
|
+
const durationSplit = duration.split(":");
|
|
44
|
+
const hours = Number(durationSplit[0]);
|
|
45
|
+
const minutes = Number(durationSplit[1]);
|
|
46
|
+
const seconds = Number(durationSplit[2]);
|
|
47
|
+
|
|
48
|
+
const roundSecondsIntoMinutes = Math.round(seconds / 60);
|
|
49
|
+
|
|
50
|
+
return hours
|
|
51
|
+
? `${minutes + roundSecondsIntoMinutes * (hours * 60)} MIN`
|
|
52
|
+
: `${minutes + roundSecondsIntoMinutes} MIN`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function normalizePosts(posts: Array<ArchivePost>, contentType: string) {
|
|
56
|
+
return posts.map((post) => ({
|
|
57
|
+
id: post.id,
|
|
58
|
+
title: post.title.rendered,
|
|
59
|
+
date: post.date,
|
|
60
|
+
contentType: post.sitename,
|
|
61
|
+
href: post.link,
|
|
62
|
+
...(contentType === "expanded" && {
|
|
63
|
+
excerpt: post.excerpt,
|
|
64
|
+
duration: getAudioDuration(post.podcast_audio.duration),
|
|
65
|
+
...(post._embedded &&
|
|
66
|
+
post._embedded.author[0].id && {
|
|
67
|
+
author: post._embedded.author[0],
|
|
68
|
+
authorImgSrc: post._embedded.author[0].avatar_urls["24"],
|
|
69
|
+
topics: post._embedded["wp:term"][0].map(
|
|
70
|
+
(term: any) => term.name
|
|
71
|
+
)
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function getArchivePosts(
|
|
78
|
+
count: number = 10,
|
|
79
|
+
page: number = 1,
|
|
80
|
+
contentType: string = "minimal",
|
|
81
|
+
filterMenuData: FilterMenuData
|
|
82
|
+
): Promise<ArchivePostNormalized> {
|
|
83
|
+
const filterQuery = {
|
|
84
|
+
site: filterMenuData.site.map((siteType) => siteType.id),
|
|
85
|
+
dates: filterMenuData.dates.map((date) =>
|
|
86
|
+
date.shortName ? date.shortName : date.id
|
|
87
|
+
),
|
|
88
|
+
author: filterMenuData.authors.map((author) => author.id),
|
|
89
|
+
category: filterMenuData.categories.map((category) => category.id)
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const COMMON_FIELDS = [
|
|
93
|
+
"id",
|
|
94
|
+
"title",
|
|
95
|
+
"date",
|
|
96
|
+
"sitename",
|
|
97
|
+
"link",
|
|
98
|
+
"podcast_audio"
|
|
99
|
+
];
|
|
100
|
+
const EXPANDED_FIELDS = [
|
|
101
|
+
"excerpt",
|
|
102
|
+
"_embedded",
|
|
103
|
+
"_links.author",
|
|
104
|
+
"_links.wp:term.0"
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
const query: any = {
|
|
108
|
+
...filterQuery,
|
|
109
|
+
_fields: ["id", "title", "date", "sitename", "link"],
|
|
110
|
+
per_page: count,
|
|
111
|
+
page
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
if (contentType === "minimal") {
|
|
115
|
+
query._fields.filter((field: string) =>
|
|
116
|
+
EXPANDED_FIELDS.includes(field)
|
|
117
|
+
);
|
|
118
|
+
delete query._embed;
|
|
119
|
+
} else {
|
|
120
|
+
query._fields = [...COMMON_FIELDS, ...EXPANDED_FIELDS];
|
|
121
|
+
query._embed = ["author", "wp:term"];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const postsUrl = `${qs.stringifyUrl(
|
|
125
|
+
{
|
|
126
|
+
url: ARCHIVE_CONTENT_URL,
|
|
127
|
+
query
|
|
128
|
+
},
|
|
129
|
+
{ arrayFormat: "comma" }
|
|
130
|
+
)}`;
|
|
131
|
+
|
|
132
|
+
// need to format dates with brackets instead of commas
|
|
133
|
+
const urlWithoutDates = qs.pick(
|
|
134
|
+
postsUrl,
|
|
135
|
+
(name: string) => name !== "dates"
|
|
136
|
+
);
|
|
137
|
+
const dateString = qs.stringify(
|
|
138
|
+
{ dates: query.dates },
|
|
139
|
+
{ arrayFormat: "bracket" }
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
const { posts, totalPages, totalResults } = await postsFetchHandler(
|
|
143
|
+
`${urlWithoutDates}${dateString && "&"}${dateString}`
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
posts: normalizePosts(posts, contentType),
|
|
148
|
+
totalPages,
|
|
149
|
+
totalResults
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export async function getArchiveFilterList() {
|
|
154
|
+
const response = await fetch(ARCHIVE_FILTER_LIST_URL);
|
|
155
|
+
|
|
156
|
+
if (response.ok) {
|
|
157
|
+
return response.json();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
console.error("Error fetching filter list, using fallback instead");
|
|
161
|
+
return altData;
|
|
162
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<template></template>
|