Skip to content

Commit 6e4633a

Browse files
committed
feat: add confirmation modal to delete redirect action
1 parent 04fe568 commit 6e4633a

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

lib/cesium_link_web/live/redirect_live/index.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ defmodule CesiumLinkWeb.RedirectLive.Index do
2020
|> assign(:redirect, Redirects.get_redirect!(id))
2121
end
2222

23+
defp apply_action(socket, :delete, %{"id" => id}) do
24+
socket
25+
|> assign(:page_title, "Delete Redirect")
26+
|> assign(:redirect, Redirects.get_redirect!(id))
27+
end
28+
2329
defp apply_action(socket, :new, _params) do
2430
socket
2531
|> assign(:page_title, "New Redirect")
@@ -42,7 +48,10 @@ defmodule CesiumLinkWeb.RedirectLive.Index do
4248
redirect = Redirects.get_redirect!(id)
4349
{:ok, _} = Redirects.delete_redirect(redirect)
4450

45-
{:noreply, stream_delete(socket, :redirects, redirect)}
51+
{:noreply,
52+
stream_delete(socket, :redirects, redirect)
53+
|> push_patch(to: ~p"/admin/redirects")
54+
|> put_flash(:info, "Redirect deleted successfully")}
4655
end
4756

4857
defp get_redirect_url(%Redirect{slug: slug, type: type}) do

lib/cesium_link_web/live/redirect_live/index.html.heex

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
<.icon name="hero-pencil" class="w-5 h-5" />
3838
</.link>
3939
</:action>
40-
<:action :let={{id, redirect}}>
41-
<.link phx-click={JS.push("delete", value: %{id: redirect.id}) |> hide("##{id}")} data-confirm="Are you sure?">
40+
<:action :let={{_id, redirect}}>
41+
<.link patch={~p"/admin/redirects/#{redirect}/delete"}>
4242
<.icon name="hero-trash" class="w-5 h-5" />
4343
</.link>
4444
</:action>
@@ -47,3 +47,24 @@
4747
<.modal :if={@live_action in [:new, :edit]} id="redirect-modal" show on_cancel={JS.patch(~p"/admin/redirects")}>
4848
<.live_component module={CesiumLinkWeb.RedirectLive.FormComponent} id={@redirect.id || :new} title={@page_title} action={@live_action} redirect={@redirect} patch={~p"/admin/redirects"} />
4949
</.modal>
50+
51+
<.modal :if={@live_action == :delete} id="delete-modal" show on_cancel={JS.patch(~p"/admin/redirects")}>
52+
<div>
53+
<.header>
54+
<%= gettext("Delete Redirect") %>
55+
<:subtitle>
56+
<%= gettext("Are you sure you want to delete this redirect? This action is irreversible.") %>
57+
</:subtitle>
58+
</.header>
59+
<div class="mt-8 w-full gap-2 flex flex-row">
60+
<.link patch={~p"/admin/redirects"} class="w-full outline-none">
61+
<.button class="w-full">
62+
<%= gettext("Cancel") %>
63+
</.button>
64+
</.link>
65+
<.button phx-click="delete" phx-value-id={@redirect.id} class="w-full">
66+
<%= gettext("Confirm") %>
67+
</.button>
68+
</div>
69+
</div>
70+
</.modal>

lib/cesium_link_web/router.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ defmodule CesiumLinkWeb.Router do
8585
live "/redirects", RedirectLive.Index, :index
8686
live "/redirects/new", RedirectLive.Index, :new
8787
live "/redirects/:id/edit", RedirectLive.Index, :edit
88+
live "/redirects/:id/delete", RedirectLive.Index, :delete
8889

8990
live "/qrcode", QRCodeLive.Index, :index
9091
end

0 commit comments

Comments
 (0)