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', 'CAST(COALESCE(CASE WHEN "copies"."isFoil" THEN cards.price_usd_foil ELSE cards.price_usd END, 0) as decimal) DESC',
*orderings, *orderings,
] ]
elif sort_by == "created":
orderings = [
"copies.created_date DESC",
*orderings,
]
params["last_update_key"] = "last_update" params["last_update_key"] = "last_update"
query = " ".join( query = " ".join(
[ [

View file

@ -48,11 +48,12 @@ decodeStatistics =
type SortBy type SortBy
= PriceDescending = PriceDescending
| RarityDescending | RarityDescending
| CreatedDescending
type alias Criteria = type alias Criteria =
{ query : String { query : String
, sortBy : String , sortBy : SortBy
, ownedOnly : Bool , ownedOnly : Bool
} }
@ -90,7 +91,7 @@ type Msg
type CriteriaMsg type CriteriaMsg
= UpdateName String = UpdateName String
| UpdateSortBy String | UpdateSortBy SortBy
| UpdateOwnedOnly Bool | UpdateOwnedOnly Bool
@ -115,8 +116,21 @@ 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"
CreatedDescending ->
"created"
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 +175,12 @@ 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 )
, ( "created", CreatedDescending )
] ]
) )
|> 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 +203,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 +279,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 +383,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 ->
@ -395,6 +391,9 @@ sortDropdown =
RarityDescending -> RarityDescending ->
"Rarity DESC" "Rarity DESC"
CreatedDescending ->
"Created DESC"
selectAttrs = selectAttrs =
[ Background.color UI.colors.secondary [ Background.color UI.colors.secondary
, Border.width 1 , Border.width 1
@ -430,8 +429,13 @@ sortDropdown =
toString sortBy toString sortBy
in in
Dropdown.basic Dropdown.basic
{ itemsFromModel = always [ PriceDescending, RarityDescending ] { itemsFromModel =
, selectionFromModel = .criteria >> .sortBy >> fromString >> Just always
[ PriceDescending
, RarityDescending
, CreatedDescending
]
, selectionFromModel = .criteria >> .sortBy >> Just
, dropdownMsg = DropdownMsg , dropdownMsg = DropdownMsg
, onSelectMsg = SortSelected , onSelectMsg = SortSelected
, itemToPrompt = itemToPrompt , itemToPrompt = itemToPrompt