2020-10-30 18:13:55 +00:00
|
|
|
module Route exposing
|
|
|
|
( Route(..)
|
|
|
|
, SearchArgs
|
|
|
|
, SearchRoute
|
|
|
|
, fromUrl
|
|
|
|
, href
|
|
|
|
, replaceUrl
|
|
|
|
, routeToString
|
|
|
|
)
|
2020-05-08 13:24:58 +00:00
|
|
|
|
|
|
|
import Browser.Navigation
|
|
|
|
import Html
|
|
|
|
import Html.Attributes
|
2020-10-30 18:13:55 +00:00
|
|
|
import Route.SearchQuery exposing (SearchQuery)
|
2020-05-08 13:24:58 +00:00
|
|
|
import Url
|
2020-10-28 18:51:01 +00:00
|
|
|
import Url.Builder exposing (QueryParameter)
|
2020-10-30 18:13:55 +00:00
|
|
|
import Url.Parser exposing ((</>), (<?>))
|
2020-05-08 13:24:58 +00:00
|
|
|
import Url.Parser.Query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- ROUTING
|
|
|
|
|
|
|
|
|
2020-10-30 18:13:55 +00:00
|
|
|
type alias SearchArgs =
|
|
|
|
{ query : Maybe SearchQuery
|
|
|
|
, channel : Maybe String
|
|
|
|
, show : Maybe String
|
|
|
|
, from : Maybe Int
|
|
|
|
, size : Maybe Int
|
2020-05-08 13:24:58 +00:00
|
|
|
|
2020-10-30 18:13:55 +00:00
|
|
|
-- TODO: embed sort type
|
|
|
|
, sort : Maybe String
|
|
|
|
}
|
2020-05-08 13:24:58 +00:00
|
|
|
|
2020-10-30 18:13:55 +00:00
|
|
|
|
|
|
|
type alias SearchRoute =
|
|
|
|
SearchArgs -> Route
|
|
|
|
|
|
|
|
|
|
|
|
searchQueryParser : Url.Url -> Url.Parser.Parser (SearchArgs -> msg) msg
|
|
|
|
searchQueryParser url =
|
2020-10-28 18:51:01 +00:00
|
|
|
let
|
|
|
|
rawQuery =
|
|
|
|
Route.SearchQuery.toRawQuery url
|
|
|
|
|
2020-12-16 11:45:10 +00:00
|
|
|
maybeQuery =
|
|
|
|
Maybe.andThen (Route.SearchQuery.searchString "query") rawQuery
|
2020-10-28 18:51:01 +00:00
|
|
|
in
|
2020-10-30 18:13:55 +00:00
|
|
|
Url.Parser.map (SearchArgs maybeQuery) <|
|
|
|
|
Url.Parser.top
|
|
|
|
<?> Url.Parser.Query.string "channel"
|
|
|
|
<?> Url.Parser.Query.string "show"
|
|
|
|
<?> Url.Parser.Query.int "from"
|
|
|
|
<?> Url.Parser.Query.int "size"
|
|
|
|
<?> Url.Parser.Query.string "sort"
|
|
|
|
|
|
|
|
|
|
|
|
searchArgsToUrl : SearchArgs -> ( List QueryParameter, Maybe ( String, Route.SearchQuery.SearchQuery ) )
|
|
|
|
searchArgsToUrl args =
|
|
|
|
( List.filterMap identity
|
|
|
|
[ Maybe.map (Url.Builder.string "channel") args.channel
|
|
|
|
, Maybe.map (Url.Builder.string "show") args.show
|
|
|
|
, Maybe.map (Url.Builder.int "from") args.from
|
|
|
|
, Maybe.map (Url.Builder.int "size") args.size
|
|
|
|
, Maybe.map (Url.Builder.string "sort") args.sort
|
|
|
|
]
|
|
|
|
, Maybe.map (Tuple.pair "query") args.query
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Route
|
|
|
|
= NotFound
|
|
|
|
| Home
|
|
|
|
| Packages SearchArgs
|
|
|
|
| Options SearchArgs
|
|
|
|
|
|
|
|
|
|
|
|
parser : Url.Url -> Url.Parser.Parser (Route -> msg) msg
|
|
|
|
parser url =
|
2020-05-08 13:24:58 +00:00
|
|
|
Url.Parser.oneOf
|
2020-10-28 18:51:01 +00:00
|
|
|
[ Url.Parser.map Home Url.Parser.top
|
2020-10-30 18:13:55 +00:00
|
|
|
, Url.Parser.map NotFound <| Url.Parser.s "not-found"
|
|
|
|
, Url.Parser.map Packages <| Url.Parser.s "packages" </> searchQueryParser url
|
|
|
|
, Url.Parser.map Options <| Url.Parser.s "options" </> searchQueryParser url
|
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 }
|
2020-10-28 18:51:01 +00:00
|
|
|
Url.Parser.parse (parser url) url
|
2020-05-08 13:24:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- INTERNAL
|
|
|
|
|
|
|
|
|
|
|
|
routeToString : Route -> String
|
2020-10-28 18:51:01 +00:00
|
|
|
routeToString =
|
2020-05-08 13:24:58 +00:00
|
|
|
let
|
2020-10-28 18:51:01 +00:00
|
|
|
buildString ( path, query, searchQuery ) =
|
|
|
|
Route.SearchQuery.absolute path query <|
|
|
|
|
Maybe.withDefault [] <|
|
|
|
|
Maybe.map List.singleton searchQuery
|
2020-05-08 13:24:58 +00:00
|
|
|
in
|
2020-10-28 18:51:01 +00:00
|
|
|
buildString << routeToPieces
|
2020-05-08 13:24:58 +00:00
|
|
|
|
|
|
|
|
2020-10-28 18:51:01 +00:00
|
|
|
routeToPieces : Route -> ( List String, List QueryParameter, Maybe ( String, Route.SearchQuery.SearchQuery ) )
|
2020-05-08 13:24:58 +00:00
|
|
|
routeToPieces page =
|
2020-10-30 18:13:55 +00:00
|
|
|
case page of
|
|
|
|
Home ->
|
|
|
|
( [], [], Nothing )
|
2020-10-28 18:51:01 +00:00
|
|
|
|
2020-10-30 18:13:55 +00:00
|
|
|
NotFound ->
|
|
|
|
( [ "not-found" ], [], Nothing )
|
2020-10-28 18:51:01 +00:00
|
|
|
|
2020-10-30 18:13:55 +00:00
|
|
|
Packages searchArgs ->
|
|
|
|
searchArgsToUrl searchArgs
|
|
|
|
|> (\( query, raw ) -> ( [ "packages" ], query, raw ))
|
2020-10-28 18:51:01 +00:00
|
|
|
|
2020-10-30 18:13:55 +00:00
|
|
|
Options searchArgs ->
|
|
|
|
searchArgsToUrl searchArgs
|
|
|
|
|> (\( query, raw ) -> ( [ "options" ], query, raw ))
|