noredink-ui/styleguide-app/Routes.elm

47 lines
1.0 KiB
Elm
Raw Normal View History

2018-02-13 00:32:38 +03:00
module Routes exposing (Route(..), fromLocation)
import Browser.Navigation as Navigation
2018-02-13 00:32:38 +03:00
import ModuleExample exposing (categoryFromString)
2018-12-05 21:56:04 +03:00
import Parser exposing ((|.), (|=), Parser)
import Url exposing (Url)
2018-02-13 00:32:38 +03:00
type Route
= Doodad String
| Category ModuleExample.Category
| All
2018-12-05 21:56:04 +03:00
route : Parser Route
2018-02-13 00:32:38 +03:00
route =
2018-12-05 21:56:04 +03:00
Parser.oneOf
[ Parser.succeed Category
|. Parser.token "category/"
2018-12-08 01:16:00 +03:00
|= (restOfPath |> Parser.andThen category)
2018-12-05 21:56:04 +03:00
, Parser.succeed Doodad
2018-12-08 01:16:00 +03:00
|. Parser.token "doodad/"
|= restOfPath
2018-12-05 21:56:04 +03:00
, Parser.succeed All
2018-02-13 00:32:38 +03:00
]
2018-12-08 01:16:00 +03:00
restOfPath : Parser String
restOfPath =
Parser.getChompedString (Parser.chompWhile (always True))
2018-12-05 21:56:04 +03:00
category : String -> Parser ModuleExample.Category
category string =
case categoryFromString string of
Ok c ->
Parser.succeed c
Err e ->
Parser.problem e
2018-02-13 00:32:38 +03:00
fromLocation : Url -> Route
2018-02-13 00:32:38 +03:00
fromLocation location =
2018-12-05 21:56:04 +03:00
Parser.run route (Maybe.withDefault "" location.fragment)
|> Result.withDefault All