Commits

Giuseppe Digilio authored f16dcc79427
[CST-6876] Refactoring in order to remove nested subscriptions
No tags

src/app/shared/search/search-filters/search-filter/search-facet-filter/search-facet-filter.component.ts

Modified
113 113 ngOnInit(): void {
114 114 this.currentUrl = this.router.url;
115 115 this.filterValues$ = new BehaviorSubject(createPendingRemoteDataObject());
116 116 this.currentPage = this.getCurrentPage().pipe(distinctUntilChanged());
117 117
118 118 this.searchOptions$ = this.searchConfigService.searchOptions;
119 119 this.subs.push(
120 120 this.searchOptions$.subscribe(() => this.updateFilterValueList()),
121 121 this.refreshFilters.asObservable().pipe(
122 122 filter((toRefresh: boolean) => toRefresh),
123 - ).subscribe(() => {
124 - this.retrieveFilterValues(false);
125 - })
123 + mergeMap(() => this.retrieveFilterValues(false))
124 + ).subscribe()
126 125 );
127 - this.retrieveFilterValues();
126 + this.retrieveFilterValues().subscribe();
128 127 }
129 128
130 129 /**
131 130 * Prepare for refreshing the values of this filter
132 131 */
133 132 updateFilterValueList() {
134 133 this.animationState = 'loading';
135 134 this.collapseNextUpdate = true;
136 135 this.filter = '';
137 136 }
272 271 }
273 272 }
274 273
275 274 /**
276 275 * Retrieve facet value
277 276 */
278 277 protected getFacetValue(facet: FacetValue): string {
279 278 return getFacetValueForType(facet, this.filterConfig);
280 279 }
281 280
282 - protected retrieveFilterValues(useCachedVersionIfAvailable = true) {
281 + protected retrieveFilterValues(useCachedVersionIfAvailable = true): Observable<RemoteData<PaginatedList<FacetValue>[]>> {
283 282 const facetValues$ = observableCombineLatest([this.searchOptions$, this.currentPage]).pipe(
284 283 map(([options, page]) => {
285 284 return { options, page };
286 285 }),
287 286 switchMap(({ options, page }) => {
288 287 return this.searchService.getFacetValuesFor(this.filterConfig, page, options, null, useCachedVersionIfAvailable)
289 288 .pipe(
290 289 getFirstSucceededRemoteData(),
291 290 tap((rd: RemoteData<FacetValues>) => {
292 291 this.isLastPage$.next(hasNoValue(rd?.payload?.next));
293 292 }),
294 293 map((rd: RemoteData<FacetValues>) => ({
295 294 values: observableOf(rd),
296 295 page: page
297 296 })
298 297 )
299 298 );
300 299 })
301 300 );
302 301
303 302 let filterValues = [];
304 - this.subs.push(
305 - facetValues$.pipe(
306 - mergeMap((facetOutcome) => {
307 - const newValues$ = facetOutcome.values;
308 -
309 - if (this.collapseNextUpdate) {
310 - this.showFirstPageOnly();
311 - facetOutcome.page = 1;
312 - this.collapseNextUpdate = false;
313 - }
314 - if (facetOutcome.page === 1) {
315 - filterValues = [];
316 - }
317 -
318 - filterValues = [...filterValues, newValues$];
319 -
320 - return this.rdbs.aggregate(filterValues);
321 - }),
322 - tap((rd: RemoteData<PaginatedList<FacetValue>[]>) => {
323 - this.selectedValues$ = this.filterService.getSelectedValuesForFilter(this.filterConfig).pipe(
324 - map((selectedValues) => {
325 - return selectedValues.map((value: string) => {
326 - const fValue = [].concat(...rd.payload.map((page) => page.page))
327 - .find((facetValue: FacetValue) => this.getFacetValue(facetValue) === value);
328 - if (hasValue(fValue)) {
329 - return fValue;
330 - }
331 - const filterValue = stripOperatorFromFilterValue(value);
332 - return Object.assign(new FacetValue(), { label: filterValue, value: filterValue });
333 - });
334 - })
335 - );
336 - })
337 - ).subscribe((rd: RemoteData<PaginatedList<FacetValue>[]>) => {
303 + return facetValues$.pipe(
304 + mergeMap((facetOutcome) => {
305 + const newValues$ = facetOutcome.values;
306 +
307 + if (this.collapseNextUpdate) {
308 + this.showFirstPageOnly();
309 + facetOutcome.page = 1;
310 + this.collapseNextUpdate = false;
311 + }
312 + if (facetOutcome.page === 1) {
313 + filterValues = [];
314 + }
315 +
316 + filterValues = [...filterValues, newValues$];
317 +
318 + return this.rdbs.aggregate(filterValues);
319 + }),
320 + tap((rd: RemoteData<PaginatedList<FacetValue>[]>) => {
321 + this.selectedValues$ = this.filterService.getSelectedValuesForFilter(this.filterConfig).pipe(
322 + map((selectedValues) => {
323 + return selectedValues.map((value: string) => {
324 + const fValue = [].concat(...rd.payload.map((page) => page.page))
325 + .find((facetValue: FacetValue) => this.getFacetValue(facetValue) === value);
326 + if (hasValue(fValue)) {
327 + return fValue;
328 + }
329 + const filterValue = stripOperatorFromFilterValue(value);
330 + return Object.assign(new FacetValue(), { label: filterValue, value: filterValue });
331 + });
332 + })
333 + );
334 + }),
335 + tap((rd: RemoteData<PaginatedList<FacetValue>[]>) => {
338 336 this.animationState = 'ready';
339 337 this.filterValues$.next(rd);
340 338 })
341 339 );
342 340 }
343 341
344 342 /**
345 343 * Transforms the facet value string, so if the query matches part of the value, it's emphasized in the value
346 344 * @param {FacetValue} facet The value of the facet as returned by the server
347 345 * @param {string} query The query that was used to search facet values

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut