From f83cc1fb391b228316efad9dbaccf0e868668ae6 Mon Sep 17 00:00:00 2001
From: Mathias Chouet <mathias.chouet@irstea.fr>
Date: Mon, 23 Mar 2020 11:31:12 +0100
Subject: [PATCH] Fix bug in fieldsets refresh after compute

---
 src/app/formulaire/definition/form-fixedvar.ts           | 9 +++++----
 src/app/formulaire/definition/form-macrorugo-compound.ts | 6 ++----
 src/app/formulaire/definition/form-section.ts            | 2 ++
 3 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/app/formulaire/definition/form-fixedvar.ts b/src/app/formulaire/definition/form-fixedvar.ts
index 72928a7b9..20314b520 100644
--- a/src/app/formulaire/definition/form-fixedvar.ts
+++ b/src/app/formulaire/definition/form-fixedvar.ts
@@ -89,8 +89,8 @@ export class FormulaireFixedVar extends FormulaireDefinition {
 
     protected compute() {
         this.runNubCalc(this.currentNub);
+        this.refreshFieldsets(); // important: before reaffectResultComponents() or it will break results components localization
         this.reaffectResultComponents();
-        this.refreshFieldsets();
     }
 
     protected reaffectResultComponents() {
@@ -118,6 +118,9 @@ export class FormulaireFixedVar extends FormulaireDefinition {
         }
     }
 
+    /**
+     * Forces all fieldsets to update all their fields
+     */
     protected refreshFieldsets() {
         for (const fs of this.allFieldsets) {
             fs.updateFields();
@@ -132,9 +135,7 @@ export class FormulaireFixedVar extends FormulaireDefinition {
         if (data.action === "propertyChange") {
             this.reset();
             // reflect changes in GUI (who knows ?), for ex. show / hide dependent fields
-            for (const fs of this.allFieldsets) {
-                fs.updateFields();
-            }
+            this.refreshFieldsets();
         }
     }
 }
diff --git a/src/app/formulaire/definition/form-macrorugo-compound.ts b/src/app/formulaire/definition/form-macrorugo-compound.ts
index 663da1f96..c94dad30a 100644
--- a/src/app/formulaire/definition/form-macrorugo-compound.ts
+++ b/src/app/formulaire/definition/form-macrorugo-compound.ts
@@ -87,10 +87,8 @@ export class FormulaireMacrorugoCompound extends FormulaireRepeatableFieldset {
      * Reflect inclinedApron property state in GUI
      */
     public updateApronState(inclined: MRCInclination) {
-        for (const fs of this.allFieldsets) {
-            // show / hide dependent fields (read from model)
-            fs.updateFields();
-        }
+        // show / hide dependent fields (read from model)
+        this.refreshFieldsets();
         // show / hide children list (GUI only)
         for (const elt of this.allFormElements) {
             if (elt instanceof FieldsetContainer) {
diff --git a/src/app/formulaire/definition/form-section.ts b/src/app/formulaire/definition/form-section.ts
index db63b5a1d..77669213a 100644
--- a/src/app/formulaire/definition/form-section.ts
+++ b/src/app/formulaire/definition/form-section.ts
@@ -18,6 +18,8 @@ export class FormulaireSection extends FormulaireFixedVar {
                 // show / hide dependent fields
                 fs.updateFields();
             }
+            // show / hide dependent fields
+            this.refreshFieldsets();
             // empty fields ? only those belonging to the specific section type
             if (ServiceFactory.instance.applicationSetupService.enableEmptyFieldsOnFormInit) {
                 // "LargeurBerge" is hackily used as LargeurFond in Rectangular and Trapez sections, omit it here
-- 
GitLab