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}
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

@ -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}"

@ -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
</.edit_button>
</.link>
<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"}>
<.edit_button>
Edit
</.edit_button>
</.link>
</div>
<% end %>
</:actions>
</.header>

@ -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

Loading…
Cancel
Save