diff --git a/lib/decentralised_book_index_web/components/my_components.ex b/lib/decentralised_book_index_web/components/my_components.ex index cc5cf50..6136e97 100644 --- a/lib/decentralised_book_index_web/components/my_components.ex +++ b/lib/decentralised_book_index_web/components/my_components.ex @@ -23,6 +23,9 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents do alias MyComponents.SelectPublisher import MyComponents.SelectedPublisher, only: [selected_publisher: 1] + + alias MyComponents.SelectAuthor + import MyComponents.SelectedAuthor, only: [selected_author: 1] end end end diff --git a/lib/decentralised_book_index_web/components/my_components/select_author.ex b/lib/decentralised_book_index_web/components/my_components/select_author.ex new file mode 100644 index 0000000..c05af77 --- /dev/null +++ b/lib/decentralised_book_index_web/components/my_components/select_author.ex @@ -0,0 +1,57 @@ +defmodule DecentralisedBookIndexWeb.Components.MyComponents.SelectAuthor do + use DecentralisedBookIndexWeb, :live_component + alias DecentralisedBookIndex.Metadata + + @impl true + def render(assigns) do + ~H""" +
+ <.header> + Select Author + + +
+ +
+
+ +
+ +
+
+ + <%= for author <- @page.results do %> +
+
{author.name}
+
+ <% end %> +
+ """ + end + + @impl true + def update(assigns, socket) do + {:ok, + socket + |> assign(assigns) + |> assign(:author_query, "") + |> search()} + end + + def handle_event("search", %{"query" => query}, socket) do + {:noreply, + socket + |> assign(:author_query, query) + |> search()} + end + + defp search(socket) do + query = Map.get(socket.assigns, :author_query, "") + page = Metadata.search_author!(query) + + socket + |> assign(page: page) + end +end diff --git a/lib/decentralised_book_index_web/components/my_components/selected_author.ex b/lib/decentralised_book_index_web/components/my_components/selected_author.ex new file mode 100644 index 0000000..96dff35 --- /dev/null +++ b/lib/decentralised_book_index_web/components/my_components/selected_author.ex @@ -0,0 +1,30 @@ +defmodule DecentralisedBookIndexWeb.Components.MyComponents.SelectedAuthor do + use Phoenix.Component + use DecentralisedBookIndexWeb, :verified_routes + + attr :author_form, :map, default: nil + attr :form_path, :string, default: "" + + def selected_author(assigns) do + author_id = assigns.author_form.value + + author = + if author_id == nil do + author_id + else + DecentralisedBookIndex.Metadata.get_author_by_id!(author_id) + end + + assigns = assign(assigns, :author, author) + + ~H""" + + """ + end +end