Merge branch 'feature/advanced-routing'

This commit is contained in:
Fran Jurmanović
2021-05-29 21:37:56 +02:00
4 changed files with 18 additions and 8 deletions

View File

@@ -25,11 +25,11 @@ class AppMainElement extends HTMLElement {
path: "/", path: "/",
component: "home-page", component: "home-page",
layout: "menu-layout", layout: "menu-layout",
middleware: this.middleAuth,
}, },
{ {
path: "/home", path: "/home",
component: "home-page", component: "home-page",
middleware: this.isAuth,
}, },
{ {
path: "/rb", path: "/rb",
@@ -47,12 +47,16 @@ class AppMainElement extends HTMLElement {
this.routerService.init(); this.routerService.init();
} }
isAuth = () => { middleAuth = () => {
if (this.authStore?.token == null) { if (!this.isAuth) {
this.routerService.goTo("/unauthorized"); this.routerService.goTo("/unauthorized");
return true; return true;
} }
}; };
get isAuth(): boolean {
return this.authStore && this.authStore.token;
}
} }
export type { AppMainElement }; export type { AppMainElement };

View File

@@ -40,10 +40,8 @@ class RouterService {
} }
let changed: boolean = false; let changed: boolean = false;
if (_mainRoot?.childNodes.length > 0) { if (_mainRoot?.childNodes.length > 0) {
console.log(_mainRoot.childNodes);
_mainRoot?.childNodes?.forEach?.( _mainRoot?.childNodes?.forEach?.(
(child: BaseLayoutElement) => { (child: BaseLayoutElement) => {
console.log("Eh");
if ( if (
route.layout && route.layout &&
route.layout.toUpperCase() === child.tagName && route.layout.toUpperCase() === child.tagName &&
@@ -101,9 +99,10 @@ class RouterService {
_mainRoot.innerHTML = "404 - Not found"; _mainRoot.innerHTML = "404 - Not found";
} }
@update
goTo(path: string) { goTo(path: string) {
if (!Array.isArray(this.historyStack)) this.historyStack = []; if (!Array.isArray(this.historyStack)) this.historyStack = [];
const currentPath = window.location.pathname;
if (path == currentPath) return;
const _index = this._routes.findIndex((route) => route.path === path); const _index = this._routes.findIndex((route) => route.path === path);
if (_index >= 0) { if (_index >= 0) {
const newRoute = this._routes[_index]; const newRoute = this._routes[_index];
@@ -111,6 +110,7 @@ class RouterService {
const url = new URL(window.location.toString()); const url = new URL(window.location.toString());
url.pathname = path; url.pathname = path;
window.history.pushState({}, "", url.toString()); window.history.pushState({}, "", url.toString());
this.update();
} }
} }
@@ -128,7 +128,11 @@ class RouterService {
} }
@update @update
init() {} init() {
window.addEventListener("popstate", () => {
this.update();
});
}
} }
class RouteState { class RouteState {

View File

@@ -11,6 +11,8 @@ class AuthStore {
} }
get token() { get token() {
if (this._token == "null") return null;
if (this._token == "undefined") return undefined;
return this._token; return this._token;
} }

View File

@@ -14,7 +14,7 @@ class HomePageElement extends HTMLElement {
@update @update
connectedCallback() { connectedCallback() {
this.pingService = new PingService(this.appMain?.appService); this.pingService = new PingService(this.appMain?.appService);
this.getPong(); if (this.appMain.isAuth) this.getPong();
} }
getPong = async () => { getPong = async () => {