@@ -18,18 +18,27 @@ import {
1818} from '@angular/router' ;
1919import {
2020 BehaviorSubject ,
21- combineLatest as observableCombineLatest ,
21+ combineLatest ,
22+ from ,
2223 Observable ,
2324 of as observableOf ,
2425 Subscription ,
2526} from 'rxjs' ;
2627import {
28+ debounceTime ,
2729 distinctUntilChanged ,
30+ filter ,
2831 map ,
32+ mergeMap ,
33+ reduce ,
2934 switchMap ,
3035 take ,
3136 tap ,
3237} from 'rxjs/operators' ;
38+ import {
39+ getFacetValueForType ,
40+ stripOperatorFromFilterValue ,
41+ } from 'src/app/shared/search/search.utils' ;
3342
3443import { RemoteDataBuildService } from '../../../../../core/cache/builders/remote-data-build.service' ;
3544import { getFirstSucceededRemoteDataPayload } from '../../../../../core/shared/operators' ;
@@ -40,6 +49,7 @@ import { SEARCH_CONFIG_SERVICE } from '../../../../../my-dspace-page/my-dspace-c
4049import {
4150 hasNoValue ,
4251 hasValue ,
52+ isNotEmpty ,
4353} from '../../../../empty.util' ;
4454import { InputSuggestion } from '../../../../input-suggestions/input-suggestions.model' ;
4555import { currentPath } from '../../../../utils/route.utils' ;
@@ -166,9 +176,33 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
166176 ) ;
167177 this . subs . push (
168178 this . searchOptions$ . subscribe ( ( ) => this . updateFilterValueList ( ) ) ,
179+ this . refreshFilters . asObservable ( ) . pipe (
180+ filter ( ( toRefresh : boolean ) => toRefresh ) ,
181+ // NOTE This is a workaround, otherwise retrieving filter values returns tha old cached response
182+ debounceTime ( ( 100 ) ) ,
183+ mergeMap ( ( ) => this . retrieveFilterValues ( false ) ) ,
184+ ) . subscribe ( ) ,
169185 this . retrieveFilterValues ( ) . subscribe ( ) ,
170186 ) ;
171- this . selectedAppliedFilters$ = this . searchService . getSelectedValuesForFilter ( this . filterConfig . name ) . pipe (
187+
188+ this . selectedAppliedFilters$ = combineLatest ( [
189+ this . searchService . getSelectedValuesForFilter ( this . filterConfig . name ) ,
190+ this . facetValues$ . asObservable ( ) . pipe (
191+ mergeMap ( ( values : FacetValues [ ] ) => from ( values ) . pipe (
192+ reduce ( ( acc : FacetValue [ ] , value : FacetValues ) => acc . concat ( value . page ) , [ ] ) ,
193+ ) ) ,
194+ ) ,
195+ ] ) . pipe (
196+ filter ( ( [ allAppliedFilters , facetValues ] : [ AppliedFilter [ ] , FacetValue [ ] ] ) => isNotEmpty ( facetValues ) ) ,
197+ map ( ( [ allAppliedFilters , facetValues ] : [ AppliedFilter [ ] , FacetValue [ ] ] ) => {
198+ return allAppliedFilters . map ( ( appliedValue ) => {
199+ const fValue = facetValues
200+ . find ( ( facetValue : FacetValue ) => stripOperatorFromFilterValue ( getFacetValueForType ( facetValue , this . filterConfig ) ) === appliedValue . value ) ;
201+ return ( hasValue ( fValue ) ) ? Object . assign ( appliedValue , {
202+ label : fValue . label ,
203+ } ) : appliedValue ;
204+ } ) ;
205+ } ) ,
172206 map ( ( allAppliedFilters : AppliedFilter [ ] ) => allAppliedFilters . filter ( ( appliedFilter : AppliedFilter ) => FACET_OPERATORS . includes ( appliedFilter . operator ) ) ) ,
173207 distinctUntilChanged ( ( previous : AppliedFilter [ ] , next : AppliedFilter [ ] ) => JSON . stringify ( previous ) === JSON . stringify ( next ) ) ,
174208 ) ;
@@ -287,9 +321,9 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
287321 * Retrieves all the filter value suggestion pages that need to be displayed in the facet and combines it into one
288322 * list.
289323 */
290- protected retrieveFilterValues ( ) : Observable < FacetValues [ ] > {
291- return observableCombineLatest ( [ this . searchOptions$ , this . currentPage ] ) . pipe (
292- switchMap ( ( [ options , page ] : [ SearchOptions , number ] ) => this . searchService . getFacetValuesFor ( this . filterConfig , page , options ) . pipe (
324+ protected retrieveFilterValues ( useCachedVersionIfAvailable = true ) : Observable < FacetValues [ ] > {
325+ return combineLatest ( [ this . searchOptions$ , this . currentPage ] ) . pipe (
326+ switchMap ( ( [ options , page ] : [ SearchOptions , number ] ) => this . searchService . getFacetValuesFor ( this . filterConfig , page , options , null , useCachedVersionIfAvailable ) . pipe (
293327 getFirstSucceededRemoteDataPayload ( ) ,
294328 tap ( ( facetValues : FacetValues ) => {
295329 this . isLastPage$ . next ( hasNoValue ( facetValues ?. next ) ) ;
0 commit comments