From 8b35c9b060ad9cedce635619724e6b15e54967cc Mon Sep 17 00:00:00 2001 From: KKlochko Date: Mon, 21 Apr 2025 13:21:39 +0300 Subject: [PATCH] Add the add edition button for Book Show LiveView. --- .../live/book_live/edit.ex | 6 +++- .../live/book_live/form_component.ex | 31 ++++++++++++++++--- .../live/book_live/show.ex | 17 +++++++--- lib/decentralised_book_index_web/router.ex | 1 + 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/lib/decentralised_book_index_web/live/book_live/edit.ex b/lib/decentralised_book_index_web/live/book_live/edit.ex index 9ff2b7e..21f56b3 100644 --- a/lib/decentralised_book_index_web/live/book_live/edit.ex +++ b/lib/decentralised_book_index_web/live/book_live/edit.ex @@ -11,14 +11,18 @@ defmodule DecentralisedBookIndexWeb.BookLive.Edit do current_user={@current_user} action={@live_action} book={@book} + alternative_book_id={@alternative_book_id} /> """ end @impl true - def mount(_params, _session, socket) do + def mount(params, _session, socket) do + alternative_book_id = Map.get(params, "alternative_book_id", nil) + {:ok, socket + |> assign(:alternative_book_id, alternative_book_id) |> assign_new(:current_user, fn -> nil end)} end diff --git a/lib/decentralised_book_index_web/live/book_live/form_component.ex b/lib/decentralised_book_index_web/live/book_live/form_component.ex index e1cf36e..5fbfafa 100644 --- a/lib/decentralised_book_index_web/live/book_live/form_component.ex +++ b/lib/decentralised_book_index_web/live/book_live/form_component.ex @@ -1,6 +1,8 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do use DecentralisedBookIndexWeb, :live_component + alias DecentralisedBookIndex.Metadata + @impl true def render(assigns) do ~H""" @@ -340,7 +342,15 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do defp notify_parent(msg), do: send(self(), {__MODULE__, msg}) - defp assign_form(%{assigns: %{book: book}} = socket) do + defp assign_form(%{assigns: %{book: book, alternative_book_id: alternative_book_id}} = socket) do + alternative_edition_id = + if alternative_book_id == nil do + nil + else + alternative_book = Metadata.get_book_by_id!(alternative_book_id) + alternative_book.book_editions_registry_id + end + form = if book do book = @@ -349,15 +359,26 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do AshPhoenix.Form.for_update(book, :update, as: "book", actor: socket.assigns.current_user) else - AshPhoenix.Form.for_create(DecentralisedBookIndex.Metadata.Book, :create, - as: "book", - actor: socket.assigns.current_user - ) + form = + AshPhoenix.Form.for_create(DecentralisedBookIndex.Metadata.Book, :create, + as: "book", + actor: socket.assigns.current_user + ) + |> set_book_edition_registry_id(alternative_edition_id) end assign(socket, form: to_form(form)) end + defp set_book_edition_registry_id(form, alternative_edition_id) do + if alternative_edition_id != nil do + form + |> AshPhoenix.Form.update_params(&Map.put(&1, "book_editions_registry_id", alternative_edition_id)) + else + form + end + end + defp patch_url(action, book_id) do case action do :edit -> ~p"/books/#{book_id}" diff --git a/lib/decentralised_book_index_web/live/book_live/show.ex b/lib/decentralised_book_index_web/live/book_live/show.ex index 2b7d27f..10840d9 100644 --- a/lib/decentralised_book_index_web/live/book_live/show.ex +++ b/lib/decentralised_book_index_web/live/book_live/show.ex @@ -15,11 +15,18 @@ defmodule DecentralisedBookIndexWeb.BookLive.Show do <:actions> <%= if @current_user != nil and Role.can_moderate?(@current_user.role) do %> - <.link patch={~p"/books/#{@book}/edit"}> - <.edit_button> - Edit - - +
+ <.link patch={~p"/books/#{@book}/new"}> + <.add_button> + Edition + + + <.link patch={~p"/books/#{@book}/edit"}> + <.edit_button> + Edit + + +
<% end %> diff --git a/lib/decentralised_book_index_web/router.ex b/lib/decentralised_book_index_web/router.ex index 891734c..37d700b 100644 --- a/lib/decentralised_book_index_web/router.ex +++ b/lib/decentralised_book_index_web/router.ex @@ -37,6 +37,7 @@ defmodule DecentralisedBookIndexWeb.Router do ash_authentication_live_session :moderator_authenticated_routes, on_mount: {DecentralisedBookIndexWeb.LiveUserAuth, :moderator_required} do live "/books/new", BookLive.Edit, :new + live "/books/:alternative_book_id/new", BookLive.Edit, :new live "/books/:id/edit", BookLive.Edit, :edit live "/authors/new", AuthorLive.Edit, :new