diff --git a/lib/pinchflat_web/controllers/sources/source_controller.ex b/lib/pinchflat_web/controllers/sources/source_controller.ex index 9c48cec..08af6d1 100644 --- a/lib/pinchflat_web/controllers/sources/source_controller.ex +++ b/lib/pinchflat_web/controllers/sources/source_controller.ex @@ -1,12 +1,11 @@ defmodule PinchflatWeb.Sources.SourceController do use PinchflatWeb, :controller - use Pinchflat.Media.MediaQuery + use Pinchflat.Sources.SourcesQuery alias Pinchflat.Repo alias Pinchflat.Tasks alias Pinchflat.Sources alias Pinchflat.Sources.Source - alias Pinchflat.Media.MediaItem alias Pinchflat.Profiles.MediaProfile alias Pinchflat.Media.FileSyncingWorker alias Pinchflat.Sources.SourceDeletionWorker @@ -15,33 +14,7 @@ defmodule PinchflatWeb.Sources.SourceController do alias Pinchflat.Metadata.SourceMetadataStorageWorker def index(conn, _params) do - source_query = - from s in Source, - as: :source, - inner_join: mp in assoc(s, :media_profile), - where: is_nil(s.marked_for_deletion_at) and is_nil(mp.marked_for_deletion_at), - preload: [media_profile: mp], - order_by: [asc: s.custom_name], - select: map(s, ^Source.__schema__(:fields)), - 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) - ) - } - - render(conn, :index, sources: Repo.all(source_query)) + render(conn, :index) end def new(conn, params) do diff --git a/lib/pinchflat_web/controllers/sources/source_html/index.html.heex b/lib/pinchflat_web/controllers/sources/source_html/index.html.heex index 5913d30..55cd45a 100644 --- a/lib/pinchflat_web/controllers/sources/source_html/index.html.heex +++ b/lib/pinchflat_web/controllers/sources/source_html/index.html.heex @@ -12,32 +12,7 @@
- <.table rows={@sources} table_class="text-black dark:text-white"> - <:col :let={source} label="Name"> - <.subtle_link href={~p"/sources/#{source.id}"}> - <%= StringUtils.truncate(source.custom_name || source.collection_name, 35) %> - - - <:col :let={source} label="Type"><%= source.collection_type %> - <:col :let={source} label="Pending"><.localized_number number={source.pending_count} /> - <:col :let={source} label="Downloaded"><.localized_number number={source.downloaded_count} /> - <:col :let={source} label="Retention"> - <%= if source.retention_period_days && source.retention_period_days > 0 do %> - <.localized_number number={source.retention_period_days} /> - <.pluralize count={source.retention_period_days} word="day" /> - <% else %> - - <% end %> - - <:col :let={source} label="Media Profile"> - <.subtle_link href={~p"/media_profiles/#{source.media_profile_id}"}> - <%= source.media_profile.name %> - - - <:col :let={source} label="" class="flex place-content-evenly"> - <.icon_link href={~p"/sources/#{source.id}/edit"} icon="hero-pencil-square" class="mx-1" /> - - + <%= live_render(@conn, PinchflatWeb.Sources.IndexTableLive) %>
diff --git a/lib/pinchflat_web/controllers/sources/source_html/index_table_live.ex b/lib/pinchflat_web/controllers/sources/source_html/index_table_live.ex new file mode 100644 index 0000000..47b5e69 --- /dev/null +++ b/lib/pinchflat_web/controllers/sources/source_html/index_table_live.ex @@ -0,0 +1,95 @@ +defmodule PinchflatWeb.Sources.IndexTableLive do + use PinchflatWeb, :live_view + use Pinchflat.Media.MediaQuery + use Pinchflat.Sources.SourcesQuery + + alias Pinchflat.Repo + alias Pinchflat.Sources.Source + alias Pinchflat.Media.MediaItem + + def render(assigns) do + ~H""" + <.table rows={@sources} table_class="text-black dark:text-white"> + <:col :let={source} label="Name"> + <.subtle_link href={~p"/sources/#{source.id}"}> + <%= StringUtils.truncate(source.custom_name || source.collection_name, 35) %> + + + <:col :let={source} label="Pending"><.localized_number number={source.pending_count} /> + <:col :let={source} label="Downloaded"><.localized_number number={source.downloaded_count} /> + <:col :let={source} label="Retention"> + <%= if source.retention_period_days && source.retention_period_days > 0 do %> + <.localized_number number={source.retention_period_days} /> + <.pluralize count={source.retention_period_days} word="day" /> + <% else %> + + <% end %> + + <:col :let={source} label="Media Profile"> + <.subtle_link href={~p"/media_profiles/#{source.media_profile_id}"}> + <%= source.media_profile.name %> + + + <:col :let={source} label="Enabled?"> + <.input + name={"source[#{source.id}][enabled]"} + id={"source_#{source.id}_enabled"} + phx-hook="formless-input" + data-subscribe="change" + data-event-name="toggle_enabled" + data-identifier={source.id} + type="toggle" + /> + + <:col :let={source} label="" class="flex place-content-evenly"> + <.icon_link href={~p"/sources/#{source.id}/edit"} icon="hero-pencil-square" class="mx-1" /> + + + """ + end + + def mount(_params, _session, socket) do + new_assigns = %{ + sources: get_sources() + } + + {:ok, assign(socket, new_assigns)} + end + + def handle_event("formless-input", %{"event" => "toggle_enabled"} = params, socket) do + # should_enable = params["value"] == "true" + IO.inspect(params) + + {:noreply, socket} + end + + defp get_sources do + source_query = + from s in Source, + as: :source, + inner_join: mp in assoc(s, :media_profile), + where: is_nil(s.marked_for_deletion_at) and is_nil(mp.marked_for_deletion_at), + preload: [media_profile: mp], + order_by: [asc: s.custom_name], + select: map(s, ^Source.__schema__(:fields)), + 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) + ) + } + + Repo.all(source_query) + end +end