|
|
@ -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
|
|
|
|
AshPhoenix.Form.for_create(DecentralisedBookIndex.Metadata.Book, :create,
|
|
|
|
form =
|
|
|
|
as: "book",
|
|
|
|
AshPhoenix.Form.for_create(DecentralisedBookIndex.Metadata.Book, :create,
|
|
|
|
actor: socket.assigns.current_user
|
|
|
|
as: "book",
|
|
|
|
)
|
|
|
|
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}"
|
|
|
|