diff --git a/lib/decentralised_book_index_web/live/author_live/edit.ex b/lib/decentralised_book_index_web/live/author_live/edit.ex index 8e16cd5..421702c 100644 --- a/lib/decentralised_book_index_web/live/author_live/edit.ex +++ b/lib/decentralised_book_index_web/live/author_live/edit.ex @@ -13,14 +13,18 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Edit do current_user={@current_user} action={@live_action} author={@author} + alternative_author_id={@alternative_author_id} /> """ end @impl true - def mount(_params, _session, socket) do + def mount(params, _session, socket) do + alternative_author_id = Map.get(params, "alternative_author_id", nil) + {:ok, socket + |> assign(:alternative_author_id, alternative_author_id) |> assign_new(:current_user, fn -> nil end)} end 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 19f2687..bf77fa3 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 @@ -1,6 +1,8 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do use DecentralisedBookIndexWeb, :live_component + alias DecentralisedBookIndex.Metadata + @impl true def render(assigns) do ~H""" @@ -101,7 +103,15 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do defp notify_parent(msg), do: send(self(), {__MODULE__, msg}) - defp assign_form(%{assigns: %{author: author}} = socket) do + defp assign_form(%{assigns: %{author: author, alternative_author_id: alternative_author_id}} = socket) do + alias_id = + if alternative_author_id == nil do + nil + else + alternative_author = Metadata.get_author_by_id!(alternative_author_id) + alternative_author.author_alias_registry_id + end + form = if author do AshPhoenix.Form.for_update(author, :update, @@ -109,15 +119,26 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do actor: socket.assigns.current_user ) else - AshPhoenix.Form.for_create(Metadata.Author, :create, - as: "author", - actor: socket.assigns.current_user - ) + form = + AshPhoenix.Form.for_create(Metadata.Author, :create, + as: "author", + actor: socket.assigns.current_user + ) + |> set_author_alias_registry_id(alias_id) end assign(socket, form: to_form(form)) end + defp set_author_alias_registry_id(form, alias_id) do + if alias_id != nil do + form + |> AshPhoenix.Form.update_params(&Map.put(&1, "author_alias_registry_id", alias_id)) + else + form + end + end + defp patch_url(action, author_id) do case action do :edit -> ~p"/authors/#{author_id}" diff --git a/lib/decentralised_book_index_web/live/author_live/show.ex b/lib/decentralised_book_index_web/live/author_live/show.ex index 1cdaa95..5244bdb 100644 --- a/lib/decentralised_book_index_web/live/author_live/show.ex +++ b/lib/decentralised_book_index_web/live/author_live/show.ex @@ -11,11 +11,18 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do <:actions> <%= if @current_user != nil and Role.can_moderate?(@current_user.role) do %> - <.link patch={~p"/authors/#{@author}/edit"}> - <.edit_button> - Edit - - +
+ <.link patch={~p"/authors/#{@author}/new"}> + <.add_button> + Alias + + + <.link patch={~p"/authors/#{@author}/edit"}> + <.edit_button> + Edit + + +
<% end %> diff --git a/lib/decentralised_book_index_web/router.ex b/lib/decentralised_book_index_web/router.ex index 1c2175d..891734c 100644 --- a/lib/decentralised_book_index_web/router.ex +++ b/lib/decentralised_book_index_web/router.ex @@ -40,6 +40,7 @@ defmodule DecentralisedBookIndexWeb.Router do live "/books/:id/edit", BookLive.Edit, :edit live "/authors/new", AuthorLive.Edit, :new + live "/authors/:alternative_author_id/new", AuthorLive.Edit, :new live "/authors/:id/edit", AuthorLive.Edit, :edit live "/publishers", PublisherLive.Index, :index