-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRoute.elm
50 lines (36 loc) · 992 Bytes
/
Route.elm
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
module Route exposing (Route(..), parser, pushRoute, routeLink)
import Browser.Navigation as Nav
import Html as H
import Html.Attributes as HA
import Url.Builder exposing (absolute)
import Url.Parser exposing ((</>), Parser, int, map, oneOf, s, string, top)
type Route
= Login
| Register
| Lobby
| Game Int
routeLink : Route -> List (H.Html a) -> H.Html a
routeLink r =
H.a [ HA.href (toString r) ]
parser : Parser (Route -> a) a
parser =
oneOf
[ map Lobby top
, map Register (s "register")
, map Login (s "login")
, map Game (s "game" </> int)
]
toString : Route -> String
toString r =
case r of
Login ->
absolute [ "login" ] []
Register ->
absolute [ "register" ] []
Lobby ->
absolute [] []
Game gId ->
absolute [ "game", String.fromInt gId ] []
pushRoute : Nav.Key -> Route -> Cmd a
pushRoute k =
toString >> Nav.pushUrl k