From 9d9eda087a022a50f85564a2f8671d8fb9bb381f Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Fri, 6 Nov 2020 16:31:13 +0100
Subject: [PATCH] FEAT #15350 TIME 0:20 fix feature tour

---
 .../home/administration.component.ts          |  2 +-
 src/frontend/app/home/home.component.ts       |  4 +-
 src/frontend/service/featureTour.service.ts   | 39 ++++++++++++-------
 3 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/src/frontend/app/administration/home/administration.component.ts b/src/frontend/app/administration/home/administration.component.ts
index 9cfcdd9ea6e..3559b0623fa 100644
--- a/src/frontend/app/administration/home/administration.component.ts
+++ b/src/frontend/app/administration/home/administration.component.ts
@@ -87,7 +87,7 @@ export class AdministrationComponent implements OnInit, AfterViewInit {
     }
 
     ngAfterViewInit(): void {
-        if (this.headerService.user.mode === 'root_visible' || this.headerService.user.mode === 'root_invisible') {
+        if (!this.featureTourService.isComplete()) {
             this.featureTourService.init();
         }
     }
diff --git a/src/frontend/app/home/home.component.ts b/src/frontend/app/home/home.component.ts
index 417d9c08e61..56fdcd3c9db 100644
--- a/src/frontend/app/home/home.component.ts
+++ b/src/frontend/app/home/home.component.ts
@@ -71,8 +71,8 @@ export class HomeComponent implements OnInit, AfterViewInit {
             });
     }
 
-    ngAfterViewInit(): void {        
-        if (this.headerService.user.mode === 'root_visible' || this.headerService.user.mode === 'root_invisible') {
+    ngAfterViewInit(): void {
+        if (!this.featureTourService.isComplete()) {
             this.featureTourService.init();
         }
         this.http.get('../rest/home/lastRessources')
diff --git a/src/frontend/service/featureTour.service.ts b/src/frontend/service/featureTour.service.ts
index 52b2ade804b..2bd8599222e 100644
--- a/src/frontend/service/featureTour.service.ts
+++ b/src/frontend/service/featureTour.service.ts
@@ -102,10 +102,11 @@ export class FeatureTourService {
         private http: HttpClient,
         private notify: NotificationService,
     ) {
-        this.getCurrentStepType();
     }
 
     init() {
+        this.getCurrentStepType();
+
         if (!this.functionService.empty(this.currentStepType)) {
             const steps = this.tour.filter(step => step.type === this.currentStepType).map(step => step.stepId);
             this.joyrideService.startTour(
@@ -132,7 +133,9 @@ export class FeatureTourService {
                     /*handle error*/
                 },
                 () => {
-                    if (this.currentTour.redirectToAdmin) {
+                    if (this.currentTour === null) {
+                        this.router.navigate(['/home']);
+                    } else if (this.currentTour.redirectToAdmin) {
                         this.router.navigate(['/administration']);
                     } else {
                         this.endTour();
@@ -143,22 +146,28 @@ export class FeatureTourService {
     }
 
     getCurrentStepType() {
-        if (this.headerService.user.userId !== null) {
-            this.featureTourEnd = this.headerService.user.featureTour;
-        }
-        const unique = [...new Set(this.tour.map(item => item.type))];
-        this.currentStepType = unique.filter(stepType => this.featureTourEnd.indexOf(stepType) === -1)[0];
+        this.featureTourEnd = this.headerService.user.featureTour;
+        this.currentStepType = this.getFeatureTourTypes().filter(stepType => this.featureTourEnd.indexOf(stepType) === -1)[0];
     }
 
     endTour() {
-        this.featureTourEnd.push(this.currentStepType);
-        this.http.put('../rest/currentUser/profile/featureTour', {featureTour : this.featureTourEnd}).pipe(
-            catchError((err: any) => {
-                this.notify.handleSoftErrors(err);
-                return of(false);
-            })
-        ).subscribe();
-        this.getCurrentStepType();
+        if (this.currentStepType !== undefined) {
+            this.featureTourEnd.push(this.currentStepType);
+            this.http.put('../rest/currentUser/profile/featureTour', {featureTour : this.featureTourEnd}).pipe(
+                catchError((err: any) => {
+                    this.notify.handleSoftErrors(err);
+                    return of(false);
+                })
+            ).subscribe();
+            this.getCurrentStepType();
+        }
+    }
+
+    getFeatureTourTypes() {
+        return [...new Set(this.tour.map(item => item.type))];
     }
 
+    isComplete() {
+        return (this.headerService.user.mode === 'root_visible' || this.headerService.user.mode === 'root_invisible') && this.headerService.user.featureTour.length === this.getFeatureTourTypes().length;
+    }
 }
-- 
GitLab