Update the Author Show LiveView to show their books.

dev
KKlochko 3 weeks ago
parent e4f17524ea
commit 02a34694e4

@ -179,11 +179,17 @@ defmodule DecentralisedBookIndex.Metadata.Book do
prepare fn query, context ->
author = query.arguments.author
{:ok, author_ids} = Metadata.get_author_ids(author)
author_ids =
case Metadata.get_author_ids(author) do
{:ok, ids} -> ids
{:error, _error} -> []
end
Metadata.Book
query
|> Ash.Query.filter(expr(exists(author_roles, author_id in ^author_ids)))
end
pagination offset?: true, default_limit: 10
end
read :search do

@ -2,6 +2,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do
use DecentralisedBookIndexWeb, :live_view
alias DecentralisedBookIndex.Accounts.Role
alias DecentralisedBookIndex.Metadata
@impl true
def render(assigns) do
@ -96,6 +97,19 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do
</ul>
<% end %>
<%= if not Enum.empty?(@page.results) do %>
<h2 class="mb-2 text-lg font-semibold text-gray-900 dark:text-white mt-10">
Books
</h2>
<div class="flex flex-wrap flex-[3_1_auto]">
<%= for book <- @page.results do %>
<.book_card book={book} current_user={@current_user} />
<% end %>
</div>
<.pagination endpoint={~p"/authors/#{@author}"} page={@page} page_params={@page_params} params={@params} />
<% end %>
<.back navigate={~p"/authors"}>Back to authors</.back>
"""
end
@ -106,19 +120,30 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do
end
@impl true
def handle_params(%{"id" => id}, _, socket) do
def handle_params(%{"id" => id} = params, _, socket) do
author =
Ash.get!(DecentralisedBookIndex.Metadata.Author, id,
Ash.get!(Metadata.Author, id,
load: [:dbi_server],
actor: socket.assigns.current_user
)
alternative_names = DecentralisedBookIndex.Metadata.get_author_alternative_names!(author)
alternative_names = Metadata.get_author_alternative_names!(author)
page_params = AshPhoenix.LiveView.page_from_params(params, 10)
page =
Metadata.get_author_books!(author,
load: [:brief_description],
page: page_params ++ [count: true],
actor: socket.assigns.current_user
)
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:author, author)
|> assign(:page_params, page_params)
|> assign(:page, page)
|> assign(:params, params)
|> assign(:alternative_names, alternative_names)}
end

Loading…
Cancel
Save