From 7e82a4adf8a4c6deca10f02eb4bf419d1cd8f2da Mon Sep 17 00:00:00 2001
From: Benoit Alessandroni <benoit@happy-dev.fr>
Date: Thu, 2 Apr 2020 14:04:55 +0200
Subject: [PATCH] Fix user from store retrieval

---
 src/includes/entrepreneur/dashboard.pug |  3 ---
 src/includes/head.pug                   |  1 +
 src/index.pug                           | 16 +++++------
 src/scripts/dispatch-space-n-user.js    | 36 ++++++++++++++++++-------
 4 files changed, 35 insertions(+), 21 deletions(-)

diff --git a/src/includes/entrepreneur/dashboard.pug b/src/includes/entrepreneur/dashboard.pug
index b79263c4..6d50f78e 100644
--- a/src/includes/entrepreneur/dashboard.pug
+++ b/src/includes/entrepreneur/dashboard.pug
@@ -13,9 +13,6 @@ section#home
     // and grouped by steps
     // From two different endpoints I guess
     //-Work on the page architecture
-
-
-    //TODO : investigate why we have to remove hidden attribute to see this even if we are on the right route.
     #entrepreneur-resource-list(hidden).no-sidebar.container
         include ./resources/list.pug
 
diff --git a/src/includes/head.pug b/src/includes/head.pug
index 34fcd45b..9643a940 100644
--- a/src/includes/head.pug
+++ b/src/includes/head.pug
@@ -2,6 +2,7 @@ head
     title CoopStarter
     meta(charset="utf-8")
     script(src="https://unpkg.com/@webcomponents/webcomponentsjs@1.2.7/webcomponents-loader.js")
+    //- script(type="module" src="/scripts/sib-core/dist/index.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.4")
diff --git a/src/index.pug b/src/index.pug
index 93d9e71f..4a6d8b94 100644
--- a/src/index.pug
+++ b/src/index.pug
@@ -29,13 +29,13 @@ html
 
         #main-loader(hidden style="display: none") Loading&#8230;
 
-sib-auth
-    sib-auth-provider(
-        class="sib-auth-provider"
-        data-authority=`${sdn}`
-        data-id="coopstarter"
-    )
-
-script(type='module' src='/scripts/dispatch-space-n-user.js')
+        sib-auth
+            sib-auth-provider(
+                class="sib-auth-provider"
+                data-authority=`${sdn}`
+                data-id="coopstarter"
+            )
+
+        script(type='module' src='/scripts/dispatch-space-n-user.js')
 
 
diff --git a/src/scripts/dispatch-space-n-user.js b/src/scripts/dispatch-space-n-user.js
index 22327080..a438122f 100644
--- a/src/scripts/dispatch-space-n-user.js
+++ b/src/scripts/dispatch-space-n-user.js
@@ -1,6 +1,23 @@
-import { store, base_context } from 'https://unpkg.com/@startinblox/core@0.9/dist/libs/store/store.js';
+import { store } from 'https://unpkg.com/@startinblox/core@0.9';
 const sibAuth = document.querySelector('sib-auth');
 
+const context = {
+  '@vocab': 'http://happy-dev.fr/owl/#',
+  rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
+  rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
+  ldp: 'http://www.w3.org/ns/ldp#',
+  foaf: 'http://xmlns.com/foaf/0.1/',
+  name: 'rdfs:label',
+  acl: 'http://www.w3.org/ns/auth/acl#',
+  permissions: 'acl:accessControl',
+  mode: 'acl:mode',
+  geo: "http://www.w3.org/2003/01/geo/wgs84_pos#",
+  lat: "geo:lat",
+  lng: "geo:long",
+  'mentor_profile': 'http://happy-dev.fr/owl/#mentor_profile',
+  'entrepreneur_profile': 'http://happy-dev.fr/owl/#entrepreneur_profile',
+};
+
 //We create a custom element
 class CoopStarterLoginComponent extends HTMLElement {
 
@@ -35,24 +52,23 @@ class CoopStarterLoginComponent extends HTMLElement {
     loader.removeAttribute('hidden');
     let user = await sibAuth.getUser();
 
-    console.log(user);
     //Set a userprefix default status
     var userPrefix = "public"
     
     // Idenfify the space we should go
     if (user) {
-      await store.initGraph(user['@id'], base_context);
-      user = await store.get(user['@id']);
-      // if (!user)
-      // user = await store.get(user);
-      console.log(user);
+      await store.initGraph(user['@id'], context);
+      let userFromStore = await store.get(user['@id']);
       
       //If the user is a mentor or an entreprenur, change user prefix
-      if(user && await user.mentor_profile) {
-          console.log(user);
+      if(user) {
+        let is_mentor = await userFromStore.mentor_profile;
+        let is_entrepreneur = await userFromStore.entrepreneur_profile;
+        if (is_mentor) {
           userPrefix = "mentor"
-      } else if (user && await user.entrepreneur_profile) {
+        } else if(is_entrepreneur) {
           userPrefix = "entrepreneur"
+        }
       }
     }
 
-- 
GitLab