From 8f2548224ceceab5f801e6865ceb4eb31e660431 Mon Sep 17 00:00:00 2001 From: Pierre-Narcisi Date: Thu, 28 Nov 2024 16:36:35 +0100 Subject: [PATCH] Fix occhab observer --- .../frontend/app/services/form-service.ts | 116 ++++++++++++------ 1 file changed, 76 insertions(+), 40 deletions(-) diff --git a/contrib/gn_module_occhab/frontend/app/services/form-service.ts b/contrib/gn_module_occhab/frontend/app/services/form-service.ts index 15c8d27c74..000c19a127 100644 --- a/contrib/gn_module_occhab/frontend/app/services/form-service.ts +++ b/contrib/gn_module_occhab/frontend/app/services/form-service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable } from "@angular/core"; import { UntypedFormBuilder, UntypedFormGroup, @@ -6,13 +6,13 @@ import { Validators, AbstractControl, UntypedFormArray, -} from '@angular/forms'; -import { NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap'; -import { FormService } from '@geonature_common/form/form.service'; -import { DataFormService } from '@geonature_common/form/data-form.service'; -import { OcchabStoreService } from './store.service'; -import { ConfigService } from '@geonature/services/config.service'; -import { Station, StationFeature } from '../models'; +} from "@angular/forms"; +import { NgbDateParserFormatter } from "@ng-bootstrap/ng-bootstrap"; +import { FormService } from "@geonature_common/form/form.service"; +import { DataFormService } from "@geonature_common/form/data-form.service"; +import { OcchabStoreService } from "./store.service"; +import { ConfigService } from "@geonature/services/config.service"; +import { Station, StationFeature } from "../models"; @Injectable() export class OcchabFormService { @@ -42,8 +42,14 @@ export class OcchabFormService { id_dataset: [null, Validators.required], date_min: [null, Validators.required], date_max: [null, Validators.required], - observers: [null, !this.config.OCCHAB.OBSERVER_AS_TXT ? Validators.required : null], - observers_txt: [null, this.config.OCCHAB.OBSERVER_AS_TXT ? Validators.required : null], + observers: [ + null, + !this.config.OCCHAB.OBSERVER_AS_TXT ? Validators.required : null, + ], + observers_txt: [ + null, + this.config.OCCHAB.OBSERVER_AS_TXT ? Validators.required : null, + ], is_habitat_complex: false, id_nomenclature_exposure: null, altitude_min: null, @@ -58,11 +64,14 @@ export class OcchabFormService { habitats: this._fb.array([]), }); stationForm.setValidators([ - this._formService.dateValidator(stationForm.get('date_min'), stationForm.get('date_max')), + this._formService.dateValidator( + stationForm.get("date_min"), + stationForm.get("date_max") + ), this._formService.minMaxValidator( - stationForm.get('altitude_min'), - stationForm.get('altitude_max'), - 'invalidAlt' + stationForm.get("altitude_min"), + stationForm.get("altitude_max"), + "invalidAlt" ), ]); @@ -71,8 +80,9 @@ export class OcchabFormService { patchDefaultNomenclaureStation(defaultNomenclature) { this.stationForm.patchValue({ - id_nomenclature_area_surface_calculation: defaultNomenclature['METHOD_CALCUL_SURFACE'], - id_nomenclature_geographic_object: defaultNomenclature['NAT_OBJ_GEO'], + id_nomenclature_area_surface_calculation: + defaultNomenclature["METHOD_CALCUL_SURFACE"], + id_nomenclature_geographic_object: defaultNomenclature["NAT_OBJ_GEO"], }); } @@ -83,12 +93,14 @@ export class OcchabFormService { nom_cite: null, habref: [Validators.required, this.cdHabValidator], id_nomenclature_determination_type: defaultNomenclature - ? defaultNomenclature['DETERMINATION_TYP_HAB'] + ? defaultNomenclature["DETERMINATION_TYP_HAB"] : null, determiner: null, id_nomenclature_community_interest: null, id_nomenclature_collection_technique: [ - defaultNomenclature ? defaultNomenclature['TECHNIQUE_COLLECT_HAB'] : null, + defaultNomenclature + ? defaultNomenclature["TECHNIQUE_COLLECT_HAB"] + : null, Validators.required, ], recovery_percentage: null, @@ -100,10 +112,16 @@ export class OcchabFormService { } technicalValidator(habForm: AbstractControl): { [key: string]: boolean } { - const technicalValue = habForm.get('id_nomenclature_collection_technique').value; - const technicalPrecision = habForm.get('technical_precision').value; + const technicalValue = habForm.get( + "id_nomenclature_collection_technique" + ).value; + const technicalPrecision = habForm.get("technical_precision").value; - if (technicalValue && technicalValue.cd_nomenclature == '10' && !technicalPrecision) { + if ( + technicalValue && + technicalValue.cd_nomenclature == "10" && + !technicalPrecision + ) { return { invalidTechnicalValues: true }; } return null; @@ -129,7 +147,10 @@ export class OcchabFormService { addNewHab() { const currentHabNumber = this.stationForm.value.habitats.length - 1; const habFormArray = this.stationForm.controls.habitats as UntypedFormArray; - habFormArray.insert(0, this.initHabForm(this._storeService.defaultNomenclature)); + habFormArray.insert( + 0, + this.initHabForm(this._storeService.defaultNomenclature) + ); this.currentEditingHabForm = 0; } @@ -151,8 +172,11 @@ export class OcchabFormService { */ cancelHab() { if (this.currentEditingHabForm !== null) { - const habArrayForm = this.stationForm.controls.habitats as UntypedFormArray; - habArrayForm.controls[this.currentEditingHabForm].setValue(this.currentHabCopy); + const habArrayForm = this.stationForm.controls + .habitats as UntypedFormArray; + habArrayForm.controls[this.currentEditingHabForm].setValue( + this.currentHabCopy + ); this.currentHabCopy = null; this.currentEditingHabForm = null; } @@ -185,8 +209,8 @@ export class OcchabFormService { this._gn_dataSerice.getGeoInfo(geom).subscribe( (data) => { this.stationForm.patchValue({ - altitude_min: data['altitude']['altitude_min'], - altitude_max: data['altitude']['altitude_max'], + altitude_min: data["altitude"]["altitude_min"], + altitude_max: data["altitude"]["altitude_max"], }); }, () => { @@ -211,7 +235,7 @@ export class OcchabFormService { */ formatNomenclature(obj) { Object.keys(obj).forEach((key) => { - if (key.startsWith('id_nomenclature') && obj[key]) { + if (key.startsWith("id_nomenclature") && obj[key]) { obj[key] = obj[key].id_nomenclature; } }); @@ -232,30 +256,39 @@ export class OcchabFormService { ...hab, id_nomenclature_determination_type: this.getOrNull( hab, - 'nomenclature_determination_method' + "nomenclature_determination_method" ), id_nomenclature_collection_technique: this.getOrNull( hab, - 'nomenclature_collection_technique' + "nomenclature_collection_technique" + ), + id_nomenclature_abundance: this.getOrNull( + hab, + "nomenclature_abundance" ), - id_nomenclature_abundance: this.getOrNull(hab, 'nomenclature_abundance'), }; }); station.habitats.forEach((hab, index) => { - formatedHabitats[index]['habref'] = hab.habref || {}; - formatedHabitats[index]['habref']['search_name'] = hab.nom_cite; + formatedHabitats[index]["habref"] = hab.habref || {}; + formatedHabitats[index]["habref"]["search_name"] = hab.nom_cite; }); - station['habitats'] = formatedHabitats; + station["habitats"] = formatedHabitats; return { ...station, date_min: this._dateParser.parse(station.date_min), date_max: this._dateParser.parse(station.date_max), - id_nomenclature_geographic_object: this.getOrNull(station, 'nomenclature_geographic_object'), + id_nomenclature_geographic_object: this.getOrNull( + station, + "nomenclature_geographic_object" + ), id_nomenclature_area_surface_calculation: this.getOrNull( station, - 'nomenclature_area_surface_calculation' + "nomenclature_area_surface_calculation" + ), + id_nomenclature_exposure: this.getOrNull( + station, + "nomenclature_exposure" ), - id_nomenclature_exposure: this.getOrNull(station, 'nomenclature_exposure'), }; } @@ -269,6 +302,9 @@ export class OcchabFormService { const formatedData = this.formatStationAndHabtoPatch(oneStation.properties); this.stationForm.patchValue(formatedData); + this.stationForm.patchValue({ + observers: oneStation.properties.observers[0], + }); this.stationForm.patchValue({ geom_4326: oneStation.geometry, }); @@ -284,7 +320,7 @@ export class OcchabFormService { formData.habitats.forEach((element) => { if (element.habref) { element.cd_hab = element.habref.cd_hab; - delete element['habref']; + delete element["habref"]; } }); @@ -301,10 +337,10 @@ export class OcchabFormService { }); // Format data in geojson - const geom = formData['geom_4326']; - delete formData['geom_4326']; + const geom = formData["geom_4326"]; + delete formData["geom_4326"]; return { - type: 'Feature', + type: "Feature", geometry: { ...geom, },