Skip to content

Commit

Permalink
111731: Force the isActive state to be recalculated when the scope is…
Browse files Browse the repository at this point in the history
… updated

The BehaviorSubject was necessary because when the result of SearchService#getSelectedValuesForFilter is already cached, the isActive state can be recalculated even before the @input() scope is updated
  • Loading branch information
alexandrevryghem committed May 7, 2024
1 parent 2dbf69e commit ede5f43
Showing 1 changed file with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Component,
Inject,
Input,
OnChanges,
OnDestroy,
OnInit,
} from '@angular/core';
Expand Down Expand Up @@ -56,7 +57,7 @@ import { SearchFacetFilterWrapperComponent } from './search-facet-filter-wrapper
/**
* Represents a part of the filter section for a single type of filter
*/
export class SearchFilterComponent implements OnInit, OnDestroy {
export class SearchFilterComponent implements OnInit, OnChanges, OnDestroy {
/**
* The filter config for this component
*/
Expand All @@ -65,7 +66,7 @@ export class SearchFilterComponent implements OnInit, OnDestroy {
/**
* True when the search component should show results on the current page
*/
@Input() inPlaceSearch;
@Input() inPlaceSearch: boolean;

/**
* Emits when the search filters values may be stale, and so they must be refreshed.
Expand Down Expand Up @@ -107,6 +108,11 @@ export class SearchFilterComponent implements OnInit, OnDestroy {
*/
active$: Observable<boolean>;

/**
* The current scope as an observable in order to be able to re-trigger the {@link appliedFilters$}
*/
scope$: BehaviorSubject<string> = new BehaviorSubject(undefined);

subs: Subscription[] = [];

private readonly sequenceId: number;
Expand Down Expand Up @@ -137,6 +143,12 @@ export class SearchFilterComponent implements OnInit, OnDestroy {
}));
}

ngOnChanges(): void {
if (this.scope$.value !== this.scope) {
this.scope$.next(this.scope);
}
}

ngOnDestroy(): void {
this.subs.forEach((sub: Subscription) => sub.unsubscribe());
}
Expand Down Expand Up @@ -214,13 +226,14 @@ export class SearchFilterComponent implements OnInit, OnDestroy {
return combineLatest([
this.appliedFilters$,
this.searchConfigService.searchOptions,
this.scope$,
]).pipe(
switchMap(([selectedValues, options]: [AppliedFilter[], SearchOptions]) => {
switchMap(([selectedValues, options, scope]: [AppliedFilter[], SearchOptions, string]) => {
if (isNotEmpty(selectedValues.filter((appliedFilter: AppliedFilter) => FACET_OPERATORS.includes(appliedFilter.operator)))) {
return observableOf(true);
} else {
if (hasValue(this.scope)) {
options.scope = this.scope;
if (hasValue(scope)) {
options.scope = scope;
}
return this.searchService.getFacetValuesFor(this.filter, 1, options).pipe(
filter((RD: RemoteData<FacetValues>) => !RD.isLoading),
Expand Down

0 comments on commit ede5f43

Please sign in to comment.