From f22352da2355ac194615590dba070b97f72407c0 Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Thu, 12 Dec 2024 10:30:52 -0800 Subject: [PATCH] WIP - Added UI to table to indicate sort column and direction --- .../custom_components/table_components.ex | 14 ++++---- .../sources/source_live/index_table_live.ex | 35 ++++++++----------- .../source_live/index_table_live.html.heex | 7 +++- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/lib/pinchflat_web/components/custom_components/table_components.ex b/lib/pinchflat_web/components/custom_components/table_components.ex index 17a0d72..2d714a7 100644 --- a/lib/pinchflat_web/components/custom_components/table_components.ex +++ b/lib/pinchflat_web/components/custom_components/table_components.ex @@ -16,6 +16,8 @@ defmodule PinchflatWeb.CustomComponents.TableComponents do """ attr :rows, :list, required: true attr :table_class, :string, default: "" + attr :sort_key, :string, default: nil + attr :sort_direction, :string, default: nil attr :row_item, :any, default: &Function.identity/1, @@ -35,10 +37,15 @@ defmodule PinchflatWeb.CustomComponents.TableComponents do {col[:label]} + <.icon + name={if @sort_direction == :asc, do: "hero-chevron-up", else: "hero-chevron-down"} + class="w-3 h-3 mt-2 ml-1 absolute" + :if={to_string(@sort_key) == col[:sort_key]} + /> @@ -59,11 +66,6 @@ defmodule PinchflatWeb.CustomComponents.TableComponents do """ end - # attr : - # def my_table(assigns) do - - # end - @doc """ Renders simple pagination controls for a table in a liveview. diff --git a/lib/pinchflat_web/controllers/sources/source_live/index_table_live.ex b/lib/pinchflat_web/controllers/sources/source_live/index_table_live.ex index f7a9400..d95a950 100644 --- a/lib/pinchflat_web/controllers/sources/source_live/index_table_live.ex +++ b/lib/pinchflat_web/controllers/sources/source_live/index_table_live.ex @@ -54,6 +54,7 @@ defmodule PinchflatWeb.Sources.SourceLive.IndexTableLive do assign(socket, %{sources: sources}) end + defp sort_attr(:pending_count), do: dynamic([s, mp, dl, pe], field(pe, :pending_count)) defp sort_attr(:downloaded_count), do: dynamic([s, mp, dl], field(dl, :downloaded_count)) defp sort_attr(:custom_name), do: dynamic([s], field(s, :custom_name)) defp sort_attr(_), do: sort_attr(:custom_name) @@ -67,34 +68,28 @@ defmodule PinchflatWeb.Sources.SourceLive.IndexTableLive do group_by: m.source_id ) + pending_subquery = + from( + m in MediaItem, + inner_join: s in assoc(m, :source), + inner_join: mp in assoc(s, :media_profile), + select: %{pending_count: count(m.id), source_id: m.source_id}, + where: ^MediaQuery.pending(), + group_by: m.source_id + ) + from s in Source, as: :source, inner_join: mp in assoc(s, :media_profile), - left_join: d in subquery(downloaded_subquery), + left_join: d in subquery(downloaded_subquery), on: d.source_id == s.id, + left_join: p in subquery(pending_subquery), on: p.source_id == s.id, on: d.source_id == s.id, where: is_nil(s.marked_for_deletion_at) and is_nil(mp.marked_for_deletion_at), preload: [media_profile: mp], select: map(s, ^Source.__schema__(:fields)), select_merge: %{ - downloaded_count: coalesce(d.downloaded_count, 0) + downloaded_count: coalesce(d.downloaded_count, 0), + pending_count: coalesce(p.pending_count, 0) } - - # select_merge: %{ - # downloaded_count: - # subquery( - # from m in MediaItem, - # where: m.source_id == parent_as(:source).id, - # where: ^MediaQuery.downloaded(), - # select: count(m.id) - # ), - # pending_count: - # subquery( - # from m in MediaItem, - # join: s in assoc(m, :source), - # where: m.source_id == parent_as(:source).id, - # where: ^MediaQuery.pending(), - # select: count(m.id) - # ) - # } end end diff --git a/lib/pinchflat_web/controllers/sources/source_live/index_table_live.html.heex b/lib/pinchflat_web/controllers/sources/source_live/index_table_live.html.heex index 2f8f134..d4eceeb 100644 --- a/lib/pinchflat_web/controllers/sources/source_live/index_table_live.html.heex +++ b/lib/pinchflat_web/controllers/sources/source_live/index_table_live.html.heex @@ -1,9 +1,14 @@ -<.table rows={@sources} table_class="text-white"> +<.table rows={@sources} table_class="text-white" sort_key={@sort_key} sort_direction={@sort_direction}> <:col :let={source} label="Name" sort_key="custom_name"> <.subtle_link href={~p"/sources/#{source.id}"}> {source.custom_name} + <:col :let={source} label="Pending" sort_key="pending_count"> + <.subtle_link href={~p"/sources/#{source.id}/#tab-pending"}> + <.localized_number number={source.pending_count} /> + + <:col :let={source} label="Downloaded" sort_key="downloaded_count"> <.subtle_link href={~p"/sources/#{source.id}/#tab-downloaded"}> <.localized_number number={source.downloaded_count} />