diff --git a/frontend/src/app/modules/imports/components/import_process/fields-mapping-step/mapping-theme/mapping-theme.component.ts b/frontend/src/app/modules/imports/components/import_process/fields-mapping-step/mapping-theme/mapping-theme.component.ts index 4d6f13d0c1..d09530048b 100644 --- a/frontend/src/app/modules/imports/components/import_process/fields-mapping-step/mapping-theme/mapping-theme.component.ts +++ b/frontend/src/app/modules/imports/components/import_process/fields-mapping-step/mapping-theme/mapping-theme.component.ts @@ -17,7 +17,7 @@ export class MappingThemeComponent implements OnInit { ngOnInit() {} isMapped(keySource: string) { - return this._fm.mappingStatus('mapped', keySource); + return this._fm.checkTargetFieldStatus('mapped', keySource); } displayAlert(field) { return ( diff --git a/frontend/src/app/modules/imports/components/import_process/header-stepper/header-stepper.component.ts b/frontend/src/app/modules/imports/components/import_process/header-stepper/header-stepper.component.ts index 052cc54d18..78ed1a7333 100644 --- a/frontend/src/app/modules/imports/components/import_process/header-stepper/header-stepper.component.ts +++ b/frontend/src/app/modules/imports/components/import_process/header-stepper/header-stepper.component.ts @@ -63,6 +63,5 @@ export class HeaderStepperComponent implements OnInit, OnChanges { destinationName: destination.label, fileName: full_file_name, }; - console.log(1, this.infoBox); } } diff --git a/frontend/src/app/modules/imports/services/mappings/field-mapping.service.ts b/frontend/src/app/modules/imports/services/mappings/field-mapping.service.ts index b5417c9e6b..20c3531ea1 100644 --- a/frontend/src/app/modules/imports/services/mappings/field-mapping.service.ts +++ b/frontend/src/app/modules/imports/services/mappings/field-mapping.service.ts @@ -13,8 +13,6 @@ import { FieldMapping, FieldMappingValues } from '../../models/mapping.model'; import { BehaviorSubject, Subscription, forkJoin } from 'rxjs'; import { ImportProcessService } from '../../components/import_process/import-process.service'; import { ConfigService } from '@geonature/services/config.service'; -import { TaxonTreeComponent } from '@geonature_common/form/taxon-tree/taxon-tree.component'; -import { Observable } from 'rxjs-compat'; import { FormService } from '@geonature_common/form/form.service'; interface FieldsMappingStatus { @@ -104,8 +102,15 @@ export class FieldMappingService { return this.sourceFields?.length - this.fieldMappingStatus.mapped.size; } - mappingStatus(category: string, item: string) { - return this.fieldMappingStatus[category].has(item); + /** + * Checks if a source field is [mapped|unmapped|autogenerated]. + * + * @param {string} mappingStatus - Field mapping status to check + * @param {string} targetField - The source field to check + * @return {boolean} - True if the item is associated with the given status + */ + checkTargetFieldStatus(mappingStatus: string, targetField: string) { + return this.fieldMappingStatus[mappingStatus].has(targetField); } parseData({ fieldMappings, targetFields, sourceFields }) { @@ -197,11 +202,12 @@ export class FieldMappingService { * */ populateMappingForm() { + // Populate the form group this.flattenTargetFieldData(this.targetFieldsData).forEach( ({ name_field, autogenerated, mandatory, mandatory_conditions, optional_conditions }) => { let control: AbstractControl; let oldValue = null; - if (!(name_field in this.fieldMappingStatus.mapped)) { + if (!this.checkTargetFieldStatus('mapped', name_field)) { control = new FormControl(null, mandatory ? [Validators.required] : []); } else { control = this.mappingFormGroup.controls[name_field]; @@ -211,10 +217,12 @@ export class FieldMappingService { else this.onFieldMappingChange(vc, oldValue); oldValue = vc; }); - + // Reset the control in the form group this.mappingFormGroup.addControl(name_field, control); } ); + + // Deal with inter-field conditions this.flattenTargetFieldData(this.targetFieldsData).forEach( ({ name_field, autogenerated, mandatory, mandatory_conditions, optional_conditions }) => { if (mandatory_conditions !== null && !mandatory) { @@ -225,11 +233,13 @@ export class FieldMappingService { ); mandatory_conditions.forEach((mandatory_condition) => { this.mappingFormGroup.get(mandatory_condition).valueChanges.subscribe((vc) => { - this.mappingFormGroup.get(name_field).updateValueAndValidity(); + this.mappingFormGroup + .get(name_field) + .updateValueAndValidity({ onlySelf: true, emitEvent: false }); }); }); } - + const formControl = this.mappingFormGroup.get(name_field); if (optional_conditions !== null && mandatory) { this.mappingFormGroup .get(name_field) @@ -237,9 +247,8 @@ export class FieldMappingService { this._formservice.NotRequiredIfControlIsNotNullValidator(optional_conditions) ); optional_conditions.forEach((optional_condition) => { - console.log(this.mappingFormGroup.get(optional_condition)); this.mappingFormGroup.get(optional_condition).valueChanges.subscribe((vc) => { - this.mappingFormGroup.get(name_field).updateValueAndValidity(); + formControl.updateValueAndValidity({ onlySelf: true, emitEvent: false }); }); }); } @@ -315,76 +324,17 @@ export class FieldMappingService { } } - // add a form control for each target field in the mappingForm - // mandatory target fields have a required validator + /** + * This method check if an autogenerated field was not checked. In this case, we display an alert that + * indicates to the user that UUID will not be generated. + * @jacquesfize : In my opinion, should be deleted... + * @param field + * @returns + */ displayAlert(field) { return ( field.name_field === 'unique_id_sinp_generate' && !this.mappingFormGroup.get(field.name_field).value ); } - /** - * Add custom validator to the form - */ - geoFormValidator(g: FormGroup): ValidationErrors | null { - /* We require a position (wkt/x,y) and/or a attachement (code{maille,commune,departement}) - We can set both as some file can have a position for few rows, and a attachement for others. - Contraints are: - - We must have a position or a attachement (or both). - - WKT and X/Y are mutually exclusive. - - Code{maille,commune,departement} are mutually exclusive. - */ - /* - 6 cases : - - all null : all required - - wkt == null and both coordinates != null : wkt not required, codes not required, coordinates required - - wkt != '' : wkt required, coordinates and codes not required - - one of the code not empty: others not required - - wkt and X/Y filled => error - */ - let xy = false; - let attachment = false; - - let wkt_errors = null; - let longitude_errors = null; - let latitude_errors = null; - let codemaille_errors = null; - let codecommune_errors = null; - let codedepartement_errors = null; - // check for position - if (g.value.longitude != null || g.value.latitude != null) { - xy = true; - // ensure both x/y are set - if (g.value.longitude == null) longitude_errors = { required: true }; - if (g.value.latitude == null) latitude_errors = { required: true }; - } - if (g.value.WKT != null) { - xy = true; - } - // check for attachment - if ( - g.value.codemaille != null || - g.value.codecommune != null || - g.value.codedepartement != null - ) { - attachment = true; - } - if (xy == false && attachment == false) { - wkt_errors = { required: true }; - longitude_errors = { required: true }; - latitude_errors = { required: true }; - codemaille_errors = { required: true }; - codecommune_errors = { required: true }; - codedepartement_errors = { required: true }; - } - if ('WKT' in g.controls) g.controls.WKT.setErrors(wkt_errors); - if ('longitude' in g.controls) g.controls.longitude.setErrors(longitude_errors); - if ('latitude' in g.controls) g.controls.latitude.setErrors(latitude_errors); - if ('codemaille' in g.controls) g.controls.codemaille.setErrors(codemaille_errors); - if ('codecommune' in g.controls) g.controls.codecommune.setErrors(codecommune_errors); - if ('codedepartement' in g.controls) - g.controls.codedepartement.setErrors(codedepartement_errors); - // we set errors on individual form control level, so we return no errors (null) at form group level. - return null; - } }