Skip to content

Commit 07d30ff

Browse files
committed
Create the model before displaying the vocabulary fields in edit item page
1 parent 1e33960 commit 07d30ff

3 files changed

Lines changed: 75 additions & 77 deletions

File tree

src/app/dso-shared/dso-edit-metadata/dso-edit-metadata-value/dso-edit-metadata-value.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
<ds-dynamic-scrollable-dropdown *ngIf="mdValue.editing && (isScrollableVocabulary() | async)"
1010
[bindId]="mdField"
1111
[group]="group"
12-
[model]="getModel() | async"
12+
[model]="getModel()"
1313
(change)="onChangeAuthorityField($event)">
1414
</ds-dynamic-scrollable-dropdown>
1515
<ds-dynamic-onebox *ngIf="mdValue.editing && ((isHierarchicalVocabulary() | async) || (isSuggesterVocabulary() | async))"
1616
[group]="group"
17-
[model]="getModel() | async"
17+
[model]="getModel()"
1818
(change)="onChangeAuthorityField($event)">
1919
</ds-dynamic-onebox>
2020
<div *ngIf="!isVirtual && !mdValue.editing && mdValue.newValue.authority && mdValue.newValue.confidence !== ConfidenceTypeEnum.CF_UNSET && mdValue.newValue.confidence !== ConfidenceTypeEnum.CF_NOVALUE">

src/app/dso-shared/dso-edit-metadata/dso-edit-metadata-value/dso-edit-metadata-value.component.spec.ts

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ import {
1010
import { By } from '@angular/platform-browser';
1111
import { RouterTestingModule } from '@angular/router/testing';
1212
import { TranslateModule } from '@ngx-translate/core';
13-
import {
14-
Observable,
15-
of,
16-
} from 'rxjs';
13+
import { of } from 'rxjs';
1714
import { MetadataField } from 'src/app/core/metadata/metadata-field.model';
1815
import { MetadataSchema } from 'src/app/core/metadata/metadata-schema.model';
1916
import { RegistryService } from 'src/app/core/registry/registry.service';
@@ -346,14 +343,11 @@ describe('DsoEditMetadataValueComponent', () => {
346343
});
347344

348345
it('getModel should return a DynamicScrollableDropdownModel', () => {
349-
const result = component.getModel();
346+
const model = component.getModel();
350347

351-
expect(result instanceof Observable).toBe(true);
348+
expect(model instanceof DynamicScrollableDropdownModel).toBe(true);
349+
expect(model.vocabularyOptions.name).toBe(mockVocabularyScrollable.name);
352350

353-
result.subscribe((model) => {
354-
expect(model instanceof DynamicScrollableDropdownModel).toBe(true);
355-
expect(model.vocabularyOptions.name).toBe(mockVocabularyScrollable.name);
356-
});
357351
});
358352
});
359353

@@ -380,14 +374,10 @@ describe('DsoEditMetadataValueComponent', () => {
380374
});
381375

382376
it('getModel should return a DynamicOneboxModel', () => {
383-
const result = component.getModel();
384-
385-
expect(result instanceof Observable).toBe(true);
377+
const model = component.getModel();
386378

387-
result.subscribe((model) => {
388-
expect(model instanceof DynamicOneboxModel).toBe(true);
389-
expect(model.vocabularyOptions.name).toBe(mockVocabularyHierarchical.name);
390-
});
379+
expect(model instanceof DynamicOneboxModel).toBe(true);
380+
expect(model.vocabularyOptions.name).toBe(mockVocabularyHierarchical.name);
391381
});
392382
});
393383

@@ -416,14 +406,10 @@ describe('DsoEditMetadataValueComponent', () => {
416406
});
417407

418408
it('getModel should return a DynamicOneboxModel', () => {
419-
const result = component.getModel();
420-
421-
expect(result instanceof Observable).toBe(true);
409+
const model = component.getModel();
422410

423-
result.subscribe((model) => {
424-
expect(model instanceof DynamicOneboxModel).toBe(true);
425-
expect(model.vocabularyOptions.name).toBe(mockVocabularySuggester.name);
426-
});
411+
expect(model instanceof DynamicOneboxModel).toBe(true);
412+
expect(model.vocabularyOptions.name).toBe(mockVocabularySuggester.name);
427413
});
428414

429415
describe('authority key edition', () => {

src/app/dso-shared/dso-edit-metadata/dso-edit-metadata-value/dso-edit-metadata-value.component.ts

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
TranslateService,
3030
} from '@ngx-translate/core';
3131
import {
32+
BehaviorSubject,
3233
EMPTY,
3334
Observable,
3435
of as observableOf,
@@ -37,6 +38,7 @@ import {
3738
map,
3839
switchMap,
3940
take,
41+
tap,
4042
} from 'rxjs/operators';
4143
import { RegistryService } from 'src/app/core/registry/registry.service';
4244
import { VocabularyService } from 'src/app/core/submission/vocabularies/vocabulary.service';
@@ -195,9 +197,9 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
195197
group = new UntypedFormGroup({ authorityField : new UntypedFormControl() });
196198

197199
/**
198-
* Observable property of the model to use for editinf authorities values
200+
* Model to use for editing authorities values
199201
*/
200-
private model$: Observable<DynamicOneboxModel | DynamicScrollableDropdownModel>;
202+
private model$: BehaviorSubject<DynamicOneboxModel | DynamicScrollableDropdownModel> = new BehaviorSubject(null);
201203

202204
/**
203205
* Observable with information about the authority vocabulary used
@@ -274,6 +276,8 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
274276
}
275277

276278
this.isAuthorityControlled$ = this.vocabulary$.pipe(
279+
// Create the model used by the authority fields to ensure its existence when the field is initialized
280+
tap((v: Vocabulary) => this.model$.next(this.createModel(v))),
277281
map((result: Vocabulary) => isNotEmpty(result)),
278282
);
279283

@@ -289,54 +293,62 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
289293
map((result: Vocabulary) => isNotEmpty(result) && !result.hierarchical && !result.scrollable),
290294
);
291295

292-
this.model$ = this.vocabulary$.pipe(
293-
map((vocabulary: Vocabulary) => {
294-
let formFieldValue;
295-
if (isNotEmpty(this.mdValue.newValue.value)) {
296-
formFieldValue = new FormFieldMetadataValueObject();
297-
formFieldValue.value = this.mdValue.newValue.value;
298-
formFieldValue.display = this.mdValue.newValue.value;
299-
if (this.mdValue.newValue.authority) {
300-
formFieldValue.authority = this.mdValue.newValue.authority;
301-
formFieldValue.confidence = this.mdValue.newValue.confidence;
302-
}
303-
} else {
304-
formFieldValue = this.mdValue.newValue.value;
305-
}
296+
}
306297

307-
const vocabularyOptions = vocabulary ? {
308-
closed: false,
309-
name: vocabulary.name,
310-
} as VocabularyOptions : null;
311-
312-
if (!vocabulary.scrollable) {
313-
const model: DsDynamicOneboxModelConfig = {
314-
id: 'authorityField',
315-
label: `${this.dsoType}.edit.metadata.edit.value`,
316-
vocabularyOptions: vocabularyOptions,
317-
metadataFields: [this.mdField],
318-
value: formFieldValue,
319-
repeatable: false,
320-
submissionId: 'edit-metadata',
321-
hasSelectableMetadata: false,
322-
};
323-
return new DynamicOneboxModel(model);
324-
} else {
325-
const model: DynamicScrollableDropdownModelConfig = {
326-
id: 'authorityField',
327-
label: `${this.dsoType}.edit.metadata.edit.value`,
328-
placeholder: `${this.dsoType}.edit.metadata.edit.value`,
329-
vocabularyOptions: vocabularyOptions,
330-
metadataFields: [this.mdField],
331-
value: formFieldValue,
332-
repeatable: false,
333-
submissionId: 'edit-metadata',
334-
hasSelectableMetadata: false,
335-
maxOptions: 10,
336-
};
337-
return new DynamicScrollableDropdownModel(model);
298+
/**
299+
* Returns a {@link DynamicOneboxModel} or {@link DynamicScrollableDropdownModel} model based on the
300+
* vocabulary used.
301+
*/
302+
private createModel(vocabulary: Vocabulary): DynamicOneboxModel | DynamicScrollableDropdownModel {
303+
if (isNotEmpty(vocabulary)) {
304+
let formFieldValue;
305+
if (isNotEmpty(this.mdValue.newValue.value)) {
306+
formFieldValue = new FormFieldMetadataValueObject();
307+
formFieldValue.value = this.mdValue.newValue.value;
308+
formFieldValue.display = this.mdValue.newValue.value;
309+
if (this.mdValue.newValue.authority) {
310+
formFieldValue.authority = this.mdValue.newValue.authority;
311+
formFieldValue.confidence = this.mdValue.newValue.confidence;
338312
}
339-
}));
313+
} else {
314+
formFieldValue = this.mdValue.newValue.value;
315+
}
316+
317+
const vocabularyOptions = vocabulary ? {
318+
closed: false,
319+
name: vocabulary.name,
320+
} as VocabularyOptions : null;
321+
322+
if (!vocabulary.scrollable) {
323+
const model: DsDynamicOneboxModelConfig = {
324+
id: 'authorityField',
325+
label: `${this.dsoType}.edit.metadata.edit.value`,
326+
vocabularyOptions: vocabularyOptions,
327+
metadataFields: [this.mdField],
328+
value: formFieldValue,
329+
repeatable: false,
330+
submissionId: 'edit-metadata',
331+
hasSelectableMetadata: false,
332+
};
333+
return new DynamicOneboxModel(model);
334+
} else {
335+
const model: DynamicScrollableDropdownModelConfig = {
336+
id: 'authorityField',
337+
label: `${this.dsoType}.edit.metadata.edit.value`,
338+
placeholder: `${this.dsoType}.edit.metadata.edit.value`,
339+
vocabularyOptions: vocabularyOptions,
340+
metadataFields: [this.mdField],
341+
value: formFieldValue,
342+
repeatable: false,
343+
submissionId: 'edit-metadata',
344+
hasSelectableMetadata: false,
345+
maxOptions: 10,
346+
};
347+
return new DynamicScrollableDropdownModel(model);
348+
}
349+
} else {
350+
return null;
351+
}
340352
}
341353

342354
/**
@@ -434,11 +446,11 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
434446
}
435447

436448
/**
437-
* Returns an observable with the {@link DynamicOneboxModel} or {@link DynamicScrollableDropdownModel} model used
449+
* Returns the {@link DynamicOneboxModel} or {@link DynamicScrollableDropdownModel} model used
438450
* for the authority field
439451
*/
440-
getModel(): Observable<DynamicOneboxModel | DynamicScrollableDropdownModel> {
441-
return this.model$;
452+
getModel(): DynamicOneboxModel | DynamicScrollableDropdownModel {
453+
return this.model$.value;
442454
}
443455

444456
/**

0 commit comments

Comments
 (0)