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 => {
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;
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 }

View File

@ -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;
}

View File

@ -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;
}