Skip to content

Commit ae3f0a7

Browse files
Simone-Ramundivins01-4science
authored andcommitted
Merged in DSC-1367 (pull request DSpace#1574)
DSC-1367 Approved-by: Vincenzo Mecca
2 parents 2ca575c + 92f8ed4 commit ae3f0a7

3 files changed

Lines changed: 120 additions & 13 deletions

File tree

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<a class="badge badge-primary mb-1"
2-
[attr.aria-label]="'search.filters.remove' | translate:{ type: ('search.filters.applied.' + key) | translate, value: normalizeFilterValue(getStrippedValue(value)) }"
2+
*ngVar="filterValue | async as filter"
3+
[attr.aria-label]="'search.filters.remove' | translate:{ type: ('search.filters.applied.' + key) | translate, value: filter }"
34
[routerLink]="searchLink"
45
[queryParams]="(removeParameters | async)" queryParamsHandling="merge">
5-
<span class="d-flex">
6-
<span class="flex-grow-1 text-left">{{('search.filters.applied.' + key) | translate}}: {{'search.filters.' + filterName + '.' + value | translate: {default: normalizeFilterValue(getStrippedValue(value))} }}</span>
6+
<span class="d-flex" *ngIf="filter">
7+
<span class="flex-grow-1 text-left">{{('search.filters.applied.' + key) | translate}}: {{'search.filters.' + filterName + '.' + filter | translate: {default: filter } }}</span>
78
<span class="pl-1" aria-hidden="true">×</span>
89
</span>
910
</a>

src/app/shared/search/search-labels/search-label/search-label.component.spec.ts

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,42 @@ import { PaginationComponentOptions } from '../../../pagination/pagination-compo
1515
import { PaginationService } from '../../../../core/pagination/pagination.service';
1616
import { SearchConfigurationService } from '../../../../core/shared/search/search-configuration.service';
1717
import { PaginationServiceStub } from '../../../testing/pagination-service.stub';
18+
import { ItemDataService } from '../../../../core/data/item-data.service';
19+
import { DSONameService } from '../../../../core/breadcrumbs/dso-name.service';
20+
import { Item } from '../../../../core/shared/item.model';
21+
import {
22+
createFailedRemoteDataObject$,
23+
createSuccessfulRemoteDataObject$
24+
} from '../../../remote-data.utils';
1825

1926
describe('SearchLabelComponent', () => {
27+
28+
const item = Object.assign(new Item(), {
29+
uuid: 'itemUUID',
30+
});
31+
const name = 'itemName';
32+
const mockItemDataService = jasmine.createSpyObj ('ItemDataService', {
33+
findById: jasmine.createSpy('findById'),
34+
});
35+
const mockDSODataService = jasmine.createSpyObj ('DSODataService', {
36+
getName: jasmine.createSpy('getName'),
37+
});
2038
let comp: SearchLabelComponent;
2139
let fixture: ComponentFixture<SearchLabelComponent>;
2240

2341
const searchLink = '/search';
2442
let searchService;
25-
2643
const key1 = 'author';
2744
const key2 = 'subject';
2845
const value1 = 'Test, Author';
2946
const normValue1 = 'Test, Author';
3047
const value2 = 'TestSubject';
31-
const value3 = 'Test, Authority,authority';
32-
const normValue3 = 'Test, Authority';
48+
const valueAuthority = 'Test, Authority,authority';
49+
const normValueAuthority = 'Test, Authority';
50+
const valueEquals = 'Test, Author,equals';
51+
const normValueEquals = 'Test, Author';
52+
const randomAuthor = 'Test Author';
53+
const normValue4 = 'Test, Authority';
3354
const filter1 = [key1, value1];
3455
const filter2 = [key2, value2];
3556
const mockFilters = [
@@ -49,7 +70,9 @@ describe('SearchLabelComponent', () => {
4970
{ provide: SEARCH_CONFIG_SERVICE, useValue: new SearchConfigurationServiceStub() },
5071
{ provide: SearchConfigurationService, useValue: new SearchConfigurationServiceStub() },
5172
{ provide: PaginationService, useValue: paginationService },
52-
{ provide: Router, useValue: {} }
73+
{ provide: Router, useValue: {} },
74+
{ provide: ItemDataService, useValue: mockItemDataService },
75+
{ provide: DSONameService, useValue: mockDSODataService }
5376
// { provide: SearchConfigurationService, useValue: {getCurrentFrontendFilters : () => observableOf({})} }
5477
],
5578
schemas: [NO_ERRORS_SCHEMA]
@@ -62,17 +85,18 @@ describe('SearchLabelComponent', () => {
6285
fixture = TestBed.createComponent(SearchLabelComponent);
6386
comp = fixture.componentInstance;
6487
searchService = (comp as any).searchService;
65-
comp.key = key1;
66-
comp.value = value1;
6788
(comp as any).appliedFilters = observableOf(mockFilters);
68-
fixture.detectChanges();
89+
mockDSODataService.getName.and.returnValue(name);
6990
});
7091

7192
describe('when getRemoveParams is called', () => {
7293
let obs: Observable<Params>;
7394

7495
beforeEach(() => {
7596
obs = comp.getRemoveParams();
97+
comp.key = key1;
98+
comp.value = value1;
99+
fixture.detectChanges();
76100
});
77101

78102
it('should return all params but the provided filter', () => {
@@ -84,14 +108,59 @@ describe('SearchLabelComponent', () => {
84108
});
85109

86110
describe('when normalizeFilterValue is called', () => {
111+
beforeEach(() => {
112+
comp.key = key1;
113+
comp.value = value1;
114+
fixture.detectChanges();
115+
});
87116
it('should return properly filter value', () => {
88117
let result: string;
89118

90119
result = comp.normalizeFilterValue(value1);
91120
expect(result).toBe(normValue1);
92121

93-
result = comp.normalizeFilterValue(value3);
94-
expect(result).toBe(normValue3);
122+
result = comp.normalizeFilterValue(valueAuthority);
123+
expect(result).toBe(normValueAuthority);
124+
});
125+
});
126+
127+
describe('when value is set as authority', () => {
128+
beforeEach(() => {
129+
comp.key = key1;
130+
comp.value = valueAuthority;
131+
});
132+
it('should return the value retrieved from Item', () => {
133+
mockItemDataService.findById.and.returnValue(createSuccessfulRemoteDataObject$(item));
134+
fixture.detectChanges();
135+
expect(comp.filterValue.getValue()).toBe(name);
136+
});
137+
it('should return the given value', () => {
138+
mockItemDataService.findById.and.returnValue(createFailedRemoteDataObject$());
139+
fixture.detectChanges();
140+
expect(comp.filterValue.getValue()).toBe(normValueAuthority);
141+
});
142+
});
143+
144+
describe('when value is not set as authority', () => {
145+
beforeEach(() => {
146+
comp.key = key1;
147+
comp.value = valueEquals;
148+
fixture.detectChanges();
149+
});
150+
it('should return the given value', () => {
151+
expect(comp.filterValue.getValue()).toBe(normValueEquals);
152+
});
153+
});
154+
155+
describe('when value is random', () => {
156+
beforeEach(() => {
157+
comp.key = key1;
158+
comp.value = randomAuthor;
159+
fixture.detectChanges();
160+
});
161+
it('should return the value', () => {
162+
expect(comp.filterValue.getValue()).toBe(randomAuthor);
95163
});
96164
});
165+
97166
});

src/app/shared/search/search-labels/search-label/search-label.component.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, Input, OnInit } from '@angular/core';
2-
import { Observable } from 'rxjs';
2+
import { BehaviorSubject, Observable } from 'rxjs';
33
import { Params, Router } from '@angular/router';
44
import { map } from 'rxjs/operators';
55
import { hasValue, isNotEmpty } from '../../../empty.util';
@@ -8,6 +8,9 @@ import { currentPath } from '../../../utils/route.utils';
88
import { PaginationService } from '../../../../core/pagination/pagination.service';
99
import { SearchConfigurationService } from '../../../../core/shared/search/search-configuration.service';
1010
import { stripOperatorFromFilterValue } from '../../search.utils';
11+
import { ItemDataService } from '../../../../core/data/item-data.service';
12+
import { DSONameService } from '../../../../core/breadcrumbs/dso-name.service';
13+
import { getFirstCompletedRemoteData } from '../../../../core/shared/operators';
1114

1215
@Component({
1316
selector: 'ds-search-label',
@@ -31,20 +34,54 @@ export class SearchLabelComponent implements OnInit {
3134
*/
3235
filterName: string;
3336

37+
/**
38+
* Represents the name associated with the corresponding ID value.
39+
*/
40+
filterValue: BehaviorSubject<string> = new BehaviorSubject<string>('');
41+
3442
/**
3543
* Initialize the instance variable
3644
*/
3745
constructor(
3846
private searchService: SearchService,
3947
private paginationService: PaginationService,
4048
private searchConfigurationService: SearchConfigurationService,
49+
private itemDataService: ItemDataService,
50+
private dsoNameService: DSONameService,
4151
private router: Router) {
4252
}
4353

4454
ngOnInit(): void {
4555
this.searchLink = this.getSearchLink();
4656
this.removeParameters = this.getRemoveParams();
4757
this.filterName = this.getFilterName();
58+
this.setFilterValue();
59+
}
60+
61+
/**
62+
* Retrieves and sets the appropriate filter value based on the given input, updating the filterValue accordingly.
63+
* @returns {void}
64+
*/
65+
setFilterValue(): void{
66+
const parts = this.value.split(',');
67+
const partTrimmed = parts.pop().trim();
68+
if (partTrimmed === 'authority') {
69+
const id = parts[0].trim();
70+
this.itemDataService.findById(id).pipe(
71+
getFirstCompletedRemoteData(),
72+
map((rq)=> rq.hasSucceeded ? rq.payload : null),
73+
).subscribe((result)=>{
74+
let tmpValue: string;
75+
if (isNotEmpty(result)){
76+
tmpValue = this.dsoNameService.getName(result);
77+
} else {
78+
tmpValue = this.value;
79+
}
80+
this.filterValue.next(this.normalizeFilterValue(this.getStrippedValue(tmpValue)));
81+
});
82+
} else {
83+
this.filterValue.next(this.normalizeFilterValue(this.getStrippedValue(this.value)));
84+
}
4885
}
4986

5087
/**

0 commit comments

Comments
 (0)