diff --git a/src/app/shared/form/builder/form-builder.service.ts b/src/app/shared/form/builder/form-builder.service.ts index a3dc3626a57..a9deaf9781e 100644 --- a/src/app/shared/form/builder/form-builder.service.ts +++ b/src/app/shared/form/builder/form-builder.service.ts @@ -708,4 +708,9 @@ export class FormBuilderService extends DynamicFormService { return this.getDefaultTypeBindModelId(); } + /** Type-bind controlling fields (underscore notation, e.g. `dc_type`, `edm_type`) from `submit.type-bind.field`. */ + getTypeFieldValues(): string[] { + return Array.from(this.typeFields.values()); + } + } diff --git a/src/app/shared/mocks/form-builder-service.mock.ts b/src/app/shared/mocks/form-builder-service.mock.ts index ce60600f2ce..618ccaab74a 100644 --- a/src/app/shared/mocks/form-builder-service.mock.ts +++ b/src/app/shared/mocks/form-builder-service.mock.ts @@ -48,6 +48,7 @@ export function getMockFormBuilderService(): FormBuilderService { ), getTypeBindModelUpdates: EMPTY, resolveTypeBindModelId: undefined, + getTypeFieldValues: ['dc_type'], }); // as the real implementation behaves for a reference the type field map does not remap diff --git a/src/app/submission/sections/form/section-form.component.spec.ts b/src/app/submission/sections/form/section-form.component.spec.ts index 2b123cc985d..1bf43f27fd5 100644 --- a/src/app/submission/sections/form/section-form.component.spec.ts +++ b/src/app/submission/sections/form/section-form.component.spec.ts @@ -608,6 +608,36 @@ describe('SubmissionSectionFormComponent test suite', () => { }); + it('should call dispatchSaveSection on form change when a type-bind field (e.g. dc.type) changes', () => { + spyOn(comp, 'hasStoredValue').and.returnValue(false); + formOperationsService.getFieldPathSegmentedFromChangeEvent.and.returnValue('dc.type'); + formOperationsService.getFieldValueFromChangeEvent.and.returnValue({ value: 'Corpus' }); + + comp.onChange(dynamicFormControlEvent); + + expect(submissionServiceStub.dispatchSaveSection).toHaveBeenCalledWith(submissionId, sectionObject.id); + }); + + it('should call dispatchSaveSection on form change when a sponsor value changes', () => { + spyOn(comp, 'hasStoredValue').and.returnValue(false); + formOperationsService.getFieldPathSegmentedFromChangeEvent.and.returnValue('local.sponsor'); + formOperationsService.getFieldValueFromChangeEvent.and.returnValue({ value: 'EU' }); + + comp.onChange(dynamicFormControlEvent); + + expect(submissionServiceStub.dispatchSaveSection).toHaveBeenCalledWith(submissionId, sectionObject.id); + }); + + it('should not call dispatchSaveSection on form change for a regular field', () => { + spyOn(comp, 'hasStoredValue').and.returnValue(false); + formOperationsService.getFieldPathSegmentedFromChangeEvent.and.returnValue('dc.description'); + formOperationsService.getFieldValueFromChangeEvent.and.returnValue('some text'); + + comp.onChange(dynamicFormControlEvent); + + expect(submissionServiceStub.dispatchSaveSection).not.toHaveBeenCalled(); + }); + it('should set previousValue on form focus event', () => { formBuilderService.hasMappedGroupValue.and.returnValue(false); formOperationsService.getFieldValueFromChangeEvent.and.returnValue('test'); diff --git a/src/app/submission/sections/form/section-form.component.ts b/src/app/submission/sections/form/section-form.component.ts index 973225c8f94..8e6006f8edf 100644 --- a/src/app/submission/sections/form/section-form.component.ts +++ b/src/app/submission/sections/form/section-form.component.ts @@ -52,6 +52,8 @@ import { isNotEmpty, isUndefined, } from '../../../shared/empty.util'; +import { AUTHOR_METADATA_FIELD_NAME } from '../../../shared/form/builder/ds-dynamic-form-ui/models/clarin-name.model'; +import { SPONSOR_METADATA_NAME } from '../../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-complex.model'; import { FormBuilderService } from '../../../shared/form/builder/form-builder.service'; import { FormFieldPreviousValueObject } from '../../../shared/form/builder/models/form-field-previous-value-object'; import { FormComponent } from '../../../shared/form/form.component'; @@ -446,6 +448,13 @@ export class SubmissionSectionFormComponent extends SectionModelComponent { if ((environment.submission.autosave.metadata.indexOf(metadata) !== -1 && isNotEmpty(value)) || this.hasRelatedCustomError(metadata)) { this.submissionService.dispatchSave(this.submissionId); } + + // Save immediately when a type-bind field (values like `dc_type`, `edm_type`) or a sponsor/author value changes. + const isTypeBindField = this.formBuilderService.getTypeFieldValues() + .some((typeValue) => typeValue === metadata.replace(/\./g, '_')); + if (isTypeBindField || [SPONSOR_METADATA_NAME, AUTHOR_METADATA_FIELD_NAME].includes(metadata)) { + this.submissionService.dispatchSaveSection(this.submissionId, this.sectionData.id); + } } private hasRelatedCustomError(medatata): boolean {