diff --git a/lib/decentralised_book_index_web/live/book_live/show.ex b/lib/decentralised_book_index_web/live/book_live/show.ex index dd35ad1..5ed7ffa 100644 --- a/lib/decentralised_book_index_web/live/book_live/show.ex +++ b/lib/decentralised_book_index_web/live/book_live/show.ex @@ -9,7 +9,9 @@ defmodule DecentralisedBookIndexWeb.BookLive.Show do ~H""" <.header> {@book.title} - <:subtitle>{@authors_string} + <:subtitle> + <.authors book={@book} /> + <:actions> <%= if @current_user != nil and Role.can_moderate?(@current_user.role) do %> @@ -65,32 +67,44 @@ 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, book) - |> assign(:authors_string, authors_string) + |> assign(:alternative_editions, alternative_editions) } end defp page_title(:show), do: "Show 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}<%= if {index} != {@author_count} do %>,<% end %> + <% end %> + """ + end end