From 7e34f883aa4a8a008577b109c5fc6f3815c77f55 Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 29 Nov 2023 17:04:00 +0100 Subject: [PATCH] fix: click on loggin protected do not change history --- frontend/static/js/index.js | 24 +++++++++---------- .../static/js/views/accounts/LoginView.js | 2 +- .../static/js/views/accounts/RegisterView.js | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index a261a8c..e8e71b0 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -23,12 +23,12 @@ const getParams = match => { })); }; -const navigateTo = url => { - history.pushState(null, null, url); - router(); +const navigateTo = async (uri) => { + if (await router(uri) === 0) + history.pushState(null, null, uri); }; -const router = async () => { +const router = async (uri = "") => { const routes = [ { path: "/", view: Dashboard }, { path: "/posts", view: Posts }, @@ -45,7 +45,7 @@ const router = async () => { const potentialMatches = routes.map(route => { return { route: route, - result: location.pathname.match(pathToRegex(route.path)) + result: uri.match(pathToRegex(route.path)) }; }); @@ -54,18 +54,19 @@ const router = async () => { if (!match) { match = { route: routes[0], - result: [location.pathname] + result: [uri] }; } const view = new match.route.view(getParams(match)); let content = await view.getHtml(); if (content == null) - return; - - document.querySelector("#app").innerHTML = content + return 1; + + document.querySelector("#app").innerHTML = content await view.postInit(); + return 0; }; window.addEventListener("popstate", router); @@ -74,11 +75,10 @@ document.addEventListener("DOMContentLoaded", () => { document.body.addEventListener("click", e => { if (e.target.matches("[data-link]")) { e.preventDefault(); - navigateTo(e.target.href); + navigateTo(e.target.href.slice(location.origin.length)); } }); - - router(); + router(location.pathname); }); export { client, navigateTo } diff --git a/frontend/static/js/views/accounts/LoginView.js b/frontend/static/js/views/accounts/LoginView.js index ff39a0a..f92c5de 100644 --- a/frontend/static/js/views/accounts/LoginView.js +++ b/frontend/static/js/views/accounts/LoginView.js @@ -30,11 +30,11 @@ async function login() export default class extends AbstractView { constructor(params) { super(params); - this.setTitle("Login"); } async postInit() { + this.setTitle("Login"); document.getElementById("button").onclick = login; } diff --git a/frontend/static/js/views/accounts/RegisterView.js b/frontend/static/js/views/accounts/RegisterView.js index a4b8287..d026719 100644 --- a/frontend/static/js/views/accounts/RegisterView.js +++ b/frontend/static/js/views/accounts/RegisterView.js @@ -25,11 +25,11 @@ async function register() export default class extends AbstractView { constructor(params) { super(params); - this.setTitle("register"); } async postInit() { + this.setTitle("register"); document.getElementById("button").onclick = register; }