From 4dc138ca48c0eb893126623e26488facd16d8a7b Mon Sep 17 00:00:00 2001
From: VakarisZ <vakarisz@yahoo.com>
Date: Thu, 15 Jul 2021 10:08:14 +0300
Subject: [PATCH] Island UI: finish the routing implementation related to
 landing page (if mode not chosen redirect to landing page, etc.)

---
 .../cc/ui/src/components/Main.tsx             | 51 +++++++++++--------
 1 file changed, 31 insertions(+), 20 deletions(-)

diff --git a/monkey/monkey_island/cc/ui/src/components/Main.tsx b/monkey/monkey_island/cc/ui/src/components/Main.tsx
index 0727b3e70..3d41c8b54 100644
--- a/monkey/monkey_island/cc/ui/src/components/Main.tsx
+++ b/monkey/monkey_island/cc/ui/src/components/Main.tsx
@@ -79,25 +79,26 @@ class AppComponent extends AuthComponent {
         if (res) {
           this.checkMode()
             .then(() => {
-              if(this.state.islandMode === null) {
-                return
-              }
-              this.authFetch('/api')
-                .then(res => res.json())
-                .then(res => {
-                  // This check is used to prevent unnecessary re-rendering
-                  let isChanged = false;
-                  for (let step in this.state.completedSteps) {
-                    if (this.state.completedSteps[step] !== res['completed_steps'][step]) {
-                      isChanged = true;
-                      break;
+                if (this.state.islandMode === null) {
+                  return
+                }
+                this.authFetch('/api')
+                  .then(res => res.json())
+                  .then(res => {
+                    // This check is used to prevent unnecessary re-rendering
+                    let isChanged = false;
+                    for (let step in this.state.completedSteps) {
+                      if (this.state.completedSteps[step] !== res['completed_steps'][step]) {
+                        isChanged = true;
+                        break;
+                      }
                     }
-                  }
-                  if (isChanged) {
-                    this.setState({completedSteps: res['completed_steps']});
-                    this.showInfectionDoneNotification();
-                  }
-                });}
+                    if (isChanged) {
+                      this.setState({completedSteps: res['completed_steps']});
+                      this.showInfectionDoneNotification();
+                    }
+                  });
+              }
             )
 
         }
@@ -115,9 +116,9 @@ class AppComponent extends AuthComponent {
     let render_func = () => {
       switch (this.state.isLoggedIn) {
         case true:
-          if (this.state.islandMode === null && route_path !== Routes.LandingPage) {
+          if (this.needsRedirectionToLandingPage(route_path)) {
             return <Redirect to={{pathname: Routes.LandingPage}}/>
-          } else if(route_path === Routes.LandingPage && this.state.islandMode !== null){
+          } else if (this.needsRedirectionToGettingStarted(route_path)) {
             return <Redirect to={{pathname: Routes.GettingStartedPage}}/>
           }
           return page_component;
@@ -142,6 +143,16 @@ class AppComponent extends AuthComponent {
     }
   };
 
+  needsRedirectionToLandingPage = (route_path) => {
+    return (this.state.islandMode === null && route_path !== Routes.LandingPage)
+  }
+
+  needsRedirectionToGettingStarted = (route_path) => {
+    return route_path === Routes.LandingPage &&
+      this.state.islandMode !== null &&
+      this.state.islandMode !== undefined
+  }
+
   redirectTo = (userPath, targetPath) => {
     let pathQuery = new RegExp(userPath + '[/]?$', 'g');
     if (window.location.pathname.match(pathQuery)) {