fix: click on loggin protected do not change

history
This commit is contained in:
starnakin 2023-11-29 17:04:00 +01:00
parent 07d06253ba
commit 7e34f883aa
3 changed files with 14 additions and 14 deletions

View File

@ -23,12 +23,12 @@ const getParams = match => {
})); }));
}; };
const navigateTo = url => { const navigateTo = async (uri) => {
history.pushState(null, null, url); if (await router(uri) === 0)
router(); history.pushState(null, null, uri);
}; };
const router = async () => { const router = async (uri = "") => {
const routes = [ const routes = [
{ path: "/", view: Dashboard }, { path: "/", view: Dashboard },
{ path: "/posts", view: Posts }, { path: "/posts", view: Posts },
@ -45,7 +45,7 @@ const router = async () => {
const potentialMatches = routes.map(route => { const potentialMatches = routes.map(route => {
return { return {
route: route, route: route,
result: location.pathname.match(pathToRegex(route.path)) result: uri.match(pathToRegex(route.path))
}; };
}); });
@ -54,18 +54,19 @@ const router = async () => {
if (!match) { if (!match) {
match = { match = {
route: routes[0], route: routes[0],
result: [location.pathname] result: [uri]
}; };
} }
const view = new match.route.view(getParams(match)); const view = new match.route.view(getParams(match));
let content = await view.getHtml(); let content = await view.getHtml();
if (content == null) if (content == null)
return; return 1;
document.querySelector("#app").innerHTML = content document.querySelector("#app").innerHTML = content
await view.postInit(); await view.postInit();
return 0;
}; };
window.addEventListener("popstate", router); window.addEventListener("popstate", router);
@ -74,11 +75,10 @@ document.addEventListener("DOMContentLoaded", () => {
document.body.addEventListener("click", e => { document.body.addEventListener("click", e => {
if (e.target.matches("[data-link]")) { if (e.target.matches("[data-link]")) {
e.preventDefault(); e.preventDefault();
navigateTo(e.target.href); navigateTo(e.target.href.slice(location.origin.length));
} }
}); });
router(location.pathname);
router();
}); });
export { client, navigateTo } export { client, navigateTo }

View File

@ -30,11 +30,11 @@ async function login()
export default class extends AbstractView { export default class extends AbstractView {
constructor(params) { constructor(params) {
super(params); super(params);
this.setTitle("Login");
} }
async postInit() async postInit()
{ {
this.setTitle("Login");
document.getElementById("button").onclick = login; document.getElementById("button").onclick = login;
} }

View File

@ -25,11 +25,11 @@ async function register()
export default class extends AbstractView { export default class extends AbstractView {
constructor(params) { constructor(params) {
super(params); super(params);
this.setTitle("register");
} }
async postInit() async postInit()
{ {
this.setTitle("register");
document.getElementById("button").onclick = register; document.getElementById("button").onclick = register;
} }