From 34bf021962d30543f6f819f3635d7835eb8cb178 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 20 Apr 2025 16:15:54 +0300 Subject: [PATCH] Update the Book form to select publisher via the components. --- .../live/book_live/form_component.ex | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) 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 b52cf04..674df64 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 @@ -33,7 +33,10 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do label="Language" /> <.input field={@form[:page_count]} type="number" label="Page count" /> - <.input field={@form[:publisher_id]} type="text" label="Publisher" /> + <.selected_publisher + publisher_form={@form[:publisher_id]} + notify_component={@myself} + /> <.input field={@form[:published]} type="date" @@ -57,7 +60,10 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do label="Language" /> <.input field={@form[:page_count]} type="number" label="Page count" /> - <.input field={@form[:publisher_id]} type="text" label="Publisher" /> + <.selected_publisher + publisher_form={@form[:publisher_id]} + notify_component={@myself} + /> <.input field={@form[:published]} type="date" @@ -74,6 +80,21 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do + + <.modal + :if={@select_publisher_open? == true} + id="select-publisher-modal" + show + on_cancel={JS.push("close-select-publisher", target: @myself)} + > + <.live_component + id="select-publisher" + module={SelectPublisher} + action={:edit} + current_user={@current_user} + notify_component={@myself} + /> + """ end @@ -252,6 +273,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do {:ok, socket |> assign(assigns) + |> assign(:select_publisher_open?, false) |> assign_form()} end @@ -315,4 +337,32 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do _ -> ~p"/books" end end + + def handle_event("select-publisher", %{"publisher" => publisher_id}, socket) do + socket = + socket + |> update(:form, fn form -> + form + |> AshPhoenix.Form.update_params(&Map.put(&1, "publisher_id", publisher_id)) + end) + |> assign(:select_publisher_open?, false) + + {:noreply, socket} + end + + def handle_event("open-select-publisher", _params, socket) do + socket = + socket + |> assign(:select_publisher_open?, true) + + {:noreply, socket} + end + + def handle_event("close-select-publisher", _params, socket) do + socket = + socket + |> assign(:select_publisher_open?, false) + + {:noreply, socket} + end end