forked from gcanti/elm-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavigation.tsx
73 lines (56 loc) · 1.68 KB
/
Navigation.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import * as React from 'react'
import { cmd } from '../src'
import { Location, push } from '../src/Navigation'
import { Html } from '../src/React'
// --- Routes
const routes = {
RouteA: true,
RouteB: true
}
type Route = keyof typeof routes
// --- Flags
export type Flags = Model
const defaultRoute: Route = 'RouteA'
export const flags: Flags = defaultRoute
// --- Model
export type Model = Route
function isRoute(route: string): route is Route {
return routes.hasOwnProperty(route)
}
function getRoute(location: Location): Route {
const route = location.pathname.substring(1)
return isRoute(route) ? route : defaultRoute
}
export function locationToMsg(location: Location): Msg {
return { type: getRoute(location) } as Msg
}
export function init(_: Flags): (location: Location) => [Model, cmd.Cmd<Msg>] {
return location => [getRoute(location), cmd.none]
}
// --- Messages
export type Msg = { type: 'RouteA' } | { type: 'RouteB' } | { type: 'Push'; url: Route }
// --- Update
export function update(msg: Msg, model: Model): [Model, cmd.Cmd<Msg>] {
switch (msg.type) {
case 'RouteA':
return ['RouteA', cmd.none]
case 'RouteB':
return ['RouteB', cmd.none]
case 'Push':
return [model, push(msg.url)]
}
}
// --- View
export function view(model: Model): Html<Msg> {
return dispatch => <div>{model === 'RouteA' ? RouteA(dispatch) : RouteB(dispatch)}</div>
}
const RouteA: Html<Msg> = dispatch => (
<div>
RouteA <button onClick={() => dispatch({ type: 'Push', url: 'RouteB' })}>RouteB</button>
</div>
)
const RouteB: Html<Msg> = dispatch => (
<div>
RouteB <button onClick={() => dispatch({ type: 'Push', url: 'RouteA' })}>RouteA</button>
</div>
)