Add the add alias button for Author Show LiveView.

dev
KKlochko 2 months ago
parent dcc3d43387
commit d7e0a2117c

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

@ -1,6 +1,8 @@
defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do defmodule DecentralisedBookIndexWeb.AuthorLive.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"""
@ -101,7 +103,15 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do
defp notify_parent(msg), do: send(self(), {__MODULE__, msg}) 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 = form =
if author do if author do
AshPhoenix.Form.for_update(author, :update, AshPhoenix.Form.for_update(author, :update,
@ -109,15 +119,26 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do
actor: socket.assigns.current_user actor: socket.assigns.current_user
) )
else else
AshPhoenix.Form.for_create(Metadata.Author, :create, form =
as: "author", AshPhoenix.Form.for_create(Metadata.Author, :create,
actor: socket.assigns.current_user as: "author",
) actor: socket.assigns.current_user
)
|> set_author_alias_registry_id(alias_id)
end end
assign(socket, form: to_form(form)) assign(socket, form: to_form(form))
end 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 defp patch_url(action, author_id) do
case action do case action do
:edit -> ~p"/authors/#{author_id}" :edit -> ~p"/authors/#{author_id}"

@ -11,11 +11,18 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.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 %>
<.link patch={~p"/authors/#{@author}/edit"}> <div class="flex flex-row gap-2">
<.edit_button> <.link patch={~p"/authors/#{@author}/new"}>
Edit <.add_button>
</.edit_button> Alias
</.link> </.add_button>
</.link>
<.link patch={~p"/authors/#{@author}/edit"}>
<.edit_button>
Edit
</.edit_button>
</.link>
</div>
<% end %> <% end %>
</:actions> </:actions>
</.header> </.header>

@ -40,6 +40,7 @@ defmodule DecentralisedBookIndexWeb.Router do
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
live "/authors/:alternative_author_id/new", AuthorLive.Edit, :new
live "/authors/:id/edit", AuthorLive.Edit, :edit live "/authors/:id/edit", AuthorLive.Edit, :edit
live "/publishers", PublisherLive.Index, :index live "/publishers", PublisherLive.Index, :index

Loading…
Cancel
Save