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

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

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

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

Loading…
Cancel
Save