From e7c54e7729cbc0bd31304b7d3efaf126c2791da3 Mon Sep 17 00:00:00 2001 From: Vincent Bourgeois Date: Thu, 28 Nov 2024 13:58:48 +0100 Subject: [PATCH] feat(import): improve default value json edition --- .../mapping-theme.component.html | 7 ++++ .../mappings/field-mapping.service.ts | 35 ++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/frontend/src/app/modules/imports/components/import_process/fields-mapping-step/mapping-theme/mapping-theme.component.html b/frontend/src/app/modules/imports/components/import_process/fields-mapping-step/mapping-theme/mapping-theme.component.html index 02b6de014b..c222066290 100644 --- a/frontend/src/app/modules/imports/components/import_process/fields-mapping-step/mapping-theme/mapping-theme.component.html +++ b/frontend/src/app/modules/imports/components/import_process/fields-mapping-step/mapping-theme/mapping-theme.component.html @@ -98,6 +98,7 @@ > @@ -108,6 +109,12 @@ formControlName="{{ field.name_field }}_default_value" /> +
+ JSON non valide +
; unmapped: Set; @@ -238,7 +238,7 @@ export class FieldMappingService { populateMappingForm() { // Populate the form group this.flattenTargetFieldData(this.targetFieldsData).forEach( - ({ name_field, autogenerated, mandatory, mandatory_conditions, optional_conditions }) => { + ({ name_field, mandatory, multi }) => { let column_src_control: AbstractControl; let oldValue = null; if (!(name_field in this.mappingFormGroup.controls)) { @@ -256,7 +256,25 @@ export class FieldMappingService { let default_value_control: AbstractControl; const name_field_default_value = `${name_field}_default_value`; if (!(name_field_default_value in this.mappingFormGroup.controls)) { - default_value_control = new FormControl(null); + default_value_control = new FormControl(null, [ + (control: AbstractControl): ValidationErrors | null => { + if (!multi || control.value == null || control.value == '') { + return null; + } + + let isError = false; + try { + const json = JSON.parse(control.value); + if (!isPlainObject(json)) { + isError = true; + } + } catch (error) { + isError = true; + } + + return isError ? { invalidJSON: true } : null; + }, + ]); } else { default_value_control = this.mappingFormGroup.controls[name_field_default_value]; } @@ -367,7 +385,16 @@ export class FieldMappingService { const default_value_control = this.mappingFormGroup.get(`${target}_default_value`); if (default_value_control) { - default_value_control.setValue(source.default_value); + try { + const json = JSON.parse(source.default_value); + if (typeof json === 'object') { + default_value_control.setValue(JSON.stringify(json, null, 2)); + } else { + default_value_control.setValue(source.default_value); + } + } catch (error) { + default_value_control.setValue(source.default_value); + } } }); }