Update the Book Show LiveView to show authors with their roles.

dev
KKlochko 3 months ago
parent df98f12aea
commit 9c89dec3cc

@ -1,12 +1,14 @@
defmodule DecentralisedBookIndexWeb.BookLive.Show do
use DecentralisedBookIndexWeb, :live_view
alias DecentralisedBookIndex.Metadata
@impl true
def render(assigns) do
~H"""
<.header>
Book {@book.id}
<:subtitle>This is a book record from your database.</:subtitle>
{@book.title}
<:subtitle>{@authors_string}</:subtitle>
<:actions>
<.link patch={~p"/books/#{@book}/edit"}>
@ -46,13 +48,31 @@ defmodule DecentralisedBookIndexWeb.BookLive.Show do
@impl true
def handle_params(%{"id" => id}, _, socket) do
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,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(
:book,
Ash.get!(DecentralisedBookIndex.Metadata.Book, id, actor: socket.assigns.current_user)
)}
|> assign(:book, book)
|> assign(:authors_string, authors_string)
}
end
defp page_title(:show), do: "Show Book"

Loading…
Cancel
Save