-
Notifications
You must be signed in to change notification settings - Fork 0
/
Route.elm
68 lines (49 loc) · 1.48 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module Route exposing (Route(..), fromUrl, href, replaceUrl, routeParser, routeToString)
{-| Parse a url to a Route type
see <https://guide.elm-lang.org/webapps/url_parsing.html>
-}
import Browser.Navigation as Nav
import Html exposing (Attribute)
import Html.Attributes as Attr
import Url exposing (Url)
import Url.Parser as Parser exposing ((</>), (<?>))
import Url.Parser.Query as Query
type Route
= Home
| Auth (Maybe String)
| Logout
| Capture
| CaptureTimers Int
routeParser : Parser.Parser (Route -> a) a
routeParser =
Parser.oneOf
[ Parser.map Home Parser.top
, Parser.map Auth (Parser.s "auth" <?> Query.string "jwt")
, Parser.map Logout (Parser.s "logout")
, Parser.map CaptureTimers (Parser.s "capture" </> Parser.int)
, Parser.map Capture (Parser.s "capture")
]
fromUrl : Url -> Maybe Route
fromUrl url =
Parser.parse routeParser url
href : Route -> Attribute msg
href targetRoute =
Attr.href (routeToString targetRoute)
routeToString : Route -> String
routeToString route =
case route of
Home ->
"/"
Auth Nothing ->
"/auth"
Auth (Just jwt) ->
"/auth?jwt=" ++ jwt
Logout ->
"/logout"
Capture ->
"/capture"
CaptureTimers idCapture ->
"/capture/" ++ String.fromInt idCapture
replaceUrl : Nav.Key -> Route -> Cmd msg
replaceUrl key route =
Nav.replaceUrl key (routeToString route)