Skip to content

Commit

Permalink
fix(occhab): fix edit habitats
Browse files Browse the repository at this point in the history
Fix the persistence of an habitat after an edit canceled.

Signed-off-by: Pierre-Narcisi <pierre.narcisi@mnhn.fr>
  • Loading branch information
Pierre-Narcisi authored and bouttier committed Jan 25, 2024
1 parent bbfe0bd commit 614b382
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export class OccHabFormComponent implements OnInit, OnDestroy {

// toggle the hab form and call the editHab function of form service
editHab(index) {
this.occHabForm.cancelHab();
this.occHabForm.editHab(index);
this.showHabForm = true;
}
Expand Down
13 changes: 11 additions & 2 deletions contrib/gn_module_occhab/frontend/app/services/form-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class OcchabFormService {
public typoHabControl = new UntypedFormControl();
public selectedTypo: any;
public currentEditingHabForm = null;
public currentHabCopy = null;
constructor(
private _fb: UntypedFormBuilder,
private _dateParser: NgbDateParserFormatter,
Expand Down Expand Up @@ -137,16 +138,24 @@ export class OcchabFormService {
* @param index: index of the habitat to edit
*/
editHab(index) {
const habArrayForm = this.stationForm.controls.habitats as UntypedFormArray;
this.currentEditingHabForm = index;
this.currentHabCopy = {
...habArrayForm.controls[this.currentEditingHabForm].value,
};
}

/** Cancel the current hab
* if idEdition = true, we patch the former value to no not loose it
* we keep the order
*/
cancelHab() {
this.deleteHab(this.currentEditingHabForm);
this.currentEditingHabForm = null;
if (this.currentEditingHabForm !== null) {
const habArrayForm = this.stationForm.controls.habitats as UntypedFormArray;
habArrayForm.controls[this.currentEditingHabForm].setValue(this.currentHabCopy);
this.currentHabCopy = null;
this.currentEditingHabForm = null;
}
}

/**
Expand Down

0 comments on commit 614b382

Please sign in to comment.