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

Loading…
Cancel
Save