Skip to content

Commit

Permalink
feat(import): improve default value json edition
Browse files Browse the repository at this point in the history
  • Loading branch information
20cents committed Nov 28, 2024
1 parent e97c5b9 commit e7c54e7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
></pnx-nomenclature>
<textarea
*ngSwitchCase="'textarea'"
rows="4"
class="form-control form-control-sm"
formControlName="{{ field.name_field }}_default_value"
></textarea>
Expand All @@ -108,6 +109,12 @@
formControlName="{{ field.name_field }}_default_value"
/>
</ng-container>
<div
*ngIf="_fm.mappingFormGroup.controls[field.name_field + '_default_value'].hasError('invalidJSON')"
class="invalid-feedback d-block"
>
JSON non valide
</div>
</div>
</div>
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { BehaviorSubject, Subscription, forkJoin } from 'rxjs';
import { ImportProcessService } from '../../components/import_process/import-process.service';
import { ConfigService } from '@geonature/services/config.service';
import { FormService } from '@geonature_common/form/form.service';

import { isPlainObject } from 'lodash';
interface FieldsMappingStatus {
mapped: Set<string>;
unmapped: Set<string>;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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];
}
Expand Down Expand Up @@ -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);
}
}
});
}
Expand Down

0 comments on commit e7c54e7

Please sign in to comment.