From 5ee92e7e563f7a866bfbbc3c7a5219db92301901 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Sat, 30 Mar 2024 14:33:40 -0700 Subject: [PATCH] deletion button for works --- lib/banchan/offerings/offerings.ex | 17 +++++++++-------- lib/banchan/uploads/uploads.ex | 8 ++++---- lib/banchan/workers/pruner.ex | 4 +++- lib/banchan/works/works.ex | 9 ++++++++- lib/banchan_web/components/collapse.ex | 4 ++-- lib/banchan_web/live/work_live/work.css | 4 ++++ lib/banchan_web/live/work_live/work.ex | 22 ++++++++++++++++++++++ 7 files changed, 52 insertions(+), 16 deletions(-) diff --git a/lib/banchan/offerings/offerings.ex b/lib/banchan/offerings/offerings.ex index 24176b40..ae24f178 100644 --- a/lib/banchan/offerings/offerings.ex +++ b/lib/banchan/offerings/offerings.ex @@ -801,14 +801,15 @@ defmodule Banchan.Offerings do def prune_offerings do now = NaiveDateTime.utc_now() - {:ok, {count, _}} = Repo.transaction(fn -> - from( - o in Offering, - where: not is_nil(o.deleted_at), - where: o.deleted_at < datetime_add(^now, -30, "day") - ) - |> Repo.delete_all() - end) + {:ok, {count, _}} = + Repo.transaction(fn -> + from( + o in Offering, + where: not is_nil(o.deleted_at), + where: o.deleted_at < datetime_add(^now, -30, "day") + ) + |> Repo.delete_all() + end) {:ok, count} end diff --git a/lib/banchan/uploads/uploads.ex b/lib/banchan/uploads/uploads.ex index 9d268be0..fd9b78fc 100644 --- a/lib/banchan/uploads/uploads.ex +++ b/lib/banchan/uploads/uploads.ex @@ -197,10 +197,10 @@ defmodule Banchan.Uploads do end end) - from( - u in Upload, - where: u.id not in subquery(sq) - ) + from( + u in Upload, + where: u.id not in subquery(sq) + ) |> Repo.delete_all() end) diff --git a/lib/banchan/workers/pruner.ex b/lib/banchan/workers/pruner.ex index a217a5b1..386c0bc9 100644 --- a/lib/banchan/workers/pruner.ex +++ b/lib/banchan/workers/pruner.ex @@ -23,7 +23,9 @@ defmodule Banchan.Workers.Pruner do {:ok, stats} -> Logger.info("Prune job completed successfully: #{inspect(stats)}") :ok - {:error, _, error, _} -> {:error, error} + + {:error, _, error, _} -> + {:error, error} end end end diff --git a/lib/banchan/works/works.ex b/lib/banchan/works/works.ex index adc3f64f..dc6606e2 100644 --- a/lib/banchan/works/works.ex +++ b/lib/banchan/works/works.ex @@ -511,5 +511,12 @@ defmodule Banchan.Works do ret end - ## TODO + ## Deletion + + @doc """ + Delete a Work permanently. + """ + def delete_work(%Work{} = work) do + Repo.delete(work) + end end diff --git a/lib/banchan_web/components/collapse.ex b/lib/banchan_web/components/collapse.ex index 9dfc1216..5599daf4 100644 --- a/lib/banchan_web/components/collapse.ex +++ b/lib/banchan_web/components/collapse.ex @@ -36,7 +36,7 @@ defmodule BanchanWeb.Components.Collapse do def render(assigns) do ~F""" -
+ {#if slot_assigned?(:header)}
@@ -52,7 +52,7 @@ defmodule BanchanWeb.Components.Collapse do
<#slot />
-
+ """ end end diff --git a/lib/banchan_web/live/work_live/work.css b/lib/banchan_web/live/work_live/work.css index 3cab715b..48e67180 100644 --- a/lib/banchan_web/live/work_live/work.css +++ b/lib/banchan_web/live/work_live/work.css @@ -127,3 +127,7 @@ @apply opacity-75; } } + +:global(.work-container) :deep(.delete-button) { + @apply w-full; +} diff --git a/lib/banchan_web/live/work_live/work.ex b/lib/banchan_web/live/work_live/work.ex index f1587a7f..661ddd63 100644 --- a/lib/banchan_web/live/work_live/work.ex +++ b/lib/banchan_web/live/work_live/work.ex @@ -5,8 +5,11 @@ defmodule BanchanWeb.WorkLive.Work do """ use BanchanWeb, :live_view + require Logger + import BanchanWeb.StudioLive.Helpers + alias BanchanWeb.Components.Button alias Banchan.Commissions alias Banchan.Offerings alias Banchan.Repo @@ -24,6 +27,7 @@ defmodule BanchanWeb.WorkLive.Work do alias Surface.Components.Form.{ErrorTag, Field} alias BanchanWeb.Components.{ + Collapse, Icon, Layout, RichText, @@ -296,6 +300,17 @@ defmodule BanchanWeb.WorkLive.Work do end end + def handle_event("delete_work", _, socket) do + case Works.delete_work(socket.assigns.work) do + {:ok, _} -> + {:noreply, redirect(socket, to: ~p"/studios/#{socket.assigns.studio.handle}")} + + {:error, err} -> + Logger.error("Failed to delete work: #{inspect(err)}") + {:noreply, socket |> put_flash(:error, "Failed to delete work.")} + end + end + def handle_info({:updated_uploads, _, uploads}, socket) do uploads_param = uploads @@ -451,6 +466,13 @@ defmodule BanchanWeb.WorkLive.Work do Cancel
+ + <:header> + Delete Work + + This work and all its uploads will be permanently deleted. This action cannot be undone. Are you sure? + + {/if}
{/if}