Skip to content

Commit

Permalink
deletion button for works
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Mar 30, 2024
1 parent 799f59b commit 5ee92e7
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 16 deletions.
17 changes: 9 additions & 8 deletions lib/banchan/offerings/offerings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/banchan/uploads/uploads.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion lib/banchan/workers/pruner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 8 additions & 1 deletion lib/banchan/works/works.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/banchan_web/components/collapse.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule BanchanWeb.Components.Collapse do

def render(assigns) do
~F"""
<div class={"flex flex-col gap-2", @class}>
<bc-collapse class={"flex flex-col gap-2", @class}>
{#if slot_assigned?(:header)}
<div class="flex flex-row items-center cursor-pointer" :on-click="toggle">
<div class="py-2 grow">
Expand All @@ -52,7 +52,7 @@ defmodule BanchanWeb.Components.Collapse do
<div class={hidden: !@open}>
<#slot />
</div>
</div>
</bc-collapse>
"""
end
end
4 changes: 4 additions & 0 deletions lib/banchan_web/live/work_live/work.css
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,7 @@
@apply opacity-75;
}
}

:global(.work-container) :deep(.delete-button) {
@apply w-full;
}
22 changes: 22 additions & 0 deletions lib/banchan_web/live/work_live/work.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +27,7 @@ defmodule BanchanWeb.WorkLive.Work do
alias Surface.Components.Form.{ErrorTag, Field}

alias BanchanWeb.Components.{
Collapse,
Icon,
Layout,
RichText,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -451,6 +466,13 @@ defmodule BanchanWeb.WorkLive.Work do
<Submit label="Save" changeset={@changeset} />
<LivePatch class="btn btn-error" to={~p"/studios/#{@studio.handle}/works/#{@work.public_id}"}>Cancel</LivePatch>
</div>
<Collapse id="delete-work">
<:header>
Delete Work
</:header>
<span>This work and all its uploads will be permanently deleted. This action cannot be undone. Are you sure?</span>
<Button class="btn btn-error delete-button" click="delete_work">Confirm Deletion</Button>
</Collapse>
{/if}
</div>
{/if}
Expand Down

0 comments on commit 5ee92e7

Please sign in to comment.