Update the Book Show to show the Authors with links.

dev
KKlochko 2 months ago
parent dc35461634
commit 2ccf820f7d

@ -9,7 +9,9 @@ defmodule DecentralisedBookIndexWeb.BookLive.Show do
~H""" ~H"""
<.header> <.header>
{@book.title} {@book.title}
<:subtitle>{@authors_string}</:subtitle> <:subtitle>
<.authors book={@book} />
</:subtitle>
<: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 %>
@ -65,32 +67,44 @@ defmodule DecentralisedBookIndexWeb.BookLive.Show do
@impl true @impl true
def handle_params(%{"id" => id}, _, socket) do def handle_params(%{"id" => id}, _, socket) do
book = Ash.get!(Metadata.Book, id, actor: socket.assigns.current_user, load: [:author_roles]) book = Ash.get!(Metadata.Book, id, actor: socket.assigns.current_user, load: [:author_roles])
author_roles = Metadata.get_author_roles_by_book_id!(book.id, load: [:author])
authors_string = Enum.reduce(
author_roles, "",
fn author_role, acc ->
if acc != "" do
acc <> ", "
else
acc
end
<>
if author_role.role == "" do
"#{author_role.author.name}"
else
"#{author_role.author.name} (#{author_role.role})"
end
end
)
{:noreply, {:noreply,
socket socket
|> assign(:page_title, page_title(socket.assigns.live_action)) |> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:book, book) |> assign(:book, book)
|> assign(:authors_string, authors_string) |> assign(:alternative_editions, alternative_editions)
} }
end end
defp page_title(:show), do: "Show Book" defp page_title(:show), do: "Show Book"
defp page_title(:edit), do: "Edit Book" defp page_title(:edit), do: "Edit Book"
attr :book, :map, required: true
def authors(assigns) do
author_roles = Metadata.get_author_roles_by_book_id!(assigns.book.id, load: [:author])
author_count = length(author_roles)
authors_string =
author_roles
|> Enum.map(fn author_role ->
if author_role.role == "" do
{"#{author_role.author.name}", author_role.author.id}
else
{"#{author_role.author.name} (#{author_role.role})", author_role.author.id}
end
end)
|> Enum.zip(1..author_count)
assigns =
assigns
|> assign(:authors_string, authors_string)
|> assign(:author_count, author_count)
~H"""
<%= for {{name, id}, index} <- @authors_string do %>
<.link navigate={~p"/authors/#{id}/"} class="hover:underline">
{name}</.link><%= if {index} != {@author_count} do %>,<% end %>
<% end %>
"""
end
end end

Loading…
Cancel
Save