diff --git a/src/scripts/dispatch-space-n-user.js b/src/scripts/dispatch-space-n-user.js index 2746b2be30ea2749e7b744570869b08a3cbd46f3..223270804e95a1dfe32e40941eb7762c37b1d73a 100644 --- a/src/scripts/dispatch-space-n-user.js +++ b/src/scripts/dispatch-space-n-user.js @@ -3,7 +3,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 + + // Those function will be call the element is connected to the dom async connectedCallback() { document.getElementById("mentor_login").onclick = () => this.triggerLogin(); this.update(); @@ -14,12 +15,27 @@ class CoopStarterLoginComponent extends HTMLElement { loader.removeAttribute('hidden'); await sibAuth.login(); } + + decodeId(id) { + let slashReplace = '~_'; + let colonReplace = '~@'; + let dotReplace = '~!'; + let tildeReplace = '~~'; + if (!id) return ''; + return id.replace(new RegExp(`\\${colonReplace}`, 'g'), ':') + .replace(new RegExp(`\\${slashReplace}`, 'g'), '/') + .replace(new RegExp(`\\${dotReplace}`, 'g'), '.') + .replace(new RegExp(`\\${tildeReplace}`, 'g'), '~') + .replace(/^@/, ''); + } + async update() { let loader = document.getElementById('main-loader'); loader.removeAttribute('hidden'); let user = await sibAuth.getUser(); + console.log(user); //Set a userprefix default status var userPrefix = "public" @@ -27,46 +43,36 @@ class CoopStarterLoginComponent extends HTMLElement { 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); + //If the user is a mentor or an entreprenur, change user prefix - if(await user.mentor_profile) { + if(user && await user.mentor_profile) { + console.log(user); userPrefix = "mentor" - } else if (await user.entrepreneur_profile) { + } else if (user && await user.entrepreneur_profile) { userPrefix = "entrepreneur" } } //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 different, we reload") - var newRoute = new URL(newUrl).pathname.replace(/^\/([a-z]{2})\//, "") - // console.log(newRoute); - console.log('route:', newRoute.match(/^[^@]+/)[0]); - console.log('resource:', newRoute.split(/@(.+)/)[1]); + window.dispatchEvent( new CustomEvent('requestNavigation', { detail: { - route: newRoute.match(/^[^@]+/)[0], - resource: newRoute.split(/@(.+)/)[1] + route: 'entrepreneur-resource-detail', + resource: { "@id": this.decodeId(newRoute.split(/@(.+)/)[1]) } } }) ); - console.log("newRoute", newRoute) - // window.location.href = newUrl - } else { - console.log("the url identique, let it go") loader.toggleAttribute('hidden', true); } } else { @@ -84,6 +90,10 @@ class CoopStarterLoginComponent extends HTMLElement { window.dispatchEvent( new CustomEvent('requestNavigation', { detail: { route: 'account-creation' } }) ); + } else { + window.dispatchEvent( + new CustomEvent('requestNavigation', { detail: { route: 'splash' } }) + ); } } }