2020-05-08 13:24:58 +00:00
|
|
|
module Route exposing (Route(..), fromUrl, href, replaceUrl)
|
|
|
|
|
|
|
|
import Browser.Navigation
|
|
|
|
import Html
|
|
|
|
import Html.Attributes
|
|
|
|
import Url
|
|
|
|
import Url.Parser exposing ((<?>))
|
|
|
|
import Url.Parser.Query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- ROUTING
|
|
|
|
|
|
|
|
|
|
|
|
type Route
|
|
|
|
= NotFound
|
|
|
|
| Home
|
2020-07-10 07:49:43 +00:00
|
|
|
| Packages (Maybe String) (Maybe String) (Maybe String) (Maybe Int) (Maybe Int) (Maybe String)
|
|
|
|
| Options (Maybe String) (Maybe String) (Maybe String) (Maybe Int) (Maybe Int) (Maybe String)
|
2020-05-08 13:24:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
parser : Url.Parser.Parser (Route -> msg) msg
|
|
|
|
parser =
|
|
|
|
Url.Parser.oneOf
|
|
|
|
[ Url.Parser.map
|
|
|
|
Home
|
|
|
|
Url.Parser.top
|
|
|
|
, Url.Parser.map
|
|
|
|
NotFound
|
|
|
|
(Url.Parser.s "not-found")
|
|
|
|
, Url.Parser.map
|
|
|
|
Packages
|
|
|
|
(Url.Parser.s "packages"
|
2020-05-11 20:42:57 +00:00
|
|
|
<?> Url.Parser.Query.string "channel"
|
2020-05-08 13:24:58 +00:00
|
|
|
<?> Url.Parser.Query.string "query"
|
2020-06-10 12:15:54 +00:00
|
|
|
<?> Url.Parser.Query.string "show"
|
2020-05-11 19:56:10 +00:00
|
|
|
<?> Url.Parser.Query.int "from"
|
|
|
|
<?> Url.Parser.Query.int "size"
|
2020-07-10 07:49:43 +00:00
|
|
|
<?> Url.Parser.Query.string "sort"
|
2020-05-08 13:24:58 +00:00
|
|
|
)
|
|
|
|
, Url.Parser.map
|
|
|
|
Options
|
|
|
|
(Url.Parser.s "options"
|
2020-05-11 20:42:57 +00:00
|
|
|
<?> Url.Parser.Query.string "channel"
|
2020-05-08 13:24:58 +00:00
|
|
|
<?> Url.Parser.Query.string "query"
|
2020-06-10 12:15:54 +00:00
|
|
|
<?> Url.Parser.Query.string "show"
|
2020-05-11 19:56:10 +00:00
|
|
|
<?> Url.Parser.Query.int "from"
|
|
|
|
<?> Url.Parser.Query.int "size"
|
2020-07-10 07:49:43 +00:00
|
|
|
<?> Url.Parser.Query.string "sort"
|
2020-05-08 13:24:58 +00:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- PUBLIC HELPERS
|
|
|
|
|
|
|
|
|
|
|
|
href : Route -> Html.Attribute msg
|
|
|
|
href targetRoute =
|
|
|
|
Html.Attributes.href (routeToString targetRoute)
|
|
|
|
|
|
|
|
|
|
|
|
replaceUrl : Browser.Navigation.Key -> Route -> Cmd msg
|
|
|
|
replaceUrl navKey route =
|
|
|
|
Browser.Navigation.replaceUrl navKey (routeToString route)
|
|
|
|
|
|
|
|
|
|
|
|
fromUrl : Url.Url -> Maybe Route
|
|
|
|
fromUrl url =
|
|
|
|
-- The RealWorld spec treats the fragment like a path.
|
|
|
|
-- This makes it *literally* the path, so we can proceed
|
|
|
|
-- with parsing as if it had been a normal path all along.
|
|
|
|
--{ url | path = Maybe.withDefault "" url.fragment, fragment = Nothing }
|
|
|
|
Url.Parser.parse parser url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- INTERNAL
|
|
|
|
|
|
|
|
|
|
|
|
routeToString : Route -> String
|
|
|
|
routeToString page =
|
|
|
|
let
|
|
|
|
( path, query ) =
|
|
|
|
routeToPieces page
|
|
|
|
in
|
|
|
|
"/" ++ String.join "/" path ++ "?" ++ String.join "&" (List.filterMap Basics.identity query)
|
|
|
|
|
|
|
|
|
|
|
|
routeToPieces : Route -> ( List String, List (Maybe String) )
|
|
|
|
routeToPieces page =
|
|
|
|
case page of
|
|
|
|
Home ->
|
|
|
|
( [], [] )
|
|
|
|
|
|
|
|
NotFound ->
|
|
|
|
( [ "not-found" ], [] )
|
|
|
|
|
2020-07-10 07:49:43 +00:00
|
|
|
Packages channel query show from size sort ->
|
2020-05-11 19:56:10 +00:00
|
|
|
( [ "packages" ]
|
2020-05-11 20:42:57 +00:00
|
|
|
, [ channel
|
|
|
|
, query
|
2020-06-10 12:15:54 +00:00
|
|
|
, show
|
2020-05-11 19:56:10 +00:00
|
|
|
, Maybe.map String.fromInt from
|
|
|
|
, Maybe.map String.fromInt size
|
2020-07-10 07:49:43 +00:00
|
|
|
, sort
|
2020-05-11 19:56:10 +00:00
|
|
|
]
|
|
|
|
)
|
2020-05-08 13:24:58 +00:00
|
|
|
|
2020-07-10 07:49:43 +00:00
|
|
|
Options channel query show from size sort ->
|
2020-05-11 19:56:10 +00:00
|
|
|
( [ "options" ]
|
2020-05-11 20:42:57 +00:00
|
|
|
, [ channel
|
|
|
|
, query
|
2020-06-10 12:15:54 +00:00
|
|
|
, show
|
2020-05-11 19:56:10 +00:00
|
|
|
, Maybe.map String.fromInt from
|
|
|
|
, Maybe.map String.fromInt size
|
2020-07-10 07:49:43 +00:00
|
|
|
, sort
|
2020-05-11 19:56:10 +00:00
|
|
|
]
|
|
|
|
)
|