@salesforcedevs/dw-components 0.71.0 → 0.72.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforcedevs/dw-components",
3
- "version": "0.71.0",
3
+ "version": "0.72.0",
4
4
  "description": "Lightning web components for https://developer.salesforce.com",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -22,5 +22,5 @@
22
22
  "@types/lodash.orderby": "^4.6.7",
23
23
  "@types/lodash.uniqby": "^4.7.7"
24
24
  },
25
- "gitHead": "4a5210f6d819521bdad4cd5a21ffc38051c7bd75"
25
+ "gitHead": "fd347f27dc9472a9ead4f35f701b8914ba9f872e"
26
26
  }
@@ -65,7 +65,7 @@
65
65
  if:true={showArchives}
66
66
  onclick={onClick}
67
67
  variant="link"
68
- href='/content-archive?c__filter=%5B"Blogs"%5D'
68
+ href='/content-archive?site=Blogs'
69
69
  text="VIEW ARCHIVES"
70
70
  ></dx-hr>
71
71
  </div>
@@ -26,7 +26,7 @@
26
26
  </button>
27
27
  </div>
28
28
  <dx-filter-menu
29
- name="site"
29
+ name="content-type"
30
30
  title={contentTypeTitle}
31
31
  options={contentTypeList}
32
32
  onchange={handleUpdateSiteFilter}
@@ -74,7 +74,7 @@
74
74
  </button>
75
75
  </div>
76
76
  <dx-filter-menu
77
- name="site"
77
+ name="contentType"
78
78
  title={contentTypeTitle}
79
79
  options={contentTypeList}
80
80
  onchange={handleUpdateSiteFilter}
@@ -158,17 +158,17 @@
158
158
  </template>
159
159
  </div>
160
160
  <div class="filter-chips-container">
161
- <template if:true={selectedFilterMenuData.site.length}>
161
+ <template if:true={selectedFilterMenuData.contentType.length}>
162
162
  <div class="chip-item-container">
163
163
  <p class="chip-title">Type</p>
164
164
  <template
165
- for:each={selectedFilterMenuData.site}
165
+ for:each={selectedFilterMenuData.contentType}
166
166
  for:item="postType"
167
167
  >
168
168
  <span class="chip-item" key={postType.name}>
169
169
  {postType.name}
170
170
  <dx-button
171
- name="site"
171
+ name="contentType"
172
172
  value={postType}
173
173
  onclick={removeFilterItem}
174
174
  variant="inline"
@@ -187,7 +187,7 @@
187
187
  for:each={selectedFilterMenuData.dates}
188
188
  for:item="date"
189
189
  >
190
- <span class="chip-item" key={date}>
190
+ <span class="chip-item" key={date.name}>
191
191
  {date.name}
192
192
  <dx-button
193
193
  name="dates"
@@ -2,12 +2,23 @@ import { LightningElement } from "lwc";
2
2
  import cx from "classnames";
3
3
  import { PER_PAGE_COUNT, getArchivePosts, getArchiveFilterList } from "./utils";
4
4
  import { sortSplittedPostsByMonth } from "dxUtils/dates";
5
- import { Filters } from "./contentArchive.types";
6
5
  import { getUrlParamValue, setUrlParamValue } from "dxUtils/queryCoordinator";
6
+ import {
7
+ Filters,
8
+ FilterMenuData,
9
+ FilterMenuNames,
10
+ FilterMenuItem
11
+ } from "./contentArchive.types";
7
12
 
8
13
  const MOBILE_MATCH = "767px";
9
14
 
10
- const QUERY_SITE_FILTER = "content-type";
15
+ enum FilterNames {
16
+ QUERY_CONTENT_FILTER = "contentType",
17
+ QUERY_AUTHOR_FILTER = "authors",
18
+ QUERY_DATES_FILTER = "dates",
19
+ QUERY_CATEGORIES_FILTER = "categories"
20
+ }
21
+
11
22
  const PODCASTS = { id: 2, name: "Podcasts" };
12
23
  const BLOGS = { id: 1, name: "Blogs" };
