Closed rofrol closed 6 years ago
Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!
Here is what to expect next, and if anyone wants to comment, keep these things in mind.
It wasn't immediately obvious to me that in such case, you need to map the parser, eg:
type alias QueryData =
{ hash : Maybe String
, lang : Maybe String
}
queryParser =
top <?> stringParam "hash" <?> stringParam "lang"
parsePath (map QueryData queryParser) location
-- /?hash=abc&lang=fr ==> Just { hash = Just "abc", lang = Just "fr" }
If you omit the map
ing, you'll have an error like the following:
The 1st argument to function `parsePath` is causing a mismatch.
75| parsePath queryParser url
^^^^^^^^^^^
Function `parsePath` is expecting the 1st argument to be:
UrlParser.Parser (Maybe String -> Maybe String) (Maybe String)
But it is:
UrlParser.Parser
(Maybe String -> Maybe String -> Maybe String)
(Maybe String)
Hint: It looks like a function needs 1 more argument.
Another, more complex example:
I parse a datatype from the sortBy
query parameter, falling back to a default if there is no parameter:
-- Sorting.elm
type Option
= ProductNameAsc
| ProductNameDesc
default = ProductNameAsc
fromQueryString pathParser =
Url.map (\f a -> f a)
(pathParser
<?> Url.customParam "sortBy" (Maybe.withDefault "" >> fromQueryValue)
)
fromQueryValue data =
case data of
"name-asc" ->
ProductNameAsc
"name-desc" ->
ProductNameDesc
_ ->
default
Then, I compose that query parser to build a more complex data type:
-- Pagination.elm
type alias Data =
{ page : Int
, perPage : Int
, sorting : Sorting.Option
}
default = Data 1 25 Sorting.default
fromQueryString pathParser =
Url.map (\constructor page perPage -> constructor << Data page perPage)
(pathParser
<?> optionalIntParam "page" (default.page)
<?> optionalIntParam "perPage" (default.perPage)
)
|> Sorting.fromQueryString
where optionalIntParam : String -> Int -> QueryParser (Int -> b) b
I think this will be clearer in the next iteration of the API, so we can revisit if this is clear enough after it comes out.
Time has come to revisit this now?
Time has come to revisit this now?
How would you want it to work?
It's not clear what should be done to have multiple query parameters.