diff --git a/lib/decentralised_book_index_web/components/my_components.ex b/lib/decentralised_book_index_web/components/my_components.ex index daafdb9..dfca610 100644 --- a/lib/decentralised_book_index_web/components/my_components.ex +++ b/lib/decentralised_book_index_web/components/my_components.ex @@ -28,6 +28,9 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents do alias MyComponents.SelectAuthor import MyComponents.SelectedAuthor, only: [selected_author: 1] + + alias MyComponents.SelectBookEdition + import MyComponents.SelectedBookEdition, only: [selected_book_edition: 1] end end end diff --git a/lib/decentralised_book_index_web/components/my_components/select_book_edition.ex b/lib/decentralised_book_index_web/components/my_components/select_book_edition.ex new file mode 100644 index 0000000..37c7a42 --- /dev/null +++ b/lib/decentralised_book_index_web/components/my_components/select_book_edition.ex @@ -0,0 +1,84 @@ +defmodule DecentralisedBookIndexWeb.Components.MyComponents.SelectBookEdition do + use DecentralisedBookIndexWeb, :live_component + alias DecentralisedBookIndex.Metadata + + @impl true + def render(assigns) do + ~H""" +
+ <.header> + Select another edition of the book + + +
+ +
+
+ +
+ +
+
+ + <%= for book_edition <- @page.results do %> +
+
+ {book_edition.title} +
+
+ <% end %> +
+ """ + end + + @impl true + def update(assigns, socket) do + {:ok, + socket + |> assign(assigns) + |> assign(:book_edition_query, "") + |> search()} + end + + def handle_event("search", %{"query" => query}, socket) do + {:noreply, + socket + |> assign(:book_edition_query, query) + |> search()} + end + + defp search(socket) do + query = Map.get(socket.assigns, :book_edition_query, "") + page = Metadata.search_book!(query) + + socket + |> assign(page: page) + end +end diff --git a/lib/decentralised_book_index_web/components/my_components/selected_book_edition.ex b/lib/decentralised_book_index_web/components/my_components/selected_book_edition.ex new file mode 100644 index 0000000..376a697 --- /dev/null +++ b/lib/decentralised_book_index_web/components/my_components/selected_book_edition.ex @@ -0,0 +1,19 @@ +defmodule DecentralisedBookIndexWeb.Components.MyComponents.SelectedBookEdition do + use Phoenix.Component + use DecentralisedBookIndexWeb, :verified_routes + + attr :book_editions_registry_form, :map, default: nil + + def selected_book_edition(assigns) do + ~H""" + + """ + end +end diff --git a/lib/decentralised_book_index_web/live/book_live/form_component.ex b/lib/decentralised_book_index_web/live/book_live/form_component.ex index 4b81e3d..d294bac 100644 --- a/lib/decentralised_book_index_web/live/book_live/form_component.ex +++ b/lib/decentralised_book_index_web/live/book_live/form_component.ex @@ -37,16 +37,13 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do <.input field={@form[:page_count]} type="number" label="Page count" />
<.input field={@form[:publisher_id]} type="text" label="Publisher" type="hidden" /> - <.selected_publisher - publisher_form={@form[:publisher_id]} - notify_component={@myself} - /> + <.selected_publisher publisher_form={@form[:publisher_id]} notify_component={@myself} /> +
+ <.input field={@form[:published]} type="date" label="Published" /> +
+ <.input field={@form[:book_editions_registry_id]} type="hidden" label="Book Editions" /> + <.selected_book_edition book_editions_registry_form={@form[:book_editions_registry_id]} notify_component={@myself} />
- <.input - field={@form[:published]} - type="date" - label="Published" - /> <% end %> <%= if @form.source.type == :update do %> <.input field={@form[:title]} type="text" label="Title" /> @@ -67,16 +64,13 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do <.input field={@form[:page_count]} type="number" label="Page count" />
<.input field={@form[:publisher_id]} type="text" label="Publisher" type="hidden" /> - <.selected_publisher - publisher_form={@form[:publisher_id]} - notify_component={@myself} - /> + <.selected_publisher publisher_form={@form[:publisher_id]} notify_component={@myself} /> +
+ <.input field={@form[:published]} type="date" label="Published" /> +
+ <.input field={@form[:book_editions_registry_id]} type="hidden" label="Book Editions" /> + <.selected_book_edition book_editions_registry_form={@form[:book_editions_registry_id]} notify_component={@myself} />
- <.input - field={@form[:published]} - type="date" - label="Published" - /> <% end %> <:actions> @@ -103,6 +97,20 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do /> + <.modal + :if={@select_book_edition_open? == true} + id="select-book-edition-modal" + show + on_cancel={JS.push("close-select-book-edition", target: @myself)} + > + <.live_component + id="select-book-edition" + module={SelectBookEdition} + current_user={@current_user} + notify_component={@myself} + /> + + <.modal :if={@select_author_open? == true} id="select-author-modal" @@ -302,6 +310,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do |> assign(assigns) |> assign(:select_publisher_open?, false) |> assign(:select_author_open?, false) + |> assign(:select_book_edition_open?, false) |> assign(:form_path, nil) |> assign_form()} end @@ -448,4 +457,32 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do {:noreply, socket} end + + def handle_event("select-book-edition", %{"book-edition" => book_edition_id}, socket) do + socket = + socket + |> update(:form, fn form -> + form + |> AshPhoenix.Form.update_params(&Map.put(&1, "book_editions_registry_id", book_edition_id)) + end) + |> assign(:select_book_edition_open?, false) + + {:noreply, socket} + end + + def handle_event("open-select-book-edition", _params, socket) do + socket = + socket + |> assign(:select_book_edition_open?, true) + + {:noreply, socket} + end + + def handle_event("close-select-book-edition", _params, socket) do + socket = + socket + |> assign(:select_book_edition_open?, false) + + {:noreply, socket} + end end