Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesfize committed Mar 18, 2024
1 parent 6dc7c80 commit b52770b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h5 class="card-title mb-0">Correspondance des champs avec le modèle</h5>
<div class="d-flex flex-row justify-content-center mt-3">
<div class="d-flex justify-content-center align-content-between row w-100">
<button
*ngIf="canRenameMapping"
*ngIf="cruved.U"
class="btn-sm mb-1 ml-1 col-xl-4 w-50"
mat-raised-button
color="primary"
Expand All @@ -35,7 +35,7 @@ <h5 class="card-title mb-0">Correspondance des champs avec le modèle</h5>
Renommer le modèle d'import
</button>
<button
*ngIf="canDeleteMapping"
*ngIf="cruved.D"
class="btn-sm mb-1 ml-1 col-xl-4 w-50"
mat-raised-button
color="warn"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnInit, ViewChild, ViewEncapsulation, Renderer2 } from '@angular/core';
import { Component, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { FormControl, FormGroup, FormBuilder, Validators, ValidatorFn } from '@angular/forms';
import { Observable, of } from 'rxjs';
Expand All @@ -16,6 +16,11 @@ import { ImportProcessService } from '../import-process.service';
import { ContentMapping, FieldMapping, FieldMappingValues } from '../../../models/mapping.model';
import { Step } from '../../../models/enums.model';
import { ConfigService } from '@geonature/services/config.service';
import {
Cruved,
CruvedWithScope,
toBooleanCruved,
} from '@geonature/modules/imports/models/cruved.model';

@Component({
selector: 'fields-mapping-step',
Expand All @@ -40,18 +45,19 @@ export class FieldsMappingStepComponent implements OnInit {
public autoMappedFields: Array<string> = [];

public formReady: boolean = false; // do not show frontend fields until all forms are ready
public fieldMappingForm = new FormControl(); // from to select the mapping to use
public mappingFormControl: FormGroup; // form group to associate each source fields to synthesis fields
public fieldMappingForm = new FormControl(); // form to select the mapping to use
public mappingFormControl: FormGroup; // form group to associate each source fields to import fields
public createOrRenameMappingForm = new FormControl(null, [Validators.required]); // form to add a new mapping
public modalCreateMappingForm = new FormControl('');

public displayAllValues: boolean = false; // checkbox to (not) show fields associated by the selected mapping
public canCreateMapping: boolean;
public canRenameMapping: boolean;
public canDeleteMapping: boolean;
public createMappingFormVisible: boolean = false; // show mapping creation form
public renameMappingFormVisible: boolean = false; // show rename mapping form
public createOrRenameMappingForm = new FormControl(null, [Validators.required]); // form to add a new mapping
public modalCreateMappingForm = new FormControl('');
public updateAvailable: boolean = false;
public mappingSelected: boolean = false;

private cruved: Cruved;

@ViewChild('saveMappingModal') saveMappingModal;
@ViewChild('deleteConfirmModal') deleteConfirmModal;

Expand All @@ -66,18 +72,17 @@ export class FieldsMappingStepComponent implements OnInit {
private _route: ActivatedRoute,
private _modalService: NgbModal,
public cruvedStore: CruvedStoreService,
public config: ConfigService,
private renderer: Renderer2
public config: ConfigService
) {
this.displayAllValues = this.config.IMPORT.DISPLAY_MAPPED_VALUES;
}

ngOnInit() {
this.step = this._route.snapshot.data.step;
this.importData = this.importProcessService.getImportData();
this.canCreateMapping = this.cruvedStore.cruved.IMPORT.module_objects.MAPPING.cruved.C > 0;
this.canRenameMapping = this.cruvedStore.cruved.IMPORT.module_objects.MAPPING.cruved.U > 0;
this.canDeleteMapping = this.cruvedStore.cruved.IMPORT.module_objects.MAPPING.cruved.D > 0;

this.cruved = toBooleanCruved(this.cruvedStore.cruved.IMPORT.module_objects.MAPPING.cruved);

forkJoin({
fieldMappings: this._importDataService.getFieldMappings(),
targetFields: this._importDataService.getBibFields(),
Expand All @@ -89,20 +94,22 @@ export class FieldsMappingStepComponent implements OnInit {
this.mappedTargetFields = new Set();
this.unmappedTargetFields = new Set();
this.mappingFormControl = this._fb.group({});
this.populateSyntheseForm();
this.populateMappingForm();
this.mappingFormControl.setValidators([this._fm.geoFormValidator]);
this.mappingFormControl.updateValueAndValidity();

this.sourceFields = sourceFields;
for (let entity of this.targetFields) {
for (let theme of entity.themes) {
for (let field of theme.fields) {
if (field.autogenerated) {
this.autogeneratedFields.push(field.name_field);

this.targetFields.forEach((entity) => {
entity.themes.forEach((theme) => {
theme.fields.forEach(({ autogenerated, name_field }) => {
if (autogenerated) {
this.autogeneratedFields.push(name_field);
}
}
}
}
});
});
});

this.mappedSourceFields = new Set();
this.unmappedSourceFields = new Set(sourceFields);
if (this.importData.fieldmapping) {
Expand Down Expand Up @@ -137,40 +144,42 @@ export class FieldsMappingStepComponent implements OnInit {
);
}

// add fields to the form
// when a field is referred multiple times, the same control is used with multiple observers
populateSyntheseForm() {
for (const entity of this.targetFields) {
for (const theme of entity.themes) {
for (const field of theme.fields) {
const key = field.name_field;
if (!(key in this.mappingFormControl.controls)) {
// A new control
if (!field.autogenerated) {
// autogenerated = checkbox
this.unmappedTargetFields.add(key);
/**
* A function to populate the mapping form based on target fields.
*/
populateMappingForm() {
this.targetFields.forEach((entity) => {
entity.themes.forEach(({ fields }) => {
fields.forEach((field) => {
const { name_field, autogenerated } = field;
if (!(name_field in this.mappingFormControl.controls)) {
if (!autogenerated) {
this.unmappedTargetFields.add(name_field);
}
const validators: Array<ValidatorFn> = field.mandatory ? [Validators.required] : [];
const control = new FormControl(null, validators);
control.valueChanges.subscribe((value) => {
this.onFieldMappingChange(key, value);
this.onFieldMappingChange(name_field, value);
});
this.mappingFormControl.addControl(key, control);
this.mappingFormControl.addControl(name_field, control);
} else {
// If a control with a given name already exists, it would not be replaced with a new one.
// The existing one will be updated with a new obser. We make sure to sync the references of it.
this.mappingFormControl.controls[key].valueChanges.subscribe((value) => {
this.mappingFormControl.controls[key].setValue(value, {
this.mappingFormControl.controls[name_field].valueChanges.subscribe((value) => {
this.mappingFormControl.controls[name_field].setValue(value, {
onlySelf: true,
emitEvent: false,
emitModelToViewChange: true,
});
});
this.mappingFormControl.addControl(key, this.mappingFormControl.controls[key]);
this.mappingFormControl.addControl(
name_field,
this.mappingFormControl.controls[name_field]
);
}
}
}
}
});
});
});
}

// should activate the "rename mapping" button or gray it?
Expand Down Expand Up @@ -274,10 +283,14 @@ export class FieldsMappingStepComponent implements OnInit {
this.userFieldMappings[index] = mapping;
});
}

onNewMappingSelected(mapping: FieldMapping): void {
/**
* Callback when a new mapping is selected
*
* @param {FieldMapping} mapping - the selected mapping
*/
onNewMappingSelected(mapping: FieldMapping = null): void {
this.hideCreateOrRenameMappingForm();
if (mapping == null) {
if (mapping === null) {
this.mappingFormControl.reset();
for (const field of this.autogeneratedFields) {
const control = this.mappingFormControl.get(field);
Expand Down Expand Up @@ -374,7 +387,7 @@ export class FieldsMappingStepComponent implements OnInit {
let mappingValue = this.fieldMappingForm.value;
if (
this.mappingFormControl.dirty &&
(this.canCreateMapping || (mappingValue && mappingValue.cruved.U && !mappingValue.public))
(this.cruved.C || (mappingValue && mappingValue.cruved.U && !mappingValue.public))
) {
if (mappingValue && !mappingValue.public) {
this.updateAvailable = true;
Expand All @@ -393,7 +406,10 @@ export class FieldsMappingStepComponent implements OnInit {
return of(this.importData).pipe(
concatMap((importData: Import) => {
if (this.mappingSelected || this.mappingFormControl.dirty) {
return this._importDataService.setImportFieldMapping(importData.id_import, this.getFieldMappingValues());
return this._importDataService.setImportFieldMapping(
importData.id_import,
this.getFieldMappingValues()
);
} else {
return of(importData);
}
Expand All @@ -410,11 +426,16 @@ export class FieldsMappingStepComponent implements OnInit {
(this.mappingSelected || this.mappingFormControl.dirty) &&
!this.config.IMPORT.ALLOW_VALUE_MAPPING
) {
return this._importDataService.getContentMapping(this.config.IMPORT.DEFAULT_VALUE_MAPPING_ID).pipe(
flatMap((mapping: ContentMapping) => {
return this._importDataService.setImportContentMapping(importData.id_import, mapping.values);
})
);
return this._importDataService
.getContentMapping(this.config.IMPORT.DEFAULT_VALUE_MAPPING_ID)
.pipe(
flatMap((mapping: ContentMapping) => {
return this._importDataService.setImportContentMapping(
importData.id_import,
mapping.values
);
})
);
} else {
return of(importData);
}
Expand Down

0 comments on commit b52770b

Please sign in to comment.