Update the Book form to select publisher via the components.

dev
KKlochko 2 months ago
parent 71a61d3765
commit 34bf021962

@ -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
</.cancel_button>
</:actions>
</.simple_form>
<.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}
/>
</.modal>
</div>
"""
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

Loading…
Cancel
Save