Enumerate sorting options with a type

This commit is contained in:
Correl Roush 2024-02-09 23:37:35 -05:00
parent e6296f1fef
commit 1358f38419

View file

@ -52,7 +52,7 @@ type SortBy
type alias Criteria = type alias Criteria =
{ query : String { query : String
, sortBy : String , sortBy : SortBy
, ownedOnly : Bool , ownedOnly : Bool
} }
@ -90,7 +90,7 @@ type Msg
type CriteriaMsg type CriteriaMsg
= UpdateName String = UpdateName String
| UpdateSortBy String | UpdateSortBy SortBy
| UpdateOwnedOnly Bool | UpdateOwnedOnly Bool
@ -115,8 +115,18 @@ toLoading cardPage =
searchQuery : Criteria -> List Url.Builder.QueryParameter searchQuery : Criteria -> List Url.Builder.QueryParameter
searchQuery criteria = 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 "q" criteria.query
, Url.Builder.string "sort_by" criteria.sortBy , Url.Builder.string "sort_by" (toString criteria.sortBy)
, Url.Builder.string "in_collection" , Url.Builder.string "in_collection"
(if criteria.ownedOnly then (if criteria.ownedOnly then
"yes" "yes"
@ -161,11 +171,11 @@ parseUrl =
sortBy = sortBy =
Url.Parser.Query.enum "sort_by" Url.Parser.Query.enum "sort_by"
(Dict.fromList (Dict.fromList
[ ( "rarity", "rarity" ) [ ( "rarity", RarityDescending )
, ( "price", "price" ) , ( "price", PriceDescending )
] ]
) )
|> Url.Parser.Query.map (Maybe.withDefault "price") |> Url.Parser.Query.map (Maybe.withDefault PriceDescending)
inCollection = inCollection =
Url.Parser.Query.enum "in_collection" Url.Parser.Query.enum "in_collection"
@ -188,7 +198,7 @@ criteriaFromUrl : Url.Url -> Criteria
criteriaFromUrl url = criteriaFromUrl url =
let let
emptyCriteria = emptyCriteria =
{ query = "", sortBy = "price", ownedOnly = True } { query = "", sortBy = PriceDescending, ownedOnly = True }
in in
Url.Parser.parse parseUrl url Url.Parser.parse parseUrl url
|> Maybe.withDefault emptyCriteria |> Maybe.withDefault emptyCriteria
@ -264,17 +274,9 @@ update msg model =
SortSelected sortBy -> SortSelected sortBy ->
let let
sortKey s = newValue : SortBy
case s of
PriceDescending ->
"price"
RarityDescending ->
"rarity"
newValue : String
newValue = newValue =
Maybe.withDefault PriceDescending sortBy |> sortKey Maybe.withDefault PriceDescending sortBy
in in
update (UpdateCriteria (UpdateSortBy newValue)) model update (UpdateCriteria (UpdateSortBy newValue)) model
@ -376,17 +378,6 @@ searchBar model =
sortDropdown = sortDropdown =
let let
fromString s =
case s of
"price" ->
PriceDescending
"rarity" ->
RarityDescending
_ ->
PriceDescending
toString s = toString s =
case s of case s of
PriceDescending -> PriceDescending ->
@ -431,7 +422,7 @@ sortDropdown =
in in
Dropdown.basic Dropdown.basic
{ itemsFromModel = always [ PriceDescending, RarityDescending ] { itemsFromModel = always [ PriceDescending, RarityDescending ]
, selectionFromModel = .criteria >> .sortBy >> fromString >> Just , selectionFromModel = .criteria >> .sortBy >> Just
, dropdownMsg = DropdownMsg , dropdownMsg = DropdownMsg
, onSelectMsg = SortSelected , onSelectMsg = SortSelected
, itemToPrompt = itemToPrompt , itemToPrompt = itemToPrompt