mirror of
https://github.com/NoRedInk/noredink-ui.git
synced 2024-12-01 09:12:33 +03:00
47 lines
1.0 KiB
Elm
47 lines
1.0 KiB
Elm
module Routes exposing (Route(..), fromLocation)
|
|
|
|
import Browser.Navigation as Navigation
|
|
import ModuleExample exposing (categoryFromString)
|
|
import Parser exposing ((|.), (|=), Parser)
|
|
import Url exposing (Url)
|
|
|
|
|
|
type Route
|
|
= Doodad String
|
|
| Category ModuleExample.Category
|
|
| All
|
|
|
|
|
|
route : Parser Route
|
|
route =
|
|
Parser.oneOf
|
|
[ Parser.succeed Category
|
|
|. Parser.token "category/"
|
|
|= (restOfPath |> Parser.andThen category)
|
|
, Parser.succeed Doodad
|
|
|. Parser.token "doodad/"
|
|
|= restOfPath
|
|
, Parser.succeed All
|
|
]
|
|
|
|
|
|
restOfPath : Parser String
|
|
restOfPath =
|
|
Parser.getChompedString (Parser.chompWhile (always True))
|
|
|
|
|
|
category : String -> Parser ModuleExample.Category
|
|
category string =
|
|
case categoryFromString string of
|
|
Ok c ->
|
|
Parser.succeed c
|
|
|
|
Err e ->
|
|
Parser.problem e
|
|
|
|
|
|
fromLocation : Url -> Route
|
|
fromLocation location =
|
|
Parser.run route (Maybe.withDefault "" location.fragment)
|
|
|> Result.withDefault All
|