Compare commits

...

2 commits

Author SHA1 Message Date
2554900554 Add sort by created date 2024-02-09 23:43:29 -05:00
1358f38419 Enumerate sorting options with a type 2024-02-09 23:37:35 -05:00
2 changed files with 39 additions and 30 deletions

View file

@ -194,6 +194,11 @@ async def advanced_search(
'CAST(COALESCE(CASE WHEN "copies"."isFoil" THEN cards.price_usd_foil ELSE cards.price_usd END, 0) as decimal) DESC',
*orderings,
]
elif sort_by == "created":
orderings = [
"copies.created_date DESC",
*orderings,
]
params["last_update_key"] = "last_update"
query = " ".join(
[

View file

@ -48,11 +48,12 @@ decodeStatistics =
type SortBy
= PriceDescending
| RarityDescending
| CreatedDescending
type alias Criteria =
{ query : String
, sortBy : String
, sortBy : SortBy
, ownedOnly : Bool
}
@ -90,7 +91,7 @@ type Msg
type CriteriaMsg
= UpdateName String
| UpdateSortBy String
| UpdateSortBy SortBy
| UpdateOwnedOnly Bool
@ -115,8 +116,21 @@ toLoading cardPage =
searchQuery : Criteria -> List Url.Builder.QueryParameter
searchQuery criteria =
let
toString : SortBy -> String
toString sortBy =
case sortBy of
PriceDescending ->
"price"
RarityDescending ->
"rarity"
CreatedDescending ->
"created"
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 +175,12 @@ parseUrl =
sortBy =
Url.Parser.Query.enum "sort_by"
(Dict.fromList
[ ( "rarity", "rarity" )
, ( "price", "price" )
[ ( "rarity", RarityDescending )
, ( "price", PriceDescending )
, ( "created", CreatedDescending )
]
)
|> Url.Parser.Query.map (Maybe.withDefault "price")
|> Url.Parser.Query.map (Maybe.withDefault PriceDescending)
inCollection =
Url.Parser.Query.enum "in_collection"
@ -188,7 +203,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 +279,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 +383,6 @@ searchBar model =
sortDropdown =
let
fromString s =
case s of
"price" ->
PriceDescending
"rarity" ->
RarityDescending
_ ->
PriceDescending
toString s =
case s of
PriceDescending ->
@ -395,6 +391,9 @@ sortDropdown =
RarityDescending ->
"Rarity DESC"
CreatedDescending ->
"Created DESC"
selectAttrs =
[ Background.color UI.colors.secondary
, Border.width 1
@ -430,8 +429,13 @@ sortDropdown =
toString sortBy
in
Dropdown.basic
{ itemsFromModel = always [ PriceDescending, RarityDescending ]
, selectionFromModel = .criteria >> .sortBy >> fromString >> Just
{ itemsFromModel =
always
[ PriceDescending
, RarityDescending
, CreatedDescending
]
, selectionFromModel = .criteria >> .sortBy >> Just
, dropdownMsg = DropdownMsg
, onSelectMsg = SortSelected
, itemToPrompt = itemToPrompt