From 70d3d72e1efec7d5f767327f4f415bbb86738133 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Thu, 3 Nov 2022 10:25:02 +0100
Subject: [PATCH] refactor: move parametric section form generation to form
 service

refs #496
---
 .../calculator.component.ts                   | 18 ++--------
 src/app/services/formulaire.service.ts        | 36 ++++++++++++++++++-
 2 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/src/app/components/generic-calculator/calculator.component.ts b/src/app/components/generic-calculator/calculator.component.ts
index 901fa1343..fc57d1653 100644
--- a/src/app/components/generic-calculator/calculator.component.ts
+++ b/src/app/components/generic-calculator/calculator.component.ts
@@ -965,25 +965,11 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
      * Génère une SectionParametree à partir du module RegimeUniforme en cours
      */
     public async generateRuSp() {
-        const ru = (this._formulaire.currentNub as RegimeUniforme);
-        // copy section
-        const serialisedSection = ru.section.serialise();
-        const sectionCopy = Session.getInstance().unserialiseSingleNub(serialisedSection, false).nub;
-        const secParam = new SectionParametree(sectionCopy as acSection);
-        // copy value of calculated param
-        const cp = ru.calculatedParam;
-        const scp = secParam.section.getParameter(cp.symbol);
-        if (cp.hasMultipleValues) {
-            scp.setValues(ru.result.getCalculatedValues().map(v => round(v, this.appSetupService.displayPrecision)));
-        } else {
-            scp.singleValue = ru.result.vCalc;
-        }
-        Session.getInstance().registerNub(secParam);
-
-        const f: FormulaireDefinition = await this.formulaireService.createFormulaire(CalculatorType.SectionParametree, secParam);
+        const f = await this.formulaireService.generateParametricSectionForm();
         // calculate
         f.doCompute();
         // go to new SP
+        this.router.navigate(["/calculator", f.uid]);
     }
 
     public get generatePARSimulationEnabled(): boolean {
diff --git a/src/app/services/formulaire.service.ts b/src/app/services/formulaire.service.ts
index 4c9e42baa..90095e6df 100644
--- a/src/app/services/formulaire.service.ts
+++ b/src/app/services/formulaire.service.ts
@@ -21,7 +21,13 @@ import {
     LoiDebit,
     PbCloison,
     CreateStructure,
-    Structure
+    Structure,
+    SectionNub,
+    SectionParametree,
+    acSection,
+    round,
+    RegimeUniforme,
+    CourbeRemous
 } from "jalhyd";
 
 import { ApplicationSetupService } from "./app-setup.service";
@@ -851,4 +857,32 @@ export class FormulaireService extends Observable {
             }
         }
     }
+
+    /**
+     * Génère un formulaire SectionParametree à partir du module courant 
+     * s'il est du type régime uniforme ou courbe de remous
+     */
+    public async generateParametricSectionForm(): Promise<FormulaireDefinition> {
+        if (this.currentForm.currentNub instanceof SectionNub) {
+            const sn: SectionNub = this.currentForm.currentNub;
+            if (sn instanceof RegimeUniforme || sn instanceof CourbeRemous) {
+                // copy section
+                const serialisedSection = sn.section.serialise();
+                const sectionCopy = Session.getInstance().unserialiseSingleNub(serialisedSection, false).nub;
+                const secParam = new SectionParametree(sectionCopy as acSection);
+                // copy value of calculated param
+                const cp: ParamDefinition = sn.calculatedParam;
+                const scp: ParamDefinition = secParam.section.getParameter(cp.symbol);
+                if (cp.hasMultipleValues) {
+                    scp.setValues(sn.result.getCalculatedValues().map(v => round(v, this.appSetupService.displayPrecision)));
+                } else {
+                    scp.singleValue = sn.result.vCalc;
+                }
+                Session.getInstance().registerNub(secParam);
+
+                return await this.createFormulaire(CalculatorType.SectionParametree, secParam);
+            }
+        }
+        return Promise.reject("cannot create parametric section from current form");
+    }
 }
-- 
GitLab