[CST-6876] Refactoring in order to remove nested subscriptions

This commit is contained in:
Giuseppe Digilio
2022-10-03 10:58:51 +02:00
parent bbdaa253aa
commit ac36cc20dc

View File

@@ -1,3 +1,7 @@
import { animate, state, style, transition, trigger } from '@angular/animations';
import { Component, Inject, OnDestroy, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { import {
BehaviorSubject, BehaviorSubject,
combineLatest as observableCombineLatest, combineLatest as observableCombineLatest,
@@ -6,10 +10,8 @@ import {
Subject, Subject,
Subscription Subscription
} from 'rxjs'; } from 'rxjs';
import { distinctUntilChanged, filter, map, switchMap, take, tap } from 'rxjs/operators'; import { distinctUntilChanged, filter, map, mergeMap, switchMap, take, tap } from 'rxjs/operators';
import { animate, state, style, transition, trigger } from '@angular/animations';
import { Component, Inject, OnDestroy, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { RemoteDataBuildService } from '../../../../../core/cache/builders/remote-data-build.service'; import { RemoteDataBuildService } from '../../../../../core/cache/builders/remote-data-build.service';
import { PaginatedList } from '../../../../../core/data/paginated-list.model'; import { PaginatedList } from '../../../../../core/data/paginated-list.model';
import { RemoteData } from '../../../../../core/data/remote-data'; import { RemoteData } from '../../../../../core/data/remote-data';
@@ -32,6 +34,7 @@ import { SEARCH_CONFIG_SERVICE } from '../../../../../my-dspace-page/my-dspace-p
import { currentPath } from '../../../../utils/route.utils'; import { currentPath } from '../../../../utils/route.utils';
import { getFacetValueForType, stripOperatorFromFilterValue } from '../../../search.utils'; import { getFacetValueForType, stripOperatorFromFilterValue } from '../../../search.utils';
import { createPendingRemoteDataObject } from '../../../../remote-data.utils'; import { createPendingRemoteDataObject } from '../../../../remote-data.utils';
import { FacetValues } from '../../../models/facet-values.model';
@Component({ @Component({
selector: 'ds-search-facet-filter', selector: 'ds-search-facet-filter',
@@ -76,6 +79,7 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
* Emits the active values for this filter * Emits the active values for this filter
*/ */
selectedValues$: Observable<FacetValue[]>; selectedValues$: Observable<FacetValue[]>;
protected collapseNextUpdate = true; protected collapseNextUpdate = true;
/** /**
@@ -284,38 +288,43 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
return this.searchService.getFacetValuesFor(this.filterConfig, page, options, null, useCachedVersionIfAvailable) return this.searchService.getFacetValuesFor(this.filterConfig, page, options, null, useCachedVersionIfAvailable)
.pipe( .pipe(
getFirstSucceededRemoteData(), getFirstSucceededRemoteData(),
map((results) => { tap((rd: RemoteData<FacetValues>) => {
return { this.isLastPage$.next(hasNoValue(rd?.payload?.next));
values: observableOf(results), }),
page: page map((rd: RemoteData<FacetValues>) => ({
}; values: observableOf(rd),
} page: page
})
) )
); );
}) })
); );
let filterValues = []; let filterValues = [];
this.subs.push(facetValues$.subscribe((facetOutcome) => { this.subs.push(
const newValues$ = facetOutcome.values; facetValues$.pipe(
mergeMap((facetOutcome) => {
const newValues$ = facetOutcome.values;
if (this.collapseNextUpdate) { if (this.collapseNextUpdate) {
this.showFirstPageOnly(); this.showFirstPageOnly();
facetOutcome.page = 1; facetOutcome.page = 1;
this.collapseNextUpdate = false; this.collapseNextUpdate = false;
} }
if (facetOutcome.page === 1) { if (facetOutcome.page === 1) {
filterValues = []; filterValues = [];
} }
filterValues = [...filterValues, newValues$]; filterValues = [...filterValues, newValues$];
this.subs.push(this.rdbs.aggregate(filterValues).pipe( return this.rdbs.aggregate(filterValues);
}),
tap((rd: RemoteData<PaginatedList<FacetValue>[]>) => { tap((rd: RemoteData<PaginatedList<FacetValue>[]>) => {
this.selectedValues$ = this.filterService.getSelectedValuesForFilter(this.filterConfig).pipe( this.selectedValues$ = this.filterService.getSelectedValuesForFilter(this.filterConfig).pipe(
map((selectedValues) => { map((selectedValues) => {
return selectedValues.map((value: string) => { return selectedValues.map((value: string) => {
const fValue = [].concat(...rd.payload.map((page) => page.page)).find((facetValue: FacetValue) => this.getFacetValue(facetValue) === value); const fValue = [].concat(...rd.payload.map((page) => page.page))
.find((facetValue: FacetValue) => this.getFacetValue(facetValue) === value);
if (hasValue(fValue)) { if (hasValue(fValue)) {
return fValue; return fValue;
} }
@@ -328,12 +337,8 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
).subscribe((rd: RemoteData<PaginatedList<FacetValue>[]>) => { ).subscribe((rd: RemoteData<PaginatedList<FacetValue>[]>) => {
this.animationState = 'ready'; this.animationState = 'ready';
this.filterValues$.next(rd); this.filterValues$.next(rd);
})
})); );
this.subs.push(newValues$.pipe(take(1)).subscribe((rd) => {
this.isLastPage$.next(hasNoValue(rd.payload.next));
}));
}));
} }
/** /**