Can order record sets
This commit is contained in:
@@ -29,15 +29,24 @@ defmodule MusicLibrary.RecordSets do
|
|||||||
def search_record_sets(query, opts \\ []) do
|
def search_record_sets(query, opts \\ []) do
|
||||||
offset = Keyword.get(opts, :offset, 0)
|
offset = Keyword.get(opts, :offset, 0)
|
||||||
limit = Keyword.get(opts, :limit, 20)
|
limit = Keyword.get(opts, :limit, 20)
|
||||||
|
order = Keyword.get(opts, :order, :updated_at)
|
||||||
|
|
||||||
record_sets_search_query(query)
|
record_sets_search_query(query)
|
||||||
|> order_by([rs], desc: rs.updated_at)
|
|> apply_order(order)
|
||||||
|> offset(^offset)
|
|> offset(^offset)
|
||||||
|> limit(^limit)
|
|> limit(^limit)
|
||||||
|> preload(items: :record)
|
|> preload(items: :record)
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
end
|
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
|
def get_record_set!(id) do
|
||||||
RecordSet
|
RecordSet
|
||||||
|> Repo.get!(id)
|
|> Repo.get!(id)
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ defmodule MusicLibraryWeb.Components.Pagination do
|
|||||||
attr :query, :string, required: true
|
attr :query, :string, required: true
|
||||||
|
|
||||||
attr :order, :atom,
|
attr :order, :atom,
|
||||||
values: [:alphabetical, :purchase, :scrobbled_at, :title, :artist, :album],
|
values: [:alphabetical, :purchase, :scrobbled_at, :title, :artist, :album, :updated_at],
|
||||||
required: true
|
required: true
|
||||||
|
|
||||||
defp next_link(assigns) do
|
defp next_link(assigns) do
|
||||||
@@ -111,7 +111,7 @@ defmodule MusicLibraryWeb.Components.Pagination do
|
|||||||
attr :query, :string, required: true
|
attr :query, :string, required: true
|
||||||
|
|
||||||
attr :order, :atom,
|
attr :order, :atom,
|
||||||
values: [:alphabetical, :purchase, :scrobbled_at, :title, :artist, :album],
|
values: [:alphabetical, :purchase, :scrobbled_at, :title, :artist, :album, :updated_at],
|
||||||
required: true
|
required: true
|
||||||
|
|
||||||
defp prev_link(assigns) do
|
defp prev_link(assigns) do
|
||||||
@@ -140,7 +140,7 @@ defmodule MusicLibraryWeb.Components.Pagination do
|
|||||||
attr :query, :string, required: true
|
attr :query, :string, required: true
|
||||||
|
|
||||||
attr :order, :atom,
|
attr :order, :atom,
|
||||||
values: [:alphabetical, :purchase, :scrobbled_at, :title, :artist, :album],
|
values: [:alphabetical, :purchase, :scrobbled_at, :title, :artist, :album, :updated_at],
|
||||||
required: true
|
required: true
|
||||||
|
|
||||||
defp numbered_link(assigns) when assigns.active do
|
defp numbered_link(assigns) when assigns.active do
|
||||||
|
|||||||
@@ -48,11 +48,13 @@ defmodule MusicLibraryWeb.RecordSetLive.Index do
|
|||||||
|
|
||||||
defp apply_action(socket, :index, params) do
|
defp apply_action(socket, :index, params) do
|
||||||
query = params["query"] || ""
|
query = params["query"] || ""
|
||||||
|
order = parse_order(params["order"] || "updated_at")
|
||||||
total_sets = RecordSets.count_record_sets(query)
|
total_sets = RecordSets.count_record_sets(query)
|
||||||
|
|
||||||
list_params =
|
list_params =
|
||||||
@default_list_params
|
@default_list_params
|
||||||
|> merge_query(query)
|
|> merge_query(query)
|
||||||
|
|> merge_order(order)
|
||||||
|> merge_pagination(params, total_sets)
|
|> merge_pagination(params, total_sets)
|
||||||
|
|
||||||
load_and_assign_sets(socket, list_params)
|
load_and_assign_sets(socket, list_params)
|
||||||
@@ -109,7 +111,8 @@ defmodule MusicLibraryWeb.RecordSetLive.Index do
|
|||||||
sets =
|
sets =
|
||||||
RecordSets.search_record_sets(list_params.query,
|
RecordSets.search_record_sets(list_params.query,
|
||||||
offset: offset,
|
offset: offset,
|
||||||
limit: list_params.page_size
|
limit: list_params.page_size,
|
||||||
|
order: list_params.order
|
||||||
)
|
)
|
||||||
|
|
||||||
list_params_with_total =
|
list_params_with_total =
|
||||||
@@ -125,7 +128,7 @@ defmodule MusicLibraryWeb.RecordSetLive.Index do
|
|||||||
def back_path(list_params) do
|
def back_path(list_params) do
|
||||||
qs =
|
qs =
|
||||||
list_params
|
list_params
|
||||||
|> Map.take([:query, :page, :page_size])
|
|> Map.take([:query, :page, :page_size, :order])
|
||||||
|> Enum.filter(fn {_, v} -> v not in ["", nil] end)
|
|> Enum.filter(fn {_, v} -> v not in ["", nil] end)
|
||||||
|
|
||||||
~p"/record-sets?#{qs}"
|
~p"/record-sets?#{qs}"
|
||||||
@@ -163,7 +166,8 @@ defmodule MusicLibraryWeb.RecordSetLive.Index do
|
|||||||
qs =
|
qs =
|
||||||
@default_list_params
|
@default_list_params
|
||||||
|> Map.put(:query, query)
|
|> 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}")}
|
{:noreply, push_patch(socket, to: ~p"/record-sets?#{qs}")}
|
||||||
end
|
end
|
||||||
@@ -199,6 +203,24 @@ defmodule MusicLibraryWeb.RecordSetLive.Index do
|
|||||||
{:noreply, stream_insert(socket, :record_sets, updated_set)}
|
{:noreply, stream_insert(socket, :record_sets, updated_set)}
|
||||||
end
|
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
|
defp render_description(description) do
|
||||||
description
|
description
|
||||||
|> Markdown.to_html()
|
|> Markdown.to_html()
|
||||||
|
|||||||
@@ -21,6 +21,27 @@
|
|||||||
|
|
||||||
<.search_form query={@list_params.query} />
|
<.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">
|
<div class="mt-6 space-y-6">
|
||||||
<ul phx-update="stream" id="record-sets-list" class="space-y-6">
|
<ul phx-update="stream" id="record-sets-list" class="space-y-6">
|
||||||
<li
|
<li
|
||||||
@@ -71,7 +92,7 @@
|
|||||||
<.dropdown_link
|
<.dropdown_link
|
||||||
id={"set-actions-#{record_set.id}-edit"}
|
id={"set-actions-#{record_set.id}-edit"}
|
||||||
patch={
|
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")}
|
{gettext("Edit")}
|
||||||
|
|||||||
@@ -386,6 +386,7 @@ msgid "Error loading play count"
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: lib/music_library_web/live/collection_live/index.html.heex
|
#: lib/music_library_web/live/collection_live/index.html.heex
|
||||||
|
#: lib/music_library_web/live/record_set_live/index.html.heex
|
||||||
#: lib/music_library_web/live/wishlist_live/index.html.heex
|
#: lib/music_library_web/live/wishlist_live/index.html.heex
|
||||||
#, elixir-autogen, elixir-format
|
#, elixir-autogen, elixir-format
|
||||||
msgid "A->Z"
|
msgid "A->Z"
|
||||||
@@ -1803,3 +1804,8 @@ msgstr ""
|
|||||||
#, elixir-autogen, elixir-format
|
#, elixir-autogen, elixir-format
|
||||||
msgid "%{collected}/%{total} records"
|
msgid "%{collected}/%{total} records"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
#: lib/music_library_web/live/record_set_live/index.html.heex
|
||||||
|
#, elixir-autogen, elixir-format
|
||||||
|
msgid "Updated"
|
||||||
|
msgstr ""
|
||||||
|
|||||||
@@ -386,6 +386,7 @@ msgid "Error loading play count"
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: lib/music_library_web/live/collection_live/index.html.heex
|
#: lib/music_library_web/live/collection_live/index.html.heex
|
||||||
|
#: lib/music_library_web/live/record_set_live/index.html.heex
|
||||||
#: lib/music_library_web/live/wishlist_live/index.html.heex
|
#: lib/music_library_web/live/wishlist_live/index.html.heex
|
||||||
#, elixir-autogen, elixir-format
|
#, elixir-autogen, elixir-format
|
||||||
msgid "A->Z"
|
msgid "A->Z"
|
||||||
@@ -1803,3 +1804,8 @@ msgstr ""
|
|||||||
#, elixir-autogen, elixir-format
|
#, elixir-autogen, elixir-format
|
||||||
msgid "%{collected}/%{total} records"
|
msgid "%{collected}/%{total} records"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
#: lib/music_library_web/live/record_set_live/index.html.heex
|
||||||
|
#, elixir-autogen, elixir-format, fuzzy
|
||||||
|
msgid "Updated"
|
||||||
|
msgstr ""
|
||||||
|
|||||||
Reference in New Issue
Block a user