Skip to content

Commit 9406f7b

Browse files
committed
122839: Retrieve preloadLevel from vocabulary before creating tree
1 parent cee9d04 commit 9406f7b

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FlatTreeControl } from '@angular/cdk/tree';
22
import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core';
33

4-
import { map } from 'rxjs/operators';
4+
import { map, tap, switchMap } from 'rxjs/operators';
55
import { Observable, Subscription } from 'rxjs';
66
import { Store } from '@ngrx/store';
77
import { TranslateService } from '@ngx-translate/core';
@@ -18,7 +18,9 @@ import { VocabularyTreeFlatDataSource } from './vocabulary-tree-flat-data-source
1818
import { CoreState } from '../../../core/core-state.model';
1919
import { lowerCase } from 'lodash/string';
2020
import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service';
21-
import { getFirstSucceededRemoteDataPayload } from '../../../core/shared/operators';
21+
import { getFirstSucceededRemoteDataPayload, getFirstCompletedRemoteData } from '../../../core/shared/operators';
22+
import { Vocabulary } from '../../../core/submission/vocabularies/models/vocabulary.model';
23+
import { RemoteData } from '../../../core/data/remote-data';
2224

2325
/**
2426
* Component that shows a hierarchical vocabulary in a tree view
@@ -219,20 +221,35 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit {
219221
*/
220222
ngOnInit(): void {
221223
this.subs.push(
222-
this.vocabularyTreeviewService.getData().subscribe((data) => {
224+
this.vocabularyService.findVocabularyById(this.vocabularyOptions.name).pipe(
225+
// Retrieve the configured preloadLevel from REST
226+
getFirstCompletedRemoteData(),
227+
map((vocabularyRD: RemoteData<Vocabulary>) => {
228+
if (vocabularyRD.hasSucceeded &&
229+
hasValue(vocabularyRD.payload.preloadLevel) &&
230+
vocabularyRD.payload.preloadLevel > 1) {
231+
return vocabularyRD.payload.preloadLevel;
232+
} else {
233+
// Set preload level to 1 in case request fails
234+
return 1;
235+
}
236+
}),
237+
tap(preloadLevel => this.preloadLevel = preloadLevel),
238+
tap(() => this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.selectedItems, null)),
239+
switchMap(() => this.vocabularyTreeviewService.getData()),
240+
).subscribe((data) => {
223241
this.dataSource.data = data;
224242
})
225243
);
226244

245+
246+
this.loading = this.vocabularyTreeviewService.isLoading();
247+
227248
this.translate.get(`search.filters.filter.${this.vocabularyOptions.name}.head`).pipe(
228249
map((type) => lowerCase(type)),
229250
).subscribe(
230251
(type) => this.description = this.translate.get('vocabulary-treeview.info', { type })
231252
);
232-
233-
this.loading = this.vocabularyTreeviewService.isLoading();
234-
235-
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.selectedItems, null);
236253
}
237254

238255
/**

0 commit comments

Comments
 (0)