Skip to content

Commit f08d8ee

Browse files
atarix83vins01-4science
authored andcommitted
Merged in task/main-cris/DSC-2337 (pull request DSpace#3162)
Task/main cris/DSC-2337 Approved-by: Vincenzo Mecca
2 parents b402c4c + f2ac921 commit f08d8ee

File tree

3 files changed

+90
-6
lines changed

3 files changed

+90
-6
lines changed

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

+50
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { PageInfo } from '../../../../../core/shared/page-info.model';
2222
import { SearchService } from '../../../../../core/shared/search/search.service';
2323
import { SearchFilterService } from '../../../../../core/shared/search/search-filter.service';
2424
import { SEARCH_CONFIG_SERVICE } from '../../../../../my-dspace-page/my-dspace-configuration.service';
25+
import { FacetValue } from '../../../../../shared/search/models/facet-value.model';
2526
import { createSuccessfulRemoteDataObject$ } from '../../../../remote-data.utils';
2627
import { RouterStub } from '../../../../testing/router.stub';
2728
import { SearchConfigurationServiceStub } from '../../../../testing/search-configuration-service.stub';
@@ -41,6 +42,8 @@ describe('SearchFacetFilterComponent', () => {
4142
const value2 = 'test2';
4243
const value3 = 'another value3';
4344
const value4 = '52d629dc-7d2f-47b9-aa2d-258b92e45ae1';
45+
const value5 = 'test authority';
46+
const authority = '52d629dc-7d2f-47b9-aa2d-258b92e45ae1';
4447
const mockFilterConfig: SearchFilterConfig = Object.assign(new SearchFilterConfig(), {
4548
name: filterName1,
4649
filterType: FilterType.text,
@@ -72,12 +75,40 @@ describe('SearchFacetFilterComponent', () => {
7275
label: value4,
7376
value: value4,
7477
});
78+
const appliedFilter5: AppliedFilter = Object.assign(new AppliedFilter(), {
79+
filter: filterName1,
80+
operator: 'authority',
81+
label: authority,
82+
value: authority,
83+
});
84+
const facetValue1: FacetValue = Object.assign(new FacetValue(), {
85+
label: value1,
86+
value: value1,
87+
});
88+
const facetValue2: FacetValue = Object.assign(new FacetValue(), {
89+
label: value2,
90+
value: value2,
91+
});
92+
const facetValue3: FacetValue = Object.assign(new FacetValue(), {
93+
label: value3,
94+
value: value3,
95+
});
96+
const facetValue4: FacetValue = Object.assign(new FacetValue(), {
97+
label: value5,
98+
value: authority,
99+
});
75100
const values: Partial<FacetValues> = {
76101
appliedFilters: [
77102
appliedFilter1,
78103
appliedFilter2,
79104
appliedFilter3,
80105
],
106+
page: [
107+
facetValue1,
108+
facetValue2,
109+
facetValue3,
110+
facetValue4,
111+
],
81112
pageInfo: Object.assign(new PageInfo(), {
82113
currentPage: 0,
83114
}),
@@ -246,4 +277,23 @@ describe('SearchFacetFilterComponent', () => {
246277
}));
247278
});
248279
});
280+
281+
describe('when selected value has an authority', () => {
282+
let selectedValues$: BehaviorSubject<AppliedFilter[]>;
283+
284+
beforeEach(() => {
285+
selectedValues$ = new BehaviorSubject([appliedFilter5]);
286+
spyOn(searchService, 'getSelectedValuesForFilter').and.returnValue(selectedValues$);
287+
comp.ngOnInit();
288+
});
289+
290+
it('should updated the label with the one of the related FacetValue', () => {
291+
const expectedValue = Object.assign(appliedFilter5, {
292+
label: facetValue4.label,
293+
});
294+
expect(comp.selectedAppliedFilters$).toBeObservable(cold('a', {
295+
a: [expectedValue],
296+
}));
297+
});
298+
});
249299
});

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

+39-5
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,27 @@ import {
1818
} from '@angular/router';
1919
import {
2020
BehaviorSubject,
21-
combineLatest as observableCombineLatest,
21+
combineLatest,
22+
from,
2223
Observable,
2324
of as observableOf,
2425
Subscription,
2526
} from 'rxjs';
2627
import {
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

3443
import { RemoteDataBuildService } from '../../../../../core/cache/builders/remote-data-build.service';
3544
import { getFirstSucceededRemoteDataPayload } from '../../../../../core/shared/operators';
@@ -40,6 +49,7 @@ import { SEARCH_CONFIG_SERVICE } from '../../../../../my-dspace-page/my-dspace-c
4049
import {
4150
hasNoValue,
4251
hasValue,
52+
isNotEmpty,
4353
} from '../../../../empty.util';
4454
import { InputSuggestion } from '../../../../input-suggestions/input-suggestions.model';
4555
import { 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));

src/app/system-wide-alert/alert-banner/system-wide-alert-banner.component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class SystemWideAlertBannerComponent implements OnInit, OnDestroy {
7979
}
8080

8181
ngOnInit() {
82-
this.subscriptions.push(this.systemWideAlertDataService.searchBy('active').pipe(
82+
this.subscriptions.push(this.systemWideAlertDataService.searchBy('active', {}, false).pipe(
8383
getAllSucceededRemoteDataPayload(),
8484
map((payload: PaginatedList<SystemWideAlert>) => payload.page),
8585
filter((page) => isNotEmpty(page)),

0 commit comments

Comments
 (0)