Enumerate sorting options with a type
This commit is contained in:
parent
e6296f1fef
commit
1358f38419
1 changed files with 20 additions and 29 deletions
|
@ -52,7 +52,7 @@ type SortBy
|
|||
|
||||
type alias Criteria =
|
||||
{ query : String
|
||||
, sortBy : String
|
||||
, sortBy : SortBy
|
||||
, ownedOnly : Bool
|
||||
}
|
||||
|
||||
|
@ -90,7 +90,7 @@ type Msg
|
|||
|
||||
type CriteriaMsg
|
||||
= UpdateName String
|
||||
| UpdateSortBy String
|
||||
| UpdateSortBy SortBy
|
||||
| UpdateOwnedOnly Bool
|
||||
|
||||
|
||||
|
@ -115,8 +115,18 @@ toLoading cardPage =
|
|||
|
||||
searchQuery : Criteria -> List Url.Builder.QueryParameter
|
||||
searchQuery criteria =
|
||||
let
|
||||
toString : SortBy -> String
|
||||
toString sortBy =
|
||||
case sortBy of
|
||||
PriceDescending ->
|
||||
"price"
|
||||
|
||||
RarityDescending ->
|
||||
"rarity"
|
||||
in
|
||||
[ Url.Builder.string "q" criteria.query
|
||||
, Url.Builder.string "sort_by" criteria.sortBy
|
||||
, Url.Builder.string "sort_by" (toString criteria.sortBy)
|
||||
, Url.Builder.string "in_collection"
|
||||
(if criteria.ownedOnly then
|
||||
"yes"
|
||||
|
@ -161,11 +171,11 @@ parseUrl =
|
|||
sortBy =
|
||||
Url.Parser.Query.enum "sort_by"
|
||||
(Dict.fromList
|
||||
[ ( "rarity", "rarity" )
|
||||
, ( "price", "price" )
|
||||
[ ( "rarity", RarityDescending )
|
||||
, ( "price", PriceDescending )
|
||||
]
|
||||
)
|
||||
|> Url.Parser.Query.map (Maybe.withDefault "price")
|
||||
|> Url.Parser.Query.map (Maybe.withDefault PriceDescending)
|
||||
|
||||
inCollection =
|
||||
Url.Parser.Query.enum "in_collection"
|
||||
|
@ -188,7 +198,7 @@ criteriaFromUrl : Url.Url -> Criteria
|
|||
criteriaFromUrl url =
|
||||
let
|
||||
emptyCriteria =
|
||||
{ query = "", sortBy = "price", ownedOnly = True }
|
||||
{ query = "", sortBy = PriceDescending, ownedOnly = True }
|
||||
in
|
||||
Url.Parser.parse parseUrl url
|
||||
|> Maybe.withDefault emptyCriteria
|
||||
|
@ -264,17 +274,9 @@ update msg model =
|
|||
|
||||
SortSelected sortBy ->
|
||||
let
|
||||
sortKey s =
|
||||
case s of
|
||||
PriceDescending ->
|
||||
"price"
|
||||
|
||||
RarityDescending ->
|
||||
"rarity"
|
||||
|
||||
newValue : String
|
||||
newValue : SortBy
|
||||
newValue =
|
||||
Maybe.withDefault PriceDescending sortBy |> sortKey
|
||||
Maybe.withDefault PriceDescending sortBy
|
||||
in
|
||||
update (UpdateCriteria (UpdateSortBy newValue)) model
|
||||
|
||||
|
@ -376,17 +378,6 @@ searchBar model =
|
|||
|
||||
sortDropdown =
|
||||
let
|
||||
fromString s =
|
||||
case s of
|
||||
"price" ->
|
||||
PriceDescending
|
||||
|
||||
"rarity" ->
|
||||
RarityDescending
|
||||
|
||||
_ ->
|
||||
PriceDescending
|
||||
|
||||
toString s =
|
||||
case s of
|
||||
PriceDescending ->
|
||||
|
@ -431,7 +422,7 @@ sortDropdown =
|
|||
in
|
||||
Dropdown.basic
|
||||
{ itemsFromModel = always [ PriceDescending, RarityDescending ]
|
||||
, selectionFromModel = .criteria >> .sortBy >> fromString >> Just
|
||||
, selectionFromModel = .criteria >> .sortBy >> Just
|
||||
, dropdownMsg = DropdownMsg
|
||||
, onSelectMsg = SortSelected
|
||||
, itemToPrompt = itemToPrompt
|
||||
|
|
Loading…
Reference in a new issue