import state from "./state.js"; import Component from "./Component.js"; import LoginPage from "./Page/LoginPage.js"; import HomePage from "./Page/HomePage.js"; import { html, toast, hesc } from "./util.js"; export default class MainApplication extends Component { constructor() { super(); $("title").text('Application'); this.$area.html(html`
Application
Sign up
`); } mountInto($parent: JQuery): void { super.mountInto($parent); this.routeToHash(); window.addEventListener('hashchange', (ev) => this.routeToHash(), false); } routeToHash() { switch (window.location.hash.substr(1)) { case "/": { // Redirect to "home page" based on our current state if (state.isLoggedIn()) { window.location.hash = `/home` } else { window.location.hash = `/login` } } break; case "/home": { if (!state.isLoggedIn()) { toast(hesc("Unauthorised"), hesc("Please log in."), "danger"); window.location.hash = `/`; return; } let p = new HomePage(); this.renderPage(p); } break; case "/login": { if (state.isLoggedIn()) { window.location.hash = `/`; return; } let p = new LoginPage(); this.renderPage(p); } break; default: { // Redirect to / window.location.hash = `/` } break; } } renderPage(p: Component): void { p.mountInto(this.$area.find(".component-page")); } }