From 37ef79e6ef11e435c98787b1b3841a8a457c8af6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionu=C8=9B=20Staicu?= Date: Sat, 7 Jun 2025 14:38:12 +0300 Subject: [PATCH] Add download in-page without page refresh --- .../pages/page_html/history_table_live.ex | 25 +++++++++++++++- .../source_html/media_item_table_live.ex | 30 +++++++++++++------ 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/lib/pinchflat_web/controllers/pages/page_html/history_table_live.ex b/lib/pinchflat_web/controllers/pages/page_html/history_table_live.ex index 6c104aa..2e5ee59 100644 --- a/lib/pinchflat_web/controllers/pages/page_html/history_table_live.ex +++ b/lib/pinchflat_web/controllers/pages/page_html/history_table_live.ex @@ -6,6 +6,9 @@ defmodule Pinchflat.Pages.HistoryTableLive do alias Pinchflat.Utils.NumberUtils alias PinchflatWeb.CustomComponents.TextComponents + alias Pinchflat.Media + alias Pinchflat.Downloading.MediaDownloadWorker + @limit 5 def render(%{records: []} = assigns) do @@ -29,7 +32,7 @@ defmodule Pinchflat.Pages.HistoryTableLive do
<.table rows={@records} table_class="text-white"> <:col :let={media_item} label="Title" class="max-w-xs"> -
+
<.tooltip :if={media_item.last_error} tooltip={media_item.last_error} @@ -38,6 +41,17 @@ defmodule Pinchflat.Pages.HistoryTableLive do > <.icon name="hero-exclamation-circle-solid" class="text-red-500" /> + + <.icon_button + icon_name="hero-arrow-down-tray" + class="h-10 w-10" + phx-click="force_download" + phx-value-source-id={media_item.source_id} + phx-value-media-id={media_item.id} + data-confirm="Are you sure you force a download of this media?" + :if={media_item.media_downloaded_at === nil} + /> + <.subtle_link href={~p"/sources/#{media_item.source_id}/media/#{media_item.id}"}> {media_item.title} @@ -90,6 +104,15 @@ defmodule Pinchflat.Pages.HistoryTableLive do {:noreply, assign(socket, new_assigns)} end + def handle_event("force_download", %{ "source-id" => source_id, "media-id" => media_id }, socket) do + IO.puts("source_id: #{source_id}, media_id: #{media_id}") + + media_item = Media.get_media_item!(media_id) + MediaDownloadWorker.kickoff_with_task(media_item, %{force: true}) + + {:noreply, socket} + end + defp fetch_pagination_attributes(base_query, page) do total_record_count = Repo.aggregate(base_query, :count, :id) total_pages = max(ceil(total_record_count / @limit), 1) 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 3a97870..203a35f 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 @@ -6,6 +6,9 @@ defmodule PinchflatWeb.Sources.MediaItemTableLive do alias Pinchflat.Sources alias Pinchflat.Utils.NumberUtils + alias Pinchflat.Media + alias Pinchflat.Downloading.MediaDownloadWorker + @limit 10 def render(%{total_record_count: 0} = assigns) do @@ -47,7 +50,7 @@ defmodule PinchflatWeb.Sources.MediaItemTableLive do <.table rows={@records} table_class="text-white"> <:col :let={media_item} label="Title" class="max-w-xs"> -
+
<.tooltip :if={media_item.last_error} tooltip={media_item.last_error} @@ -57,15 +60,15 @@ defmodule PinchflatWeb.Sources.MediaItemTableLive do <.icon name="hero-exclamation-circle-solid" class="text-red-500" /> - <.link - href={~p"/sources/#{@source.id}/media/#{media_item.id}/force_download"} - :if={@media_state !== "downloaded"} - method="post" - target="_blank" + <.icon_button + icon_name="hero-arrow-down-tray" + class="h-10 w-10" + phx-click="force_download" + phx-value-source-id={@source.id} + phx-value-media-id={media_item.id} data-confirm="Are you sure you force a download of this media?" - > - <.icon name="hero-arrow-down-tray" /> - + :if={@media_state !== "downloaded"} + /> <.subtle_link href={~p"/sources/#{@source.id}/media/#{media_item.id}"}> @@ -128,6 +131,15 @@ defmodule PinchflatWeb.Sources.MediaItemTableLive do {:noreply, assign(socket, new_assigns)} end + def handle_event("force_download", %{ "source-id" => source_id, "media-id" => media_id }, socket) do + IO.puts("source_id: #{source_id}, media_id: #{media_id}") + + media_item = Media.get_media_item!(media_id) + MediaDownloadWorker.kickoff_with_task(media_item, %{force: true}) + + {:noreply, socket} + end + # This, along with the handle_info below, is a pattern to reload _all_ # tables on page rather than just the one that triggered the reload. def handle_event("reload_page", _params, socket) do