From 69612a0d4a4f1a290eea77b935790541a1563df5 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Wed, 26 Mar 2025 12:42:47 +0200 Subject: [PATCH] Add the cancel button to Author's FormComponent. --- .../live/author_live/form_component.ex | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/decentralised_book_index_web/live/author_live/form_component.ex b/lib/decentralised_book_index_web/live/author_live/form_component.ex index f9c43ee..35e540e 100644 --- a/lib/decentralised_book_index_web/live/author_live/form_component.ex +++ b/lib/decentralised_book_index_web/live/author_live/form_component.ex @@ -48,6 +48,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do <:actions> <.button phx-disable-with="Saving...">Save Author + <.button phx-click="cancel" phx-target={@myself} class="me-auto">Cancel @@ -84,6 +85,17 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do end end + def handle_event("cancel", _params, socket) do + author = socket.assigns.author + author_id = if author == nil, do: nil, else: author.id + + socket = + socket + |> push_redirect(to: patch_url(socket.assigns.action, author_id)) + + {:noreply, socket} + end + defp notify_parent(msg), do: send(self(), {__MODULE__, msg}) defp assign_form(%{assigns: %{author: author}} = socket) do