Add the add edition button for Book Show LiveView.

dev
KKlochko 2 months ago
parent d7e0a2117c
commit 8b35c9b060

@ -11,14 +11,18 @@ defmodule DecentralisedBookIndexWeb.BookLive.Edit do
current_user={@current_user} current_user={@current_user}
action={@live_action} action={@live_action}
book={@book} book={@book}
alternative_book_id={@alternative_book_id}
/> />
""" """
end end
@impl true @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, {:ok,
socket socket
|> assign(:alternative_book_id, alternative_book_id)
|> assign_new(:current_user, fn -> nil end)} |> assign_new(:current_user, fn -> nil end)}
end end

@ -1,6 +1,8 @@
defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
use DecentralisedBookIndexWeb, :live_component use DecentralisedBookIndexWeb, :live_component
alias DecentralisedBookIndex.Metadata
@impl true @impl true
def render(assigns) do def render(assigns) do
~H""" ~H"""
@ -340,7 +342,15 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
defp notify_parent(msg), do: send(self(), {__MODULE__, msg}) 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 = form =
if book do if book do
book = book =
@ -349,15 +359,26 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
AshPhoenix.Form.for_update(book, :update, as: "book", actor: socket.assigns.current_user) AshPhoenix.Form.for_update(book, :update, as: "book", actor: socket.assigns.current_user)
else else
form =
AshPhoenix.Form.for_create(DecentralisedBookIndex.Metadata.Book, :create, AshPhoenix.Form.for_create(DecentralisedBookIndex.Metadata.Book, :create,
as: "book", as: "book",
actor: socket.assigns.current_user actor: socket.assigns.current_user
) )
|> set_book_edition_registry_id(alternative_edition_id)
end end
assign(socket, form: to_form(form)) assign(socket, form: to_form(form))
end 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 defp patch_url(action, book_id) do
case action do case action do
:edit -> ~p"/books/#{book_id}" :edit -> ~p"/books/#{book_id}"

@ -15,11 +15,18 @@ defmodule DecentralisedBookIndexWeb.BookLive.Show do
<:actions> <:actions>
<%= if @current_user != nil and Role.can_moderate?(@current_user.role) do %> <%= if @current_user != nil and Role.can_moderate?(@current_user.role) do %>
<div class="flex flex-row gap-2">
<.link patch={~p"/books/#{@book}/new"}>
<.add_button>
Edition
</.add_button>
</.link>
<.link patch={~p"/books/#{@book}/edit"}> <.link patch={~p"/books/#{@book}/edit"}>
<.edit_button> <.edit_button>
Edit Edit
</.edit_button> </.edit_button>
</.link> </.link>
</div>
<% end %> <% end %>
</:actions> </:actions>
</.header> </.header>

@ -37,6 +37,7 @@ defmodule DecentralisedBookIndexWeb.Router do
ash_authentication_live_session :moderator_authenticated_routes, on_mount: {DecentralisedBookIndexWeb.LiveUserAuth, :moderator_required} do ash_authentication_live_session :moderator_authenticated_routes, on_mount: {DecentralisedBookIndexWeb.LiveUserAuth, :moderator_required} do
live "/books/new", BookLive.Edit, :new live "/books/new", BookLive.Edit, :new
live "/books/:alternative_book_id/new", BookLive.Edit, :new
live "/books/:id/edit", BookLive.Edit, :edit live "/books/:id/edit", BookLive.Edit, :edit
live "/authors/new", AuthorLive.Edit, :new live "/authors/new", AuthorLive.Edit, :new

Loading…
Cancel
Save