Can order record sets

This commit is contained in:
Claudio Ortolina
2026-02-07 10:23:35 +00:00
parent 79aeee6363
commit abb1ab332a
6 changed files with 72 additions and 8 deletions
+10 -1
View File
@@ -29,15 +29,24 @@ defmodule MusicLibrary.RecordSets do
def search_record_sets(query, opts \\ []) do
offset = Keyword.get(opts, :offset, 0)
limit = Keyword.get(opts, :limit, 20)
order = Keyword.get(opts, :order, :updated_at)
record_sets_search_query(query)
|> order_by([rs], desc: rs.updated_at)
|> apply_order(order)
|> offset(^offset)
|> limit(^limit)
|> preload(items: :record)
|> Repo.all()
end
defp apply_order(query, :alphabetical) do
order_by(query, [rs], fragment("? COLLATE NOCASE ASC", rs.name))
end
defp apply_order(query, _) do
order_by(query, [rs], desc: rs.updated_at)
end
def get_record_set!(id) do
RecordSet
|> Repo.get!(id)
@@ -91,7 +91,7 @@ defmodule MusicLibraryWeb.Components.Pagination do
attr :query, :string, required: true
attr :order, :atom,
values: [:alphabetical, :purchase, :scrobbled_at, :title, :artist, :album],
values: [:alphabetical, :purchase, :scrobbled_at, :title, :artist, :album, :updated_at],
required: true
defp next_link(assigns) do
@@ -111,7 +111,7 @@ defmodule MusicLibraryWeb.Components.Pagination do
attr :query, :string, required: true
attr :order, :atom,
values: [:alphabetical, :purchase, :scrobbled_at, :title, :artist, :album],
values: [:alphabetical, :purchase, :scrobbled_at, :title, :artist, :album, :updated_at],
required: true
defp prev_link(assigns) do
@@ -140,7 +140,7 @@ defmodule MusicLibraryWeb.Components.Pagination do
attr :query, :string, required: true
attr :order, :atom,
values: [:alphabetical, :purchase, :scrobbled_at, :title, :artist, :album],
values: [:alphabetical, :purchase, :scrobbled_at, :title, :artist, :album, :updated_at],
required: true
defp numbered_link(assigns) when assigns.active do
@@ -48,11 +48,13 @@ defmodule MusicLibraryWeb.RecordSetLive.Index do
defp apply_action(socket, :index, params) do
query = params["query"] || ""
order = parse_order(params["order"] || "updated_at")
total_sets = RecordSets.count_record_sets(query)
list_params =
@default_list_params
|> merge_query(query)
|> merge_order(order)
|> merge_pagination(params, total_sets)
load_and_assign_sets(socket, list_params)
@@ -109,7 +111,8 @@ defmodule MusicLibraryWeb.RecordSetLive.Index do
sets =
RecordSets.search_record_sets(list_params.query,
offset: offset,
limit: list_params.page_size
limit: list_params.page_size,
order: list_params.order
)
list_params_with_total =
@@ -125,7 +128,7 @@ defmodule MusicLibraryWeb.RecordSetLive.Index do
def back_path(list_params) do
qs =
list_params
|> Map.take([:query, :page, :page_size])
|> Map.take([:query, :page, :page_size, :order])
|> Enum.filter(fn {_, v} -> v not in ["", nil] end)
~p"/record-sets?#{qs}"
@@ -163,7 +166,8 @@ defmodule MusicLibraryWeb.RecordSetLive.Index do
qs =
@default_list_params
|> Map.put(:query, query)
|> Map.take([:query, :page, :page_size])
|> Map.put(:order, socket.assigns.list_params.order)
|> Map.take([:query, :page, :page_size, :order])
{:noreply, push_patch(socket, to: ~p"/record-sets?#{qs}")}
end
@@ -199,6 +203,24 @@ defmodule MusicLibraryWeb.RecordSetLive.Index do
{:noreply, stream_insert(socket, :record_sets, updated_set)}
end
defp parse_order("alphabetical"), do: :alphabetical
defp parse_order("updated_at"), do: :updated_at
defp parse_order(_), do: :updated_at
defp merge_order(params, order) do
Map.put(params, :order, order)
end
defp order_path(list_params, order) do
qs =
list_params
|> Map.take([:query])
|> Map.put(:order, order)
|> Enum.filter(fn {_, v} -> v not in ["", nil] end)
~p"/record-sets?#{qs}"
end
defp render_description(description) do
description
|> Markdown.to_html()
@@ -21,6 +21,27 @@
<.search_form query={@list_params.query} />
<div class="flex items-end justify-between gap-6 mt-4">
<.button_group>
<.button
patch={order_path(@list_params, :updated_at)}
size="sm"
class={[@list_params.order == :updated_at && "bg-zinc-100! dark:bg-zinc-700!"]}
>
<.icon name="hero-clock" class="icon" aria-hidden="true" data-slot="icon" />
<span class="sr-only sm:not-sr-only">{gettext("Updated")}</span>
</.button>
<.button
patch={order_path(@list_params, :alphabetical)}
size="sm"
class={[@list_params.order == :alphabetical && "bg-zinc-100! dark:bg-zinc-700!"]}
>
<.icon name="hero-user-solid" class="icon" aria-hidden="true" data-slot="icon" />
<span class="sr-only sm:not-sr-only">{gettext("A->Z")}</span>
</.button>
</.button_group>
</div>
<div class="mt-6 space-y-6">
<ul phx-update="stream" id="record-sets-list" class="space-y-6">
<li
@@ -71,7 +92,7 @@
<.dropdown_link
id={"set-actions-#{record_set.id}-edit"}
patch={
~p"/record-sets/#{record_set}/edit?#{Map.take(@list_params, [:query, :page, :page_size]) |> Enum.filter(fn {_, v} -> v not in ["", nil] end)}"
~p"/record-sets/#{record_set}/edit?#{Map.take(@list_params, [:query, :page, :page_size, :order]) |> Enum.filter(fn {_, v} -> v not in ["", nil] end)}"
}
>
{gettext("Edit")}