I’m building a search interface where the user can type in a search query, but also choose from a fixed set of filters (category, color, brand, price etc).
The filters are completely dynamic and, possibly, unique for each category so there is no way for me to specify all the filter alternatives in Elm. What I’d like to do is to fetch all the selected options in a query string like this:
/search?q=Gore-Tex&color=green,blue&brand=Nike&sortBy=price&sortOrder=ASC
and parse it into a Dict String (List String)
, the above example should result in:
Dict.fromList
[ ( "q", ["Gore-Tex"] )
, ( "color", [ "green", "blue"] )
, ( "brand", ["Nike"] )
, ( "sortBy", ["price"] )
, ( "sortOrder", ["ASC"] )
]
I’m not too comfortable with the Url Parser library but it seems that every function has to take a string key. But the QueryParser actually seems to represent the query exactly as the type of Dict that I’m looking for, without any way to retrieve the entire Dict for further processing. https://github.com/elm/url/blob/1.0.0/src/Url/Parser/Internal.elm#L9
I imagine something like this should be sufficient:
import Url.Parser exposing (Parser, (</>), (<?>), int, map, oneOf, s, string)
import Url.Parser.Query as Query
type Route
| Search (Dict String (List String))
routeParser : Parser (Route -> a) a
routeParser =
oneOf
[
, map Search (s "search" <?> Query.all)
]
with the all function defined like this:
all : (Dict String (List String)) -> Parser a
all func =
Q.Parser <| \dict -> func dict
Any help on how to achieve the above would be appreciated.