Skip to content

Commit f944d07

Browse files
[DSC-1021] Fixed Hierarchical vocabulary rendering in item-detail.
1 parent f71e846 commit f944d07

3 files changed

Lines changed: 53 additions & 30 deletions

File tree

src/app/core/submission/vocabularies/vocabulary.service.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { Injectable } from '@angular/core';
22
import { Observable } from 'rxjs';
33
import { first, map, mergeMap, switchMap } from 'rxjs/operators';
4-
import { FollowLinkConfig, followLink } from '../../../shared/utils/follow-link-config.model';
4+
import { followLink, FollowLinkConfig } from '../../../shared/utils/follow-link-config.model';
55
import { RequestService } from '../../data/request.service';
66
import { RemoteData } from '../../data/remote-data';
77
import { PaginatedList } from '../../data/paginated-list.model';
88
import { Vocabulary } from './models/vocabulary.model';
99
import { VocabularyEntry } from './models/vocabulary-entry.model';
1010
import { hasValue, isNotEmpty } from '../../../shared/empty.util';
11-
import {
12-
getFirstSucceededRemoteDataPayload,
13-
getFirstSucceededRemoteListPayload
14-
} from '../../shared/operators';
11+
import { getFirstSucceededRemoteDataPayload, getFirstSucceededRemoteListPayload } from '../../shared/operators';
1512
import { VocabularyFindOptions } from './models/vocabulary-find-options.model';
1613
import { VocabularyEntryDetail } from './models/vocabulary-entry-detail.model';
1714
import { RequestParam } from '../../cache/models/request-param.model';
@@ -153,8 +150,27 @@ export class VocabularyService {
153150
*/
154151
getPublicVocabularyEntryByValue(vocabularyName: string, value: string): Observable<RemoteData<PaginatedList<VocabularyEntryDetail>>> {
155152
const params: RequestParam[] = [
156-
new RequestParam('filter',value),
157-
new RequestParam('exact','true')
153+
new RequestParam('filter', value),
154+
new RequestParam('exact', 'true')
155+
];
156+
const options = Object.assign(new FindListOptions(), {
157+
searchParams: params,
158+
elementsPerPage: 1,
159+
});
160+
const href$ = this.vocabularyDataService.getFindAllHref(options, vocabularyName + '/entries');
161+
return this.vocabularyEntryDetailDataService.findListByHref(href$);
162+
}
163+
164+
/**
165+
* Get the display value for a hierarchical vocabulary item,
166+
* given the vocabulary name and the entryID of that vocabulary-entry
167+
*
168+
* @param vocabularyName
169+
* @param entryID
170+
*/
171+
getPublicVocabularyEntryByID(vocabularyName: string, entryID: string): Observable<RemoteData<PaginatedList<VocabularyEntryDetail>>> {
172+
const params: RequestParam[] = [
173+
new RequestParam('entryID', entryID)
158174
];
159175
const options = Object.assign(new FindListOptions(), {
160176
searchParams: params,

src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/valuepair/valuepair.component.spec.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { TranslateLoaderMock } from '../../../../../../../shared/mocks/translate
66
import { VocabularyService } from '../../../../../../../core/submission/vocabularies/vocabulary.service';
77
import { Item } from '../../../../../../../core/shared/item.model';
88
import { EMPTY, of } from 'rxjs';
9-
import {AuthService} from '../../../../../../../core/auth/auth.service';
10-
import {AuthServiceStub} from '../../../../../../../shared/testing/auth-service.stub';
9+
import { AuthService } from '../../../../../../../core/auth/auth.service';
10+
import { AuthServiceStub } from '../../../../../../../shared/testing/auth-service.stub';
1111
import { LayoutField } from '../../../../../../../core/layout/models/box.model';
1212
import { DsDatePipe } from '../../../../../../pipes/ds-date.pipe';
1313

@@ -76,14 +76,18 @@ describe('ValuepairComponent', () => {
7676
return of('Italian');
7777
case 'common_iso_languages/en':
7878
return of('English');
79-
case 'types':
79+
case 'types/types:asd':
8080
return of('Value');
8181
default:
8282
return EMPTY;
8383
}
8484
};
8585

86-
const vocabularyServiceSpy = jasmine.createSpyObj('vocabularyService', { getPublicVocabularyEntryByValue: EMPTY });
86+
const vocabularyServiceSpy =
87+
jasmine.createSpyObj(
88+
'vocabularyService',
89+
{ getPublicVocabularyEntryByValue: EMPTY, getPublicVocabularyEntryByID: EMPTY }
90+
);
8791

8892
describe('when the vocabulary has no authority', () => {
8993

@@ -162,7 +166,7 @@ describe('ValuepairComponent', () => {
162166
beforeEach(() => {
163167
fixture = TestBed.createComponent(ValuepairComponent);
164168
component = fixture.componentInstance;
165-
vocabularyServiceSpy.getPublicVocabularyEntryByValue.and.callFake(vocabularyEntriesMock);
169+
vocabularyServiceSpy.getPublicVocabularyEntryByID.and.callFake(vocabularyEntriesMock);
166170
fixture.detectChanges();
167171
});
168172

@@ -172,7 +176,7 @@ describe('ValuepairComponent', () => {
172176

173177

174178
it('should ...', () => {
175-
expect(vocabularyService.getPublicVocabularyEntryByValue).toHaveBeenCalledWith(VOCABULARY_NAME_2, 'asd');
179+
expect(vocabularyService.getPublicVocabularyEntryByID).toHaveBeenCalledWith(VOCABULARY_NAME_2, `${VOCABULARY_NAME_2}:asd`);
176180
});
177181

178182

src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/valuepair/valuepair.component.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { Component, Inject, OnInit } from '@angular/core';
22

33
import { TranslateService } from '@ngx-translate/core';
4-
import { BehaviorSubject, interval, race } from 'rxjs';
5-
import { map, mapTo, take } from 'rxjs/operators';
4+
import { BehaviorSubject } from 'rxjs';
5+
import { map, take } from 'rxjs/operators';
66

77
import { FieldRenderingType, MetadataBoxFieldRendering } from '../metadata-box.decorator';
88
import { VocabularyService } from '../../../../../../../core/submission/vocabularies/vocabulary.service';
99
import {
10-
getFirstSucceededRemoteDataPayload,
11-
getPaginatedListPayload
10+
getFirstCompletedRemoteData,
11+
getPaginatedListPayload,
12+
getRemoteDataPayload
1213
} from '../../../../../../../core/shared/operators';
1314
import { AuthService } from '../../../../../../../core/auth/auth.service';
1415
import { RenderingTypeValueModelComponent } from '../rendering-type-value.model';
@@ -49,21 +50,23 @@ export class ValuepairComponent extends RenderingTypeValueModelComponent impleme
4950

5051
const vocabularyName = this.renderingSubType;
5152
const authority = this.metadataValue.authority ? this.metadataValue.authority.split(':') : undefined;
52-
const isControlledVocabulary = authority?.length > 1 && authority[0] === vocabularyName;
53-
const metadataValue = isControlledVocabulary ? authority[1] : this.metadataValue.value;
53+
const isControlledVocabulary = authority?.length > 1 && authority[0] === vocabularyName;
5454

55-
const entry$ = this.vocabularyService.getPublicVocabularyEntryByValue(vocabularyName, metadataValue).pipe(
56-
getFirstSucceededRemoteDataPayload(),
57-
getPaginatedListPayload(),
58-
map((res) => res[0]?.display ?? this.metadataValue.value),
59-
);
60-
61-
// fallback values to be shown if the display value cannot be retrieved
62-
const initValue$ = interval(5000).pipe(mapTo(this.metadataValue.value));
55+
let vocabularyEntry$ =
56+
this.vocabularyService.getPublicVocabularyEntryByValue(vocabularyName, this.metadataValue.value);
57+
if (!!isControlledVocabulary) {
58+
vocabularyEntry$ =
59+
this.vocabularyService.getPublicVocabularyEntryByID(vocabularyName, this.metadataValue.authority);
60+
}
6361

64-
race([entry$, initValue$]).pipe(take(1)).subscribe((value: string) => {
65-
this.value$.next(value);
66-
});
62+
vocabularyEntry$.pipe(
63+
getFirstCompletedRemoteData(),
64+
getRemoteDataPayload(),
65+
getPaginatedListPayload(),
66+
map((res) => res?.length > 0 ? res[0] : null),
67+
map((res) => res?.display ?? this.metadataValue.value),
68+
take(1)
69+
).subscribe(value => this.value$.next(value));
6770

6871
}
6972

0 commit comments

Comments
 (0)