From 802c6a2180d1079f6893e69b27b482154c548f17 Mon Sep 17 00:00:00 2001 From: sairaj mote Date: Tue, 15 Aug 2023 04:25:41 +0530 Subject: [PATCH] bug fix --- main_UI.js | 66 +++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/main_UI.js b/main_UI.js index 92cd018..915d8e9 100644 --- a/main_UI.js +++ b/main_UI.js @@ -350,42 +350,48 @@ class Router { this.state = state this.routingStart = routingStart this.routingEnd = routingEnd + this.lastPage = null window.addEventListener('hashchange', e => this.routeTo(window.location.hash)) } addRoute(route, callback) { this.routes[route] = callback } - set state(state) { - this._state = state - } async routeTo(path) { - let page - let wildcards = [] - let queryString - let params - [path, queryString] = path.split('?'); - if (path.includes('#')) - path = path.split('#')[1]; - if (path.includes('/')) - [, page, ...wildcards] = path.split('/') - else - page = path - this.state = { page, wildcards } - if (queryString) { - params = new URLSearchParams(queryString) - this.state.params = Object.fromEntries(params) - } - if (this.routingStart) { - this.routingStart(this.state) - } - if (this.routes[page]) { - await this.routes[page](this.state) - this.state.lastPage = page - } else { - this.routes['404'](this.state) - } - if (this.routingEnd) { - this.routingEnd(this.state) + try { + let page + let wildcards = [] + let queryString + let params + [path, queryString] = path.split('?'); + if (path.includes('#')) + path = path.split('#')[1]; + if (path.includes('/')) + [, page, ...wildcards] = path.split('/') + else + page = path + this.state = { page, wildcards, lastPage: this.lastPage } + if (queryString) { + params = new URLSearchParams(queryString) + this.state.params = Object.fromEntries(params) + } + if (this.routingStart) { + this.routingStart(this.state) + } + if (this.routes[page]) { + await this.routes[page](this.state) + this.lastPage = page + } else { + if (this.routes['404']) { + this.routes['404'](this.state); + } else { + console.error(`No route found for '${page}' and no '404' route is defined.`); + } + } + if (this.routingEnd) { + this.routingEnd(this.state) + } + } catch (e) { + console.error(e) } } }