From cde24cbf3fbc418af3c98d82e47dcd5df71e5b26 Mon Sep 17 00:00:00 2001 From: Joris Date: Tue, 18 May 2021 12:56:36 +0200 Subject: Improve route type --- src/main.ts | 4 ++-- src/router.ts | 18 ++++++------------ src/view/form.ts | 2 +- src/view/timer.ts | 4 ++-- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/main.ts b/src/main.ts index 436a217..8cc1a91 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,10 +4,10 @@ import * as Timer from 'view/timer' import * as Router from 'router' export function showPage(route: Router.Route) { - if (route.kind === Router.Kind.Form) { + if (route.name === 'form') { document.body.innerHTML = '' document.body.appendChild(Form.view(route.config, showPage)) - } else if (route.kind === Router.Kind.Timer) { + } else if (route.name === 'timer') { document.body.innerHTML = '' document.body.appendChild(Timer.view(route.config, showPage)) } diff --git a/src/router.ts b/src/router.ts index ff6c724..2d229b0 100644 --- a/src/router.ts +++ b/src/router.ts @@ -1,14 +1,8 @@ import * as Config from 'config' -export enum Kind { - Form, - Timer, -} - -export interface Route { - kind: Kind, - config: Config.Config -} +export type Route + = { name: 'form', config: Config.Config } + | { name: 'timer', config: Config.Config } export function from(location: Location): Route { const hash = location.hash.slice(1) @@ -16,7 +10,7 @@ export function from(location: Location): Route { const path = parts[0] const search = parts.length > 1 ? parts[1] : '' - const kind = path.startsWith('/timer') ? Kind.Timer : Kind.Form + const name = path.startsWith('/timer') ? 'timer' : 'form' let config = Config.init() if (search.length > 0) { search.split('&').forEach(entry => { @@ -35,11 +29,11 @@ export function from(location: Location): Route { const params = search.split('&') } - return { kind, config } + return { name, config } } export function toString(route: Route): string { - const path = route.kind === Kind.Form ? '/' : '/timer' + const path = route.name === 'form' ? '/' : '/timer' let query = '' if (route.config !== undefined) { const { warmup, tabatas, prepare, cycles, work, rest } = route.config diff --git a/src/view/form.ts b/src/view/form.ts index 0b04cfb..b0d5827 100644 --- a/src/view/form.ts +++ b/src/view/form.ts @@ -32,7 +32,7 @@ export function view(config: Config.Config, showPage: (route: Router.Route) => v { className: 'g-Form' , onsubmit: (e: Event) => { e.preventDefault() - const timerRoute = { kind: Router.Kind.Timer, config } + const timerRoute: Router.Route = { name: 'timer', config } history.pushState({}, '', Router.toString(timerRoute)) showPage(timerRoute) }}, diff --git a/src/view/timer.ts b/src/view/timer.ts index 061f5d4..07b5db3 100644 --- a/src/view/timer.ts +++ b/src/view/timer.ts @@ -18,7 +18,7 @@ export function clearInterval() { export function view(config: Config.Config, showPage: (route: Router.Route) => void) { - const formUrl = `${Router.toString({ kind: Router.Kind.Form, config })}` + const formUrl = `${Router.toString({ name: 'form', config })}` const duration = Config.getDuration(config) // State @@ -42,7 +42,7 @@ export function view(config: Config.Config, showPage: (route: Router.Route) => v } const quit = () => { - const formRoute = { kind: Router.Kind.Form, config } + const formRoute: Router.Route = { name: 'form', config } history.pushState({}, '', Router.toString(formRoute)) clearInterval() showPage(formRoute) -- cgit v1.2.3