noredink-ui/styleguide-app/Routes.elm

177 lines
4.8 KiB
Elm
Raw Normal View History

2022-05-23 19:58:21 +03:00
module Routes exposing (Route(..), fromLocation, headerId, toString, updateExample, viewBreadCrumbs)
2018-02-13 00:32:38 +03:00
2022-05-20 22:20:12 +03:00
import Accessibility.Styled as Html exposing (Html)
import Category
import Dict exposing (Dict)
import Example exposing (Example)
2022-05-20 22:20:12 +03:00
import Html.Styled.Attributes as Attributes
2022-05-20 23:47:40 +03:00
import Nri.Ui.BreadCrumbs.V1 as BreadCrumbs exposing (BreadCrumb, BreadCrumbs)
import Nri.Ui.Util exposing (dashify)
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 state msg
= Doodad (Example state msg)
| Category Category.Category
2022-05-20 23:47:40 +03:00
| CategoryDoodad Category.Category (Example state msg)
2018-02-13 00:32:38 +03:00
| All
| NotFound String
2018-02-13 00:32:38 +03:00
toString : Route state msg -> String
toString route_ =
case route_ of
Doodad example ->
"#/doodad/" ++ example.name
Category c ->
2022-05-20 23:47:40 +03:00
"#/category/" ++ Category.forRoute c
CategoryDoodad c example ->
"#/category_doodad/" ++ Category.forRoute c ++ "/" ++ example.name
All ->
"#/"
2022-05-20 23:47:40 +03:00
NotFound unmatchedRoute ->
unmatchedRoute
route : Dict String (Example state msg) -> Parser (Route state msg)
route examples =
let
2022-05-20 23:47:40 +03:00
findExample : (Example state msg -> Route state msg) -> String -> Route state msg
findExample toRoute name =
Dict.get name examples
2022-05-20 23:47:40 +03:00
|> Maybe.map toRoute
|> Maybe.withDefault (NotFound name)
in
2018-12-05 21:56:04 +03:00
Parser.oneOf
2022-05-20 23:47:40 +03:00
[ Parser.succeed (\cat -> findExample (CategoryDoodad cat))
|. Parser.token "/category_doodad/"
|= (Parser.getChompedString (Parser.chompWhile ((/=) '/'))
|> Parser.andThen category
)
|. Parser.token "/"
|= restOfPath
, Parser.succeed Category
|. Parser.token "/category/"
2018-12-08 01:16:00 +03:00
|= (restOfPath |> Parser.andThen category)
2022-05-20 23:47:40 +03:00
, Parser.succeed (findExample Doodad)
|. Parser.token "/doodad/"
2018-12-08 01:16:00 +03:00
|= 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 Category.Category
2018-12-05 21:56:04 +03:00
category string =
case Category.fromString string of
2018-12-05 21:56:04 +03:00
Ok c ->
Parser.succeed c
Err e ->
Parser.problem e
2018-02-13 00:32:38 +03:00
2022-05-22 04:27:46 +03:00
updateExample : Example state msg -> Route state msg -> Maybe (Route state msg)
updateExample example route_ =
case route_ of
Doodad _ ->
Just (Doodad example)
CategoryDoodad cat _ ->
Just (CategoryDoodad cat example)
_ ->
Nothing
fromLocation : Dict String (Example state msg) -> Url -> Route state msg
fromLocation examples location =
location.fragment
|> Maybe.withDefault ""
|> Parser.run (route examples)
2018-12-05 21:56:04 +03:00
|> Result.withDefault All
2022-05-20 22:20:12 +03:00
viewBreadCrumbs : Route state msg -> Html msg2
2022-05-20 22:20:12 +03:00
viewBreadCrumbs currentRoute =
breadCrumbs currentRoute
|> Maybe.map
(BreadCrumbs.view
{ aTagAttributes = \r -> [ Attributes.href ("/" ++ toString r) ]
, isCurrentRoute = (==) currentRoute
2022-05-25 00:51:49 +03:00
, label = "breadcrumbs"
2022-05-20 22:20:12 +03:00
}
)
|> Maybe.withDefault (Html.text "")
2022-05-23 19:58:21 +03:00
headerId : Route state msg -> Maybe String
headerId route_ =
Maybe.map BreadCrumbs.headerId (breadCrumbs route_)
breadCrumbs : Route state msg -> Maybe (BreadCrumbs (Route state msg))
2022-05-20 22:20:12 +03:00
breadCrumbs route_ =
case route_ of
All ->
Just allBreadCrumb
Category category_ ->
Just (categoryCrumb category_)
Doodad example ->
2022-05-20 23:47:40 +03:00
Just
(BreadCrumbs.after allBreadCrumb
(doodadCrumb example)
)
CategoryDoodad category_ example ->
Just
(BreadCrumbs.after (categoryCrumb category_)
(doodadCrumb example)
)
NotFound _ ->
2022-05-20 22:20:12 +03:00
Nothing
allBreadCrumb : BreadCrumbs (Route state msg)
2022-05-20 22:20:12 +03:00
allBreadCrumb =
BreadCrumbs.init
{ icon = Nothing
, iconStyle = BreadCrumbs.Default
, id = "breadcrumbs__all"
, text = "All"
, route = All
}
categoryCrumb : Category.Category -> BreadCrumbs (Route state msg)
2022-05-20 22:20:12 +03:00
categoryCrumb category_ =
BreadCrumbs.after allBreadCrumb
{ icon = Nothing
, iconStyle = BreadCrumbs.Default
, id = "breadcrumbs__" ++ Category.forId category_
, text = Category.forDisplay category_
, route = Category category_
}
2022-05-20 23:47:40 +03:00
doodadCrumb : Example state msg -> BreadCrumb (Route state msg)
doodadCrumb example =
2022-05-20 23:47:40 +03:00
{ icon = Nothing
, iconStyle = BreadCrumbs.Default
, id = "breadcrumbs__" ++ dashify example.name
, text = Example.fullName example
, route = Doodad example
}