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

This commit is contained in:
Giuseppe Digilio
2022-10-03 14:07:59 +02:00
parent ac36cc20dc
commit f16dcc7942

View File

@@ -120,11 +120,10 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
this.searchOptions$.subscribe(() => this.updateFilterValueList()), this.searchOptions$.subscribe(() => this.updateFilterValueList()),
this.refreshFilters.asObservable().pipe( this.refreshFilters.asObservable().pipe(
filter((toRefresh: boolean) => toRefresh), filter((toRefresh: boolean) => toRefresh),
).subscribe(() => { mergeMap(() => this.retrieveFilterValues(false))
this.retrieveFilterValues(false); ).subscribe()
})
); );
this.retrieveFilterValues(); this.retrieveFilterValues().subscribe();
} }
/** /**
@@ -279,7 +278,7 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
return getFacetValueForType(facet, this.filterConfig); return getFacetValueForType(facet, this.filterConfig);
} }
protected retrieveFilterValues(useCachedVersionIfAvailable = true) { protected retrieveFilterValues(useCachedVersionIfAvailable = true): Observable<RemoteData<PaginatedList<FacetValue>[]>> {
const facetValues$ = observableCombineLatest([this.searchOptions$, this.currentPage]).pipe( const facetValues$ = observableCombineLatest([this.searchOptions$, this.currentPage]).pipe(
map(([options, page]) => { map(([options, page]) => {
return { options, page }; return { options, page };
@@ -301,40 +300,39 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
); );
let filterValues = []; let filterValues = [];
this.subs.push( return facetValues$.pipe(
facetValues$.pipe( mergeMap((facetOutcome) => {
mergeMap((facetOutcome) => { const newValues$ = facetOutcome.values;
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$];
return this.rdbs.aggregate(filterValues); 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)) const fValue = [].concat(...rd.payload.map((page) => page.page))
.find((facetValue: FacetValue) => this.getFacetValue(facetValue) === value); .find((facetValue: FacetValue) => this.getFacetValue(facetValue) === value);
if (hasValue(fValue)) { if (hasValue(fValue)) {
return fValue; return fValue;
} }
const filterValue = stripOperatorFromFilterValue(value); const filterValue = stripOperatorFromFilterValue(value);
return Object.assign(new FacetValue(), { label: filterValue, value: filterValue }); return Object.assign(new FacetValue(), { label: filterValue, value: filterValue });
}); });
}) })
); );
}) }),
).subscribe((rd: RemoteData<PaginatedList<FacetValue>[]>) => { tap((rd: RemoteData<PaginatedList<FacetValue>[]>) => {
this.animationState = 'ready'; this.animationState = 'ready';
this.filterValues$.next(rd); this.filterValues$.next(rd);
}) })