From cb94e248b1615fee0e1d7d0ae5c78dd46ab1bf5b Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 5 Dec 2024 09:31:17 +0100 Subject: [PATCH 1/3] The Save action is automatically dispatched when the type is changed --- .../form/builder/form-builder.service.ts | 10 +++--- .../sections/form/section-form.component.ts | 35 +++++++++++++++---- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/app/shared/form/builder/form-builder.service.ts b/src/app/shared/form/builder/form-builder.service.ts index b6f7a4673dd..8755d774f57 100644 --- a/src/app/shared/form/builder/form-builder.service.ts +++ b/src/app/shared/form/builder/form-builder.service.ts @@ -94,7 +94,7 @@ export class FormBuilderService extends DynamicFormService { this.typeFields.set(TYPE_BIND_DEFAULT_KEY, 'dc_type'); // If optional config service was passed, perform an initial set of type field (default dc_type) for type binds if (hasValue(this.configService)) { - this.setTypeBindFieldFromConfig(); + this.setTypeBindFieldFromConfig(this.typeFields); } @@ -560,7 +560,7 @@ export class FormBuilderService extends DynamicFormService { /** * Get the type bind field from config */ - setTypeBindFieldFromConfig(metadataField: string = null): void { + setTypeBindFieldFromConfig(typeBindFields: Map, metadataField: string = null): void { this.configService.findByPropertyName('submit.type-bind.field').pipe( getFirstCompletedRemoteData(), ).subscribe((remoteData: any) => { @@ -584,7 +584,7 @@ export class FormBuilderService extends DynamicFormService { const normalizedValuePart = valuePart.replace(/\./g, '_'); // Set the value in the typeFields map - this.typeFields.set(metadataFieldConfigPart, normalizedValuePart); + typeBindFields.set(metadataFieldConfigPart, normalizedValuePart); if (metadataFieldConfigPart === metadataField) { typeFieldConfigValue = valuePart; @@ -596,7 +596,7 @@ export class FormBuilderService extends DynamicFormService { } // Always update the typeFields map with the default value, normalized - this.typeFields.set(TYPE_BIND_DEFAULT_KEY, typeFieldConfigValue.replace(/\./g, '_')); + typeBindFields.set(TYPE_BIND_DEFAULT_KEY, typeFieldConfigValue.replace(/\./g, '_')); }); }); } @@ -607,7 +607,7 @@ export class FormBuilderService extends DynamicFormService { */ getTypeField(metadataField: string): string { if (hasValue(this.configService) && isEmpty(this.typeFields.values())) { - this.setTypeBindFieldFromConfig(metadataField); + this.setTypeBindFieldFromConfig(this.typeFields, metadataField); } else if (hasNoValue(this.typeFields.get(TYPE_BIND_DEFAULT_KEY))) { this.typeFields.set(TYPE_BIND_DEFAULT_KEY, 'dc_type'); } diff --git a/src/app/submission/sections/form/section-form.component.ts b/src/app/submission/sections/form/section-form.component.ts index 02c574c204d..19198a3fb0c 100644 --- a/src/app/submission/sections/form/section-form.component.ts +++ b/src/app/submission/sections/form/section-form.component.ts @@ -135,6 +135,11 @@ export class SubmissionSectionFormComponent extends SectionModelComponent { */ public sponsorRefreshTimeout = 20; + /** + * The map of type bind fields. We need to have this map to check if the type field is updated. + */ + private typeFields: Map; + /** * The FormComponent reference */ @@ -175,6 +180,7 @@ export class SubmissionSectionFormComponent extends SectionModelComponent { @Inject('sectionDataProvider') public injectedSectionData: SectionDataObject, @Inject('submissionIdProvider') public injectedSubmissionId: string) { super(injectedCollectionId, injectedSectionData, injectedSubmissionId); + this.typeFields = new Map(); } /** @@ -209,6 +215,7 @@ export class SubmissionSectionFormComponent extends SectionModelComponent { this.cdr.detectChanges(); } }); + this.formBuilderService.setTypeBindFieldFromConfig(this.typeFields); } /** @@ -424,17 +431,31 @@ export class SubmissionSectionFormComponent extends SectionModelComponent { this.submissionService.dispatchSave(this.submissionId); } - if (metadata === SPONSOR_METADATA_NAME) { - this.submissionService.dispatchSaveSection(this.submissionId, this.sectionData.id); - this.reinitializeForm(SPONSOR_METADATA_NAME, value); - } + // Check if the type field is updated it could be e.g. `dc.type` or `edm.type` metadata field + // `.some()` method is used to break the loop when the type field is found. + // The values in the `typeFields` are in the format `dc_type` or `edm_type`, so we need to replace `_` with `.` + [...this.typeFields.values()].some(typeValue => { + if (typeValue.replace('_', '.') === metadata) { + this.handleFormSave(typeValue, value); + return true; + } + }); - if (metadata === AUTHOR_METADATA_FIELD_NAME) { - this.submissionService.dispatchSaveSection(this.submissionId, this.sectionData.id); - this.reinitializeForm(AUTHOR_METADATA_FIELD_NAME, value); + if ([SPONSOR_METADATA_NAME, AUTHOR_METADATA_FIELD_NAME].includes(metadata)) { + this.handleFormSave(metadata, value); } } + /** + * Handle form save and reinitialization + * @param metadata + * @param value + */ + handleFormSave(metadata, value) { + this.submissionService.dispatchSaveSection(this.submissionId, this.sectionData.id); + this.reinitializeForm(metadata, value); + } + /** * This method updates specific input field e.g. `local.sponsor` and check if the metadata value was updated * in the DB. When the metadata is updated in the DB refresh this input field. From 4ca426f6e25fd253fa454e890f0a70ce1855b46c Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 5 Dec 2024 09:35:43 +0100 Subject: [PATCH 2/3] Updated the handleFormSave method name to `dispatchFormSaveAndReinitialize` --- .../submission/sections/form/section-form.component.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app/submission/sections/form/section-form.component.ts b/src/app/submission/sections/form/section-form.component.ts index 19198a3fb0c..3b4af9f5ade 100644 --- a/src/app/submission/sections/form/section-form.component.ts +++ b/src/app/submission/sections/form/section-form.component.ts @@ -436,22 +436,22 @@ export class SubmissionSectionFormComponent extends SectionModelComponent { // The values in the `typeFields` are in the format `dc_type` or `edm_type`, so we need to replace `_` with `.` [...this.typeFields.values()].some(typeValue => { if (typeValue.replace('_', '.') === metadata) { - this.handleFormSave(typeValue, value); + this.dispatchFormSaveAndReinitialize(typeValue, value); return true; } }); if ([SPONSOR_METADATA_NAME, AUTHOR_METADATA_FIELD_NAME].includes(metadata)) { - this.handleFormSave(metadata, value); + this.dispatchFormSaveAndReinitialize(metadata, value); } } /** - * Handle form save and reinitialization + * Dispatch form save and reinitialize form * @param metadata * @param value */ - handleFormSave(metadata, value) { + dispatchFormSaveAndReinitialize(metadata, value) { this.submissionService.dispatchSaveSection(this.submissionId, this.sectionData.id); this.reinitializeForm(metadata, value); } From 0faade3396b9155ad3364c196e377360cd4002cf Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 5 Dec 2024 10:00:17 +0100 Subject: [PATCH 3/3] Added missing function to mock object --- src/app/shared/mocks/form-builder-service.mock.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app/shared/mocks/form-builder-service.mock.ts b/src/app/shared/mocks/form-builder-service.mock.ts index eaaeb608297..f863ce905ef 100644 --- a/src/app/shared/mocks/form-builder-service.mock.ts +++ b/src/app/shared/mocks/form-builder-service.mock.ts @@ -39,7 +39,8 @@ export function getMockFormBuilderService(): FormBuilderService { {match: 'VISIBLE', operator: 'OR', when: [{id: 'dc.type', value: 'boundType'}]} ] } - ) + ), + setTypeBindFieldFromConfig: {}, }); }