13
24
  export default class ContentArchive extends LightningElement {
@@ -22,7 +33,7 @@ export default class ContentArchive extends LightningElement {
22
33
  private filtersLoading: boolean = true;
23
34
  private filtersDatesLoading: boolean = false;
24
35
  private modalOpen: boolean = false;
25
- private cardType: string = "expanded";
36
+ private cardType: "expanded" | "minimal" = "expanded";
26
37
 
27
38
  private postsByDate: {} = {};
28
39
  private dateList: any[] = [];
@@ -33,13 +44,18 @@ export default class ContentArchive extends LightningElement {
33
44
  private mobileMatchMedia!: MediaQueryList;
34
45
 
35
46
  private selectedFilterMenuData: Filters = {
36
- site: [],
47
+ contentType: [],
37
48
  dates: [],
38
49
  authors: [],
39
50
  categories: []
40
51
  };
52
+
41
53
  private posts: any[] = [];
42
54
 
55
+ get isEmptyState() {
56
+ return this.totalResults === 0 && !this.loading;
57
+ }
58
+
43
59
  get currentPageLength() {
44
60
  if (this.loading) {
45
61
  return "Loading Results...";
@@ -65,10 +81,6 @@ export default class ContentArchive extends LightningElement {
65
81
  return this.totalResults ? `Archive Results ${pageInfo}` : "No results";
66
82
  }
67
83
 
68
- get isEmptyState() {
69
- return this.totalResults === 0 && !this.loading;
70
- }
71
-
72
84
  get spinnerSize() {
73
85
  return this.isMobile ? "medium" : "large";
74
86
  }
@@ -97,15 +109,15 @@ export default class ContentArchive extends LightningElement {
97
109
 
98
110
  get hideBlogFilters() {
99
111
  return (
100
- this.selectedFilterMenuData.site.length === 1 &&
101
- this.selectedFilterMenuData.site[0].id === 2
112
+ this.selectedFilterMenuData.contentType.length === 1 &&
113
+ this.selectedFilterMenuData.contentType[0].id === 2
102
114
  );
103
115
  }
104
116
 
105
117
  get contentTypeTitle() {
106
118
  return `Content Type ${
107
- this.selectedFilterMenuData.site.length > 0
108
- ? `(${this.selectedFilterMenuData.site.length})`
119
+ this.selectedFilterMenuData.contentType.length > 0
120
+ ? `(${this.selectedFilterMenuData.contentType.length})`
109
121
  : ""
110
122
  }`;
111
123
  }
@@ -132,16 +144,16 @@ export default class ContentArchive extends LightningElement {
132
144
  }
133
145
 
134
146
  get selectedPostType() {
135
- return this.selectedFilterMenuData.site.length === 1
136
- ? this.selectedFilterMenuData.site[0].name.toLowerCase()
147
+ return this.selectedFilterMenuData.contentType.length === 1
148
+ ? this.selectedFilterMenuData.contentType[0].name.toLowerCase()
137
149
  : "all";
138
150
  }
139
151
 
140
152
  private filterSelectedDates(
141
- currentDateList: Array<{ name: string }>,
142
153
  selectedDate: string,
143
154
  toRemove: boolean = false
144
155
  ) {
156
+ const currentDateList = this.selectedFilterMenuData.dates;
145
157
  return currentDateList.filter((currentDateItem: { name: string }) => {
146
158
  const year = currentDateItem.name.slice(-4);
147
159
 
@@ -170,8 +182,6 @@ export default class ContentArchive extends LightningElement {
170
182
  selectedFilters = null;
171
183
  }
172
184
 
173
- setUrlParamValue(QUERY_SITE_FILTER, selectedFilters);
174
-
175
185
  this.updateSelectedFilters(value, name, checked);
176
186
  this.fetchArchiveFilterDateList();
177
187
  }
@@ -183,8 +193,9 @@ export default class ContentArchive extends LightningElement {
183
193
 
184
194
  private updateSelectedFilters(
185
195
  value: any,
186
- name: keyof Filters,
187
- checked: any
196
+ name: FilterMenuNames,
197
+ checked: any,
198
+ suppressFetch: boolean = false
188
199
  ) {
189
200
  let copy = {
190
201
  ...this.selectedFilterMenuData
@@ -200,26 +211,25 @@ export default class ContentArchive extends LightningElement {
200
211
  const regex = /^(\d{4})$/g;
201
212
 
202
213
  if (regex.test(value.name) && copy[name].length > 0) {
203
- copy[name] = this.filterSelectedDates(copy[name], value.name);
214
+ copy[name] = this.filterSelectedDates(value.name);
204
215
  } else if (regex.test(value.name.slice(-4))) {
205
- copy[name] = this.filterSelectedDates(
206
- copy[name],
207
- value.name,
208
- true
209
- );
216
+ copy[name] = this.filterSelectedDates(value.name, true);
210
217
  }
211
218
 
212
- copy[name].push(value);
219
+ if (!copy[name].find((item) => item.name === value.name)) {
220
+ copy[name].push(value);
221
+ }
213
222
  }
214
223
 
215
224
  // Remove filters when only podcast is selected
216
225
  if (
217
- name === "site" &&
226
+ name === FilterNames.QUERY_CONTENT_FILTER &&
218
227
  copy[name].length === 1 &&
219
228
  !copy[name].some((item: { id: number }) => item.id === 1)
220
229
  ) {
230
+ setUrlParamValue({ QUERY_AUTHOR_FILTER: [] });
221
231
  copy = {
222
- site: [...copy.site],
232
+ contentType: [...copy.contentType],
223
233
  categories: [...copy.categories],
224
234
  dates: [...copy.dates],
225
235
  authors: []
@@ -228,21 +238,26 @@ export default class ContentArchive extends LightningElement {
228
238
 
229
239
  // Checkmark the blog type if value is a blog author
230
240
  if (name === "authors" && checked) {
241
+ setUrlParamValue({ [FilterNames.QUERY_CONTENT_FILTER]: ["Blogs"] });
231
242
  copy = {
232
- site: [{ id: 1, name: "Blogs" }],
243
+ contentType: [{ id: 1, name: "Blogs" }],
233
244
  categories: [...copy.categories],
234
245
  dates: [...copy.dates],
235
246
  authors: [...copy.authors]
236
247
  };
237
248
  }
238
249
 
250
+ setUrlParamValue({ [name]: copy[name].map((a) => a.name) });
251
+
239
252
  this.selectedFilterMenuData = copy;
240
253
  this.currentPage = 1;
241
254
  this.updateCheckedStatus();
242
- this.fetchPostsByCardType();
255
+ if (!suppressFetch) {
256
+ this.fetchPostsByCardType();
257
+ }
243
258
  }
244
259
 
245
- private updateCheckedStatus() {
260
+ private async updateCheckedStatus() {
246
261
  const filterMenuElements =
247
262
  this.template.querySelectorAll("dx-filter-menu");
248
263
 
@@ -301,7 +316,7 @@ export default class ContentArchive extends LightningElement {
301
316
 
302
317
  private handleFilterClear() {
303
318
  this.selectedFilterMenuData = {
304
- site: [],
319
+ contentType: [],
305
320
  dates: [],
306
321
  authors: [],
307
322
  categories: []
@@ -310,7 +325,12 @@ export default class ContentArchive extends LightningElement {
310
325
  this.updateCheckedStatus();
311
326
  this.fetchPostsByCardType();
312
327
 
313
- setUrlParamValue(QUERY_SITE_FILTER, null);
328
+ setUrlParamValue({
329
+ [FilterNames.QUERY_CONTENT_FILTER]: [],
330
+ [FilterNames.QUERY_AUTHOR_FILTER]: [],
331
+ [FilterNames.QUERY_CATEGORIES_FILTER]: [],
332
+ [FilterNames.QUERY_DATES_FILTER]: []
333
+ });
314
334
  }
315
335
 
316
336
  private goToPage(e: CustomEvent) {
@@ -342,6 +362,8 @@ export default class ContentArchive extends LightningElement {
342
362
  this.authorList = authors;
343
363
  this.categoryList = categories;
344
364
  this.filtersLoading = false;
365
+
366
+ requestAnimationFrame(() => this.updateAllFiltersFromUrl());
345
367
  }
346
368
  }
347
369
 
@@ -358,12 +380,12 @@ export default class ContentArchive extends LightningElement {
358
380
  private async fetchPosts(numberOfPosts: number) {
359
381
  this.loading = true;
360
382
 
361
- const { posts, totalResults, totalPages } = await getArchivePosts(
362
- numberOfPosts,
363
- this.currentPage,
364
- this.cardType,
365
- this.selectedFilterMenuData
366
- );
383
+ const { posts, totalResults, totalPages } = await getArchivePosts({
384
+ count: numberOfPosts,
385
+ page: this.currentPage,
386
+ contentType: this.cardType,
387
+ filterMenuData: this.selectedFilterMenuData
388
+ });
367
389
 
368
390
  this.posts = posts;
369
391
  this.totalResults = totalResults;
@@ -442,46 +464,56 @@ export default class ContentArchive extends LightningElement {
442
464
  this.onMobileChange(this.mobileMatchMedia);
443
465
  this.mobileMatchMedia.addEventListener("change", this.onMobileChange);
444
466
 
445
- const presetFilters: Array<string> =
446
- getUrlParamValue(QUERY_SITE_FILTER);
447
-
448
- if (presetFilters.length === 1) {
449
- if (presetFilters[0] === BLOGS.name) {
450
- this.presetSiteFilter(BLOGS);
451
- } else {
452
- this.presetSiteFilter(PODCASTS);
453
- }
454
- } else if (presetFilters.length === 2) {
455
- this.presetSiteFilter();
456
- }
457
-
458
467
  if (this.posts.length === 0) {
459
468
  this.fetchArchiveFilterList();
460
469
  this.fetchPostsByCardType();
461
470
  }
462
471
  }
463
472
 
464
- presetSiteFilter(
465
- filter: { id: number; name: string } | undefined = undefined
466
- ) {
467
- // check both content types when no filter is provided
468
- if (!filter) {
469
- this.selectedFilterMenuData = {
470
- ...this.selectedFilterMenuData,
471
- site: [BLOGS, PODCASTS]
472
- };
473
- this._contentTypeList.forEach((option) => {
474
- option.checked = true;
475
- });
476
- this.updateCheckedStatus();
477
- } else {
478
- // set either blogs OR podcasts
479
- this._contentTypeList.forEach((option) => {
480
- if (option.name === filter.name) {
481
- option.checked = true;
473
+ private findDateFromList(nameToFind: string, list: FilterMenuItem[]) {
474
+ return (
475
+ list
476
+ .map((item: any) => item.months)
477
+ .flat()
478
+ .find(
479
+ (month) =>
480
+ month.name.toLowerCase() === nameToFind.toLowerCase()
481
+ ) ||
482
+ list.find(
483
+ (item: any) =>
484
+ item.name.toLowerCase() === nameToFind.toLowerCase()
485
+ )
486
+ );
487
+ }
488
+
489
+ private updateAllFiltersFromUrl() {
490
+ const filters: FilterMenuData = {
491
+ [FilterNames.QUERY_AUTHOR_FILTER]: this.authorList,
492
+ [FilterNames.QUERY_DATES_FILTER]: this.dateList,
493
+ [FilterNames.QUERY_CATEGORIES_FILTER]: this.categoryList,
494
+ [FilterNames.QUERY_CONTENT_FILTER]: this.contentTypeList
495
+ };
496
+
497
+ for (const [key, items] of Object.entries(filters)) {
498
+ const presetFilter: Array<string> = getUrlParamValue(key);
499
+
500
+ presetFilter.forEach((item) => {
501
+ const value =
502
+ key === "dates"
503
+ ? this.findDateFromList(item, items)
504
+ : items.find(
505
+ (i) => i.name.toLowerCase() === item.toLowerCase()
506
+ );
507
+
508
+ if (value) {
509
+ this.updateSelectedFilters(
510
+ value,
511
+ key as FilterMenuNames,
512
+ true,
513
+ true
514
+ );
482
515
  }
483
516
  });
484
- this.updateSelectedFilters(filter, "site", true);
485
517
  }
486
518
  }
487
519
  }
@@ -1,10 +1,3 @@
1
- export interface Filters {
2
- site: any[];
3
- dates: any[];
4
- authors: any[];
5
- categories: any[];
6
- }
7
-
8
1
  export type ArchivePostAuthor = {
9
2
  id: number;
10
3
  link: string;
@@ -73,14 +66,32 @@ export type ArchiveContent = {
73
66
  totalPages: number;
74
67
  };
75
68
 
69
+ export type FilterMenuNames =
70
+ | "contentType"
71
+ | "dates"
72
+ | "authors"
73
+ | "categories";
74
+
75
+ export type Filters = {
76
+ [key in FilterMenuNames]: any[];
77
+ };
78
+
76
79
  export type FilterMenuItem = {
77
- id: string;
80
+ id: string | number;
78
81
  name: string;
79
82
  shortName?: string;
83
+ checked?: boolean;
84
+ months?: Array<{
85
+ id: string;
86
+ name: string;
87
+ shortName: string;
88
+ isYear: boolean;
89
+ numberOfPosts: number;
90
+ }>;
80
91
  };
81
92
 
82
93
  export type FilterMenuData = {
83
- site: Array<FilterMenuItem>;
94
+ contentType: Array<FilterMenuItem>;
84
95
  dates: Array<FilterMenuItem>;
85
96
  categories: Array<FilterMenuItem>;
86
97
  authors: Array<FilterMenuItem>;
@@ -2,11 +2,13 @@ import qs from "query-string";
2
2
  import {
3
3
  ArchivePost,
4
4
  ArchivePostNormalized,
5
- FilterMenuData
5
+ FilterMenuData,
6
+ FilterMenuItem
6
7
  } from "./contentArchive.types";
7
8
  import altData from "./altData.js";
8
9
 
9
- let controller: AbortController | null = null;
10
+ let postsController: AbortController | null = null;
11
+ let filtersController: AbortController | null = null;
10
12
 
11
13
  const ARCHIVE_BASE_URL = "https://developer.salesforce.com/blogs/wp-json/wp/v2";
12
14
  const ARCHIVE_CONTENT_URL = `${ARCHIVE_BASE_URL}/latest-post-per-site`;
@@ -14,12 +16,12 @@ const ARCHIVE_FILTER_LIST_URL = `${ARCHIVE_BASE_URL}/archives-filters?order=DESC
14
16
  export const PER_PAGE_COUNT = 10;
15
17
 
16
18
  async function postsFetchHandler(url: string) {
17
- if (controller) {
18
- controller.abort();
19
+ if (postsController) {
20
+ postsController.abort();
19
21
  }
20
22
 
21
- controller = new AbortController();
22
- const { signal } = controller;
23
+ postsController = new AbortController();
24
+ const { signal } = postsController;
23
25
 
24
26
  const response = await fetch(url, { signal });
25
27
 
@@ -75,19 +77,35 @@ function normalizePosts(posts: Array<ArchivePost>, contentType: string) {
75
77
  }));
76
78
  }
77
79
 
78
- export async function getArchivePosts(
80
+ export async function getArchivePosts({
79
81
  count = PER_PAGE_COUNT,
80
- page: number = 1,
81
- contentType: string = "minimal",
82
- filterMenuData: FilterMenuData
83
- ): Promise<ArchivePostNormalized> {
82
+ page = 1,
83
+ contentType = "minimal",
84
+ filterMenuData = {
85
+ contentType: [],
86
+ dates: [],
87
+ authors: [],
88
+ categories: []
89
+ }
90
+ }: {
91
+ count: number;
92
+ page: number;
93
+ contentType: "minimal" | "expanded";
94
+ filterMenuData: FilterMenuData;
95
+ }): Promise<ArchivePostNormalized> {
84
96
  const filterQuery = {
85
- site: filterMenuData.site.map((siteType) => siteType.id),
86
- dates: filterMenuData.dates.map((date) =>
97
+ site: filterMenuData.contentType.map(
98
+ (siteType: FilterMenuItem) => siteType.id
99
+ ),
100
+ dates: filterMenuData.dates.map((date: FilterMenuItem) =>
87
101
  date.shortName ? date.shortName : date.id
88
102
  ),
89
- author: filterMenuData.authors.map((author) => author.id),
90
- category: filterMenuData.categories.map((category) => category.id)
103
+ author: filterMenuData.authors.map(
104
+ (author: FilterMenuItem) => author.id
105
+ ),
106
+ category: filterMenuData.categories.map(
107
+ (category: FilterMenuItem) => category.id
108
+ )
91
109
  };
92
110
 
93
111
  const COMMON_FIELDS = [
@@ -152,10 +170,18 @@ export async function getArchivePosts(
152
170
  }
153
171
 
154
172
  export async function getArchiveFilterList(postType: string = "all") {
173
+ if (filtersController) {
174
+ filtersController.abort();
175
+ }
176
+
177
+ filtersController = new AbortController();
178
+ const { signal } = filtersController;
179
+
155
180
  const response = await fetch(
156
181
  `${ARCHIVE_FILTER_LIST_URL}${
157
182
  postType !== "all" ? `&post-type=${postType}` : ""
158
- }`
183
+ }`,
184
+ { signal }
159
185
  );
160
186
 
161
187
  if (response.ok) {
@@ -22,7 +22,7 @@
22
22
  if:true={showArchives}
23
23
  onclick={onClick}
24
24
  variant="link"
25
- href='/content-archive?c__filter=%5B"Podcasts"%5D'
25
+ href='/content-archive?site=Podcasts'
26
26
  text="VIEW ARCHIVES"
27
27
  ></dx-hr>
28
28
  </div>