Skip to content

Commit 6da5c11

Browse files
authored
Merge pull request DSpace#2900 from toniprieto/edit-item-authorities-bug-model
Error loading the initial value when editing an authority field in edit item page
2 parents 6c4ff40 + 07d30ff commit 6da5c11

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';
@@ -196,9 +198,9 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
196198
group = new UntypedFormGroup({ authorityField : new UntypedFormControl() });
197199

198200
/**
199-
* Observable property of the model to use for editinf authorities values
201+
* Model to use for editing authorities values
200202
*/
201-
private model$: Observable<DynamicOneboxModel | DynamicScrollableDropdownModel>;
203+
private model$: BehaviorSubject<DynamicOneboxModel | DynamicScrollableDropdownModel> = new BehaviorSubject(null);
202204

203205
/**
204206
* Observable with information about the authority vocabulary used
@@ -278,6 +280,8 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
278280
}
279281

280282
this.isAuthorityControlled$ = this.vocabulary$.pipe(
283+
// Create the model used by the authority fields to ensure its existence when the field is initialized
284+
tap((v: Vocabulary) => this.model$.next(this.createModel(v))),
281285
map((result: Vocabulary) => isNotEmpty(result)),
282286
);
283287

@@ -293,54 +297,62 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
293297
map((result: Vocabulary) => isNotEmpty(result) && !result.hierarchical && !result.scrollable),
294298
);
295299

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

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

346358
/**
@@ -438,11 +450,11 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
438450
}
439451

440452
/**
441-
* Returns an observable with the {@link DynamicOneboxModel} or {@link DynamicScrollableDropdownModel} model used
453+
* Returns the {@link DynamicOneboxModel} or {@link DynamicScrollableDropdownModel} model used
442454
* for the authority field
443455
*/
444-
getModel(): Observable<DynamicOneboxModel | DynamicScrollableDropdownModel> {
445-
return this.model$;
456+
getModel(): DynamicOneboxModel | DynamicScrollableDropdownModel {
457+
return this.model$.value;
446458
}
447459

448460
/**

0 commit comments

Comments
 (0)