From 3643040bd80a6fcfcb81c2a4b1e90b30a18c305b Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Wed, 6 Mar 2019 16:51:09 +0100
Subject: [PATCH] =?UTF-8?q?Rotueur=20:=20redirection=20vers=20la=20liste?=
 =?UTF-8?q?=20si=20le=20module=20demand=C3=A9=20n'existe=20pas?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/app/app.component.ts                          | 15 ++++++++++++++-
 .../generic-calculator/calculator.component.ts    |  4 +++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index 80b386e66..4e9c48e53 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -88,7 +88,12 @@ export class AppComponent implements OnInit, OnDestroy, Observer {
         const path = event.snapshot.url[0].path;
         if (path === "calculator") {
           const calcUid = event.snapshot.params.uid;
-          this.setActiveCalc(calcUid);
+          if (this.calculatorExists(calcUid)) {
+            this.setActiveCalc(calcUid);
+          } else {
+            // if required calculator does not exist, redirect to list page
+            this.toList();
+          }
         } else {
           this.setActiveCalc(null);
         }
@@ -264,6 +269,14 @@ export class AppComponent implements OnInit, OnDestroy, Observer {
     }
   }
 
+  /**
+   * Returns true if a form having "formUid" as UID exists
+   * @param formId UID to look for
+   */
+  private calculatorExists(formId: string): boolean {
+    return (this.getCalculatorIndexFromId(formId) > -1);
+  }
+
   private getCalculatorIndexFromId(formId: string) {
     const index = this._calculators.reduce((resultIndex, calc, currIndex) => {
       if (resultIndex === -1 && calc["uid"] === formId) {
diff --git a/src/app/components/generic-calculator/calculator.component.ts b/src/app/components/generic-calculator/calculator.component.ts
index 4d9f0fd92..54141da0a 100644
--- a/src/app/components/generic-calculator/calculator.component.ts
+++ b/src/app/components/generic-calculator/calculator.component.ts
@@ -183,7 +183,9 @@ export class GenericCalculatorComponent extends BaseComponent implements OnInit,
     }
 
     private unsubscribeRouter() {
-        this._subscription.unsubscribe();
+        if (this._subscription) {
+            this._subscription.unsubscribe();
+        }
     }
 
     /*
-- 
GitLab