From ae50da6f3d1b70111940e0df14f0b7780d596195 Mon Sep 17 00:00:00 2001
From: Alice <alice.poggioli@hotmail.fr>
Date: Wed, 1 Apr 2020 15:53:54 +0200
Subject: [PATCH] pour balessan

---
 src/includes/footer.pug              |  46 +++++-----
 src/includes/head.pug                |   2 +-
 src/includes/mentor/dashboard.pug    |  10 +--
 src/index.pug                        |  62 +------------
 src/scripts/coopstarter.js           |   4 +-
 src/scripts/dispatch-space-n-user.js | 126 +++++++++++++++++++++++++++
 6 files changed, 159 insertions(+), 91 deletions(-)
 create mode 100644 src/scripts/dispatch-space-n-user.js

diff --git a/src/includes/footer.pug b/src/includes/footer.pug
index c44d6711..e0e3d8ba 100644
--- a/src/includes/footer.pug
+++ b/src/includes/footer.pug
@@ -1,23 +1,23 @@
-.container
-    .flex.flex_espace.flex_item_center
-        a.logo_footer(href='https://coopseurope.coop/')
-            figure.margin_bord_ecran
-                img(src="../images/logo_CE.png" alt="Coopstarter")
-        a.logo_footer(href='mentor-resource-list')
-            figure.margin_bord_ecran
-                img(src="../images/logo_coops4dev.jpg" alt="Coopstarter")
-        a.logo_footer(href='mentor-resource-list')
-            figure.flex.flex_item_center
-                img.textMore(src="../images/ica_logo.png" alt="Coopstarter")
-                div
-                    p International
-                    p Co-operative
-                    p Alliance
-        a.logo_footer(href='mentor-resource-list')
-            figure.flex.flex_item_center
-                img.textMore(src="../images/logo_UE.jpg" alt="Coopstarter")
-                div
-                    p Co-funded 
-                    p by the 
-                    p European Union
-    p.footer_warning.flex_item_center  This software has been co-funded by the European Union. The contents of this software are the sole responsibility of Cooperatives Europe and can in no way be taken to reflect the views of the European Union.
\ No newline at end of file
+//- .container
+//-     .flex.flex_espace.flex_item_center
+//-         a.logo_footer(href='https://coopseurope.coop/')
+//-             figure.margin_bord_ecran
+//-                 img(src="../images/logo_CE.png" alt="Coopstarter")
+//-         a.logo_footer(href='mentor-resource-list')
+//-             figure.margin_bord_ecran
+//-                 img(src="../images/logo_coops4dev.jpg" alt="Coopstarter")
+//-         a.logo_footer(href='mentor-resource-list')
+//-             figure.flex.flex_item_center
+//-                 img.textMore(src="../images/ica_logo.png" alt="Coopstarter")
+//-                 div
+//-                     p International
+//-                     p Co-operative
+//-                     p Alliance
+//-         a.logo_footer(href='mentor-resource-list')
+//-             figure.flex.flex_item_center
+//-                 img.textMore(src="../images/logo_UE.jpg" alt="Coopstarter")
+//-                 div
+//-                     p Co-funded 
+//-                     p by the 
+//-                     p European Union
+//-     p.footer_warning.flex_item_center  This software has been co-funded by the European Union. The contents of this software are the sole responsibility of Cooperatives Europe and can in no way be taken to reflect the views of the European Union.
\ No newline at end of file
diff --git a/src/includes/head.pug b/src/includes/head.pug
index d541cdd3..34fcd45b 100644
--- a/src/includes/head.pug
+++ b/src/includes/head.pug
@@ -4,7 +4,7 @@ head
     script(src="https://unpkg.com/@webcomponents/webcomponentsjs@1.2.7/webcomponents-loader.js")
     script(type="module" src="https://unpkg.com/@startinblox/core@0.9")
     script(type="module" src="https://unpkg.com/@startinblox/router@0.7")
-    script(type="module" src="https://unpkg.com/@startinblox/oidc@0.8")
+    script(type="module" src="https://unpkg.com/@startinblox/oidc@0.8.4")
     script(type="module" src="https://unpkg.com/@startinblox/component-conversation@0.4")
     script(src="https://kit.fontawesome.com/48014d2af3.js")
     script(src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous")
diff --git a/src/includes/mentor/dashboard.pug b/src/includes/mentor/dashboard.pug
index 324fd8ab..64fd74b8 100644
--- a/src/includes/mentor/dashboard.pug
+++ b/src/includes/mentor/dashboard.pug
@@ -15,11 +15,11 @@ section#home
     #mentor-resource-list(hidden).no-sidebar.container
         include resources/list.pug
 
-        sib-router
-            sib-ac-checker(permission="acl:Write" bind-resources)
-                sib-route(id-prefix=`${endpoints.resources}`, name='mentor-resource-validate', use-id)
-                sib-route(name='resource-validation-process')
-                sib-route(id-prefix=`${endpoints.resources}`, name='mentor-resource-detail', use-id)
+    sib-router
+        sib-ac-checker(permission="acl:Write" bind-resources)
+            sib-route(id-prefix=`${endpoints.resources}`, name='mentor-resource-validate', use-id)
+            sib-route(name='resource-validation-process')
+            sib-route(id-prefix=`${endpoints.resources}`, name='mentor-resource-detail', use-id)
                 
         dialog#mentor-resource-validate.no-sidebar
             include resources/validate.pug  
diff --git a/src/index.pug b/src/index.pug
index ad5d7fe2..93d9e71f 100644
--- a/src/index.pug
+++ b/src/index.pug
@@ -27,7 +27,7 @@ html
         #public-dashboard(hidden).no-sidebar
             include includes/public/dashboard.pug
 
-        #main-loader(hidden) Loading&#8230;
+        #main-loader(hidden style="display: none") Loading&#8230;
 
 sib-auth
     sib-auth-provider(
@@ -36,64 +36,6 @@ sib-auth
         data-id="coopstarter"
     )
 
-script(type='module').
-      import { store } from 'https://unpkg.com/@startinblox/core@0.8';
-      const sibAuth = document.querySelector('sib-auth');
-      
-      class CoopStarterLoginComponent extends HTMLElement {
-        async connectedCallback() {
-          mentor_login.onclick = () => this.triggerLogin();
-          this.update();
-        }
+script(type='module' src='/scripts/dispatch-space-n-user.js')
 
-        async triggerLogin() {
-          let loader = document.getElementById('main-loader');
-          loader.removeAttribute('hidden');
-          await sibAuth.login();
-        }
-        
-        async update() {
-          let loader = document.getElementById('main-loader');
-          loader.removeAttribute('hidden');
-          let user = await sibAuth.getUser();
-          let idToken = await sibAuth.getUserIdToken();
-          console.log(window.location);
-          
-          if ( !user && window.location.href.indexOf('public-dashboard/public-resource-detail') === -1) {
-            console.log("We have no user and not in public")
-            loader.setAttribute("hidden", true);
-            window.dispatchEvent(
-              new CustomEvent('requestNavigation', { detail: { route: 'splash' } })
-            );
-          }
-
-          if(!user && window.location.href.indexOf('public-dashboard/public-resource-detail') > -1) {
-            console.log("We are targetting the public space !")
-            //- window.dispatchEvent(
-            //-   new CustomEvent('requestNavigation', { detail: { 
-            //-     route: 'public-resource-detail'
-            //-   } })
-            //- );
-            loader.setAttribute("hidden", true);
-          }
-          user = await store.get(user);
-
-          if (user && user.mentor_profile) {
-            window.dispatchEvent(
-              new CustomEvent('requestNavigation', { detail: { route: 'mentor-dashboard' } })
-            );
-          } else if (user && user.entrepreneur_profile) {
-            window.dispatchEvent(
-              new CustomEvent('requestNavigation', { detail: { route: 'entrepreneur-dashboard' } })
-            );
-          } else {
-            window.dispatchEvent(
-              new CustomEvent('requestNavigation', { detail: { route: 'account-creation' } })
-            );
-          }
-        }
-        empty(){}
-        populate(){}
-      }
-      customElements.define('cs-login', CoopStarterLoginComponent);
 
diff --git a/src/scripts/coopstarter.js b/src/scripts/coopstarter.js
index 9421d399..72410ef7 100644
--- a/src/scripts/coopstarter.js
+++ b/src/scripts/coopstarter.js
@@ -585,8 +585,8 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
 jQuery(document).ready(function($) {
   window.addEventListener('requestNavigation', function(e) {
     setTimeout(function() {
-      let loader = document.getElementById('main-loader');
-      loader.setAttribute('hidden', true);
+      // let loader = document.getElementById('main-loader');
+      // loader.setAttribute('hidden', true);
     }, 2000);
   });
 
diff --git a/src/scripts/dispatch-space-n-user.js b/src/scripts/dispatch-space-n-user.js
new file mode 100644
index 00000000..571c470e
--- /dev/null
+++ b/src/scripts/dispatch-space-n-user.js
@@ -0,0 +1,126 @@
+import { store } from 'https://unpkg.com/@startinblox/core@0.8';
+const sibAuth = document.querySelector('sib-auth');
+
+//We create a custom element
+class CoopStarterLoginComponent extends HTMLElement {
+// Those function will be call the element is connected to the dom
+  async connectedCallback() {
+    document.getElementById("mentor_login").onclick = () => this.triggerLogin();
+    this.update();
+  }
+
+  async triggerLogin() {
+    let loader = document.getElementById('main-loader');
+    loader.removeAttribute('hidden');
+    await sibAuth.login();
+  }
+  
+  async update() {
+    let loader = document.getElementById('main-loader');
+    loader.removeAttribute('hidden');
+    let user = await sibAuth.getUser();
+    // let idToken = await sibAuth.getUserIdToken();
+    // console.log(window.location);
+    
+    // //Not connected and not sharing public resource
+    // // console.log( user, sibAuth, window.location.href.indexOf('public-dashboard/public-resource-detail') === -1) 
+    // if ( !user && window.location.href.indexOf('public-dashboard/public-resource-detail') === -1) {
+    //   console.log("We have no user and not in public")
+    //   loader.setAttribute("hidden", true);
+    //   window.dispatchEvent(
+    //     new CustomEvent('requestNavigation', { detail: { route: 'splash' } })
+    //   );
+    // }
+
+    // //Not connected but sharing public resource
+    // if(window.location.href.indexOf('public-dashboard/public-resource-detail') > -1) {
+    //   console.log("We are targetting the public space !")
+    //   //- window.dispatchEvent(
+    //   //-   new CustomEvent('requestNavigation', { detail: { 
+    //   //-     route: 'public-resource-detail'
+    //   //-   } })
+    //   //- );
+    //   loader.setAttribute("hidden", true);
+    //   return
+    // }
+
+
+    // Idenfify the space we should go
+    user = await store.get(user);
+    console.log("the curent user is ", user)
+
+    //Set a userprefix default status
+    var userPrefix = "public"
+
+    //If the user is a mentor or an entreprenur, change user prefix
+    if(user && user.mentor_profile) {
+        userPrefix = "mentor"
+        console.log("he is a ",userPrefix )
+    } else if (user && user.entrepreneur_profile) {
+        userPrefix = "entrepreneur"
+        console.log("he is a ",userPrefix )
+    }
+
+    //If the url asked pointed to a resource
+    if(window.location.href.indexOf('resource-detail') > -1) {
+      console.log(" the link contains ressource detail")
+      console.log("the current link is ", window.location.href)
+      //We adapt the url with its status
+      console.log("userPrefix", userPrefix)
+      var newUrl = window.location.href.replace(/\/([a-z]+)-dashboard\/([a-z]+)-resource-detail/, `/${userPrefix}-dashboard/${userPrefix}-resource-detail`)
+     // http://localhost:9000/fr/mentor-dashboard/@https~@~_~_api-staging~!coopstarter~!happy-dev~!fr~_resources~_7~_/mentor-resource-detail/@https~@~_~_api-staging~!coopstarter~!happy-dev~!fr~_resources~_7~_
+      console.log("After treatement, the link is ", newUrl)
+
+      //If the url is different after the treatment, we reload with the good url
+      if(newUrl !== window.location.href){
+        console.log("the url are ifférent, we relaod")
+        
+        var newRoute = new URL(newUrl).pathname.replace(/^\/([a-z]{2})\//, "")
+        window.dispatchEvent(
+          new CustomEvent('requestNavigation', { detail: { route: newRoute } })
+        );
+        console.log("newRoute", newRoute)
+
+        // window.location.href = newUrl
+
+      }
+      else{
+        console.log("the url identique, let it go")
+        loader.toggleAttribute('hidden', true);
+      }
+      
+
+    }else {
+      console.log("the current link doesn't concern a detail resource")
+      if (user && user.mentor_profile) {
+        //si y a un lien public alors on traite la chaine
+        
+        window.dispatchEvent(
+            new CustomEvent('requestNavigation', { detail: { route: 'mentor-dashboard' } })
+          );
+
+      //User is a entrepreneur
+      } else if (user && user.entrepreneur_profile) {
+        window.dispatchEvent(
+          new CustomEvent('requestNavigation', { detail: { route: 'entrepreneur-dashboard' } })
+        );
+      } else {
+        window.dispatchEvent(
+          new CustomEvent('requestNavigation', { detail: { route: 'account-creation' } })
+        );
+      }
+    //user-dashboard
+    }
+
+    // //???
+    // user = await store.get(user);
+    // console.log(user)
+
+    //User is a mentor
+    
+  }
+  empty(){}
+  populate(){}
+}
+// We can use <cs-login/>
+customElements.define('cs-login', CoopStarterLoginComponent);
\ No newline at end of file
-- 
GitLab