Skip to content

Commit

Permalink
Merge branch 'w2p-122839_vocabulary-preloadlevel-fix' into vocabulary…
Browse files Browse the repository at this point in the history
…-preloadlevel-fix-main

# Conflicts:
#	src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.spec.ts
#	src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.ts
  • Loading branch information
AAwouters committed Jan 10, 2025
2 parents 952611a + 4ffde92 commit 5df7471
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vo
import { VocabularyEntryDetail } from '../../../core/submission/vocabularies/models/vocabulary-entry-detail.model';
import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model';
import { VocabularyService } from '../../../core/submission/vocabularies/vocabulary.service';
import { createSuccessfulRemoteDataObject$ } from '../../remote-data.utils';
import { createTestComponent } from '../../testing/utils.test';
import { FormFieldMetadataValueObject } from '../builder/models/form-field-metadata-value.model';
import { VocabularyTreeviewComponent } from './vocabulary-treeview.component';
Expand Down Expand Up @@ -63,6 +64,7 @@ describe('VocabularyTreeviewComponent test suite', () => {
searchTopEntries: jasmine.createSpy('searchTopEntries'),
getEntryDetailChildren: jasmine.createSpy('getEntryDetailChildren'),
clearSearchTopRequests: jasmine.createSpy('clearSearchTopRequests'),
findVocabularyById: createSuccessfulRemoteDataObject$({ preloadLevel: 2 }),
});

beforeEach(waitForAsync(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ import {
Observable,
Subscription,
} from 'rxjs';

import {
map,
switchMap,
tap,
} from 'rxjs/operators';
import { VocabularyService } from 'src/app/core/submission/vocabularies/vocabulary.service';

import { RemoteData } from '../../../core/data/remote-data';
import { getFirstCompletedRemoteData } from '../../../core/shared/operators';
import { PageInfo } from '../../../core/shared/page-info.model';
import { Vocabulary } from '../../../core/submission/vocabularies/models/vocabulary.model';
import { VocabularyEntry } from '../../../core/submission/vocabularies/models/vocabulary-entry.model';
import { VocabularyEntryDetail } from '../../../core/submission/vocabularies/models/vocabulary-entry-detail.model';
import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model';
Expand Down Expand Up @@ -168,6 +177,7 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
*/
constructor(
private vocabularyTreeviewService: VocabularyTreeviewService,
protected vocabularyService: VocabularyService,
) {
this.treeFlattener = new VocabularyTreeFlattener(this.transformer, this.getLevel,
this.isExpandable, this.getChildren);
Expand Down Expand Up @@ -209,12 +219,20 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
);
this.nodeMap.set(entryId, newNode);

if ((((level + 1) < this.preloadLevel) && newNode.childrenLoaded)
if ((((level + 1) < this.preloadLevel))
|| (newNode.isSearchNode && newNode.childrenLoaded)
|| newNode.isInInitValueHierarchy) {
if (!newNode.isSearchNode) {

if (newNode.item.id === LOAD_MORE || newNode.item.id === LOAD_MORE_ROOT) {
// When a 'LOAD_MORE' node is encountered, the parent already has a lot of expanded children
// so this is a good point to stop expanding.
return newNode;

Check warning on line 229 in src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L229 was not covered by tests
}

if (!newNode.childrenLoaded) {
this.loadChildren(newNode);
}

this.treeControl.expand(newNode);
}
return newNode;
Expand Down Expand Up @@ -255,15 +273,31 @@ export class VocabularyTreeviewComponent implements OnDestroy, OnInit, OnChanges
*/
ngOnInit(): void {
this.subs.push(
this.vocabularyTreeviewService.getData().subscribe((data) => {
this.vocabularyService.findVocabularyById(this.vocabularyOptions.name).pipe(
// Retrieve the configured preloadLevel from REST
getFirstCompletedRemoteData(),
map((vocabularyRD: RemoteData<Vocabulary>) => {
if (vocabularyRD.hasSucceeded &&
hasValue(vocabularyRD.payload.preloadLevel) &&
vocabularyRD.payload.preloadLevel > 1) {
return vocabularyRD.payload.preloadLevel;
} else {
// Set preload level to 1 in case request fails
return 1;

Check warning on line 286 in src/app/shared/form/vocabulary-treeview/vocabulary-treeview.component.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L286 was not covered by tests
}
}),
tap(preloadLevel => this.preloadLevel = preloadLevel),
tap(() => {
const entryId: string = (this.selectedItems?.length > 0) ? this.getEntryId(this.selectedItems[0]) : null;
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.getSelectedEntryIds(), entryId);
}),
switchMap(() => this.vocabularyTreeviewService.getData()),
).subscribe((data) => {
this.dataSource.data = data;
}),
);

this.loading = this.vocabularyTreeviewService.isLoading();

const entryId: string = (this.selectedItems?.length > 0) ? this.getEntryId(this.selectedItems[0]) : null;
this.vocabularyTreeviewService.initialize(this.vocabularyOptions, new PageInfo(), this.getSelectedEntryIds(), entryId);
}

/**
Expand Down

0 comments on commit 5df7471

Please sign in to comment.