From 80207083be9303bf60bb1ed4454b31a0a7fd40ad Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Thu, 23 May 2024 09:17:53 -0700 Subject: [PATCH] Added tab for other (non-downloaded and non-pending) media --- .../source_html/media_item_table_live.ex | 21 +++++++++++++++++- .../sources/source_html/show.html.heex | 13 ++++++++--- .../sources/media_item_table_live_test.exs | 22 +++++++++++++++---- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/lib/pinchflat_web/controllers/sources/source_html/media_item_table_live.ex b/lib/pinchflat_web/controllers/sources/source_html/media_item_table_live.ex index 7ffac3c..d15b20d 100644 --- a/lib/pinchflat_web/controllers/sources/source_html/media_item_table_live.ex +++ b/lib/pinchflat_web/controllers/sources/source_html/media_item_table_live.ex @@ -47,6 +47,7 @@ defmodule Pinchflat.Sources.MediaItemTableLive do source = Sources.get_source!(session["source_id"]) base_query = generate_base_query(source, media_state) pagination_attrs = fetch_pagination_attributes(base_query, page) + PinchflatWeb.Endpoint.subscribe("media_table") {:ok, assign(socket, Map.merge(pagination_attrs, %{base_query: base_query, source: source}))} end @@ -59,7 +60,13 @@ defmodule Pinchflat.Sources.MediaItemTableLive do {:noreply, assign(socket, new_assigns)} end - def handle_event("reload_page", _params, %{assigns: assigns} = socket) do + def handle_event("reload_page", _params, socket) do + PinchflatWeb.Endpoint.broadcast("media_table", "reload", nil) + + {:noreply, socket} + end + + def handle_info(%{topic: "media_table", event: "reload"}, %{assigns: assigns} = socket) do new_assigns = fetch_pagination_attributes(assigns.base_query, assigns.page) {:noreply, assign(socket, new_assigns)} @@ -95,4 +102,16 @@ defmodule Pinchflat.Sources.MediaItemTableLive do |> where(^dynamic(^MediaQuery.for_source(source) and ^MediaQuery.downloaded())) |> order_by(desc: :id) end + + defp generate_base_query(source, "other") do + MediaQuery.new() + |> MediaQuery.require_assoc(:media_profile) + |> where( + ^dynamic( + ^MediaQuery.for_source(source) and + (not (^MediaQuery.downloaded()) and not (^MediaQuery.pending())) + ) + ) + |> order_by(desc: :id) + end end diff --git a/lib/pinchflat_web/controllers/sources/source_html/show.html.heex b/lib/pinchflat_web/controllers/sources/source_html/show.html.heex index 00ad375..26fdc96 100644 --- a/lib/pinchflat_web/controllers/sources/source_html/show.html.heex +++ b/lib/pinchflat_web/controllers/sources/source_html/show.html.heex @@ -36,21 +36,28 @@ <.list_items_from_map map={Map.from_struct(@source)} /> - <:tab title="Pending Media" id="pending"> + <:tab title="Pending" id="pending"> <%= live_render( @conn, Pinchflat.Sources.MediaItemTableLive, session: %{"source_id" => @source.id, "media_state" => "pending"} ) %> - <:tab title="Downloaded Media" id="downloaded"> + <:tab title="Downloaded" id="downloaded"> <%= live_render( @conn, Pinchflat.Sources.MediaItemTableLive, session: %{"source_id" => @source.id, "media_state" => "downloaded"} ) %> - <:tab title="Pending Tasks" id="tasks"> + <:tab title="Other" id="other"> + <%= live_render( + @conn, + Pinchflat.Sources.MediaItemTableLive, + session: %{"source_id" => @source.id, "media_state" => "other"} + ) %> + + <:tab title="Tasks" id="tasks"> <%= if match?([_|_], @pending_tasks) do %> <.table rows={@pending_tasks} table_class="text-black dark:text-white"> <:col :let={task} label="Worker"> diff --git a/test/pinchflat_web/controllers/sources/media_item_table_live_test.exs b/test/pinchflat_web/controllers/sources/media_item_table_live_test.exs index da873d6..183712a 100644 --- a/test/pinchflat_web/controllers/sources/media_item_table_live_test.exs +++ b/test/pinchflat_web/controllers/sources/media_item_table_live_test.exs @@ -4,6 +4,7 @@ defmodule PinchflatWeb.Sources.MediaItemTableLiveTest do import Phoenix.LiveViewTest import Pinchflat.MediaFixtures import Pinchflat.SourcesFixtures + import Pinchflat.ProfilesFixtures alias Pinchflat.Sources.MediaItemTableLive @@ -34,10 +35,8 @@ defmodule PinchflatWeb.Sources.MediaItemTableLiveTest do describe "media_state" do test "shows pending media when pending", %{conn: conn, source: source} do - downloaded_media_item = media_item_fixture(source_id: source.id, title: "DL-#{Enum.random(0..9999)}") - - pending_media_item = - media_item_fixture(source_id: source.id, media_filepath: nil, title: "P-#{Enum.random(0..9999)}") + downloaded_media_item = media_item_fixture(source_id: source.id) + pending_media_item = media_item_fixture(source_id: source.id, media_filepath: nil) {:ok, _view, html} = live_isolated(conn, MediaItemTableLive, session: create_session(source, "pending")) @@ -54,6 +53,21 @@ defmodule PinchflatWeb.Sources.MediaItemTableLiveTest do assert html =~ downloaded_media_item.title refute html =~ pending_media_item.title end + + test "shows records that aren't pending or downloaded when other", %{conn: conn} do + media_profile = media_profile_fixture(shorts_behaviour: :exclude) + source = source_fixture(media_profile_id: media_profile.id) + + downloaded_media_item = media_item_fixture(source_id: source.id) + pending_media_item = media_item_fixture(source_id: source.id, media_filepath: nil) + other_media_item = media_item_fixture(source_id: source.id, media_filepath: nil, short_form_content: true) + + {:ok, _view, html} = live_isolated(conn, MediaItemTableLive, session: create_session(source, "other")) + + assert html =~ other_media_item.title + refute html =~ downloaded_media_item.title + refute html =~ pending_media_item.title + end end defp create_session(source, media_state \\ "pending") do