Update the Book form and core components for the bids' nested form.
This commit is contained in:
@@ -18,19 +18,14 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
|
||||
phx-submit="save"
|
||||
>
|
||||
<%= if @form.source.type == :create do %>
|
||||
<.input field={@form[:bids]} type="select" multiple label="Bids" options={[]} />
|
||||
<.input field={@form[:title]} type="text" label="Title" />
|
||||
<.input
|
||||
field={@form[:author_roles]}
|
||||
type="select"
|
||||
multiple
|
||||
label="Author roles"
|
||||
options={[]}
|
||||
/>
|
||||
<.input field={@form[:title]} type="text" label="Title" /><.input
|
||||
field={@form[:description]}
|
||||
type="text"
|
||||
label="Description"
|
||||
/><.input field={@form[:format]} type="text" label="Format" /><.input
|
||||
/>
|
||||
<.bids_inputs form={@form} myself={@myself} />
|
||||
<.input field={@form[:format]} type="text" label="Format" /><.input
|
||||
field={@form[:language]}
|
||||
type="text"
|
||||
label="Language"
|
||||
@@ -38,10 +33,6 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
|
||||
field={@form[:published]}
|
||||
type="date"
|
||||
label="Published"
|
||||
/><.input field={@form[:publisher_id]} type="text" label="Publisher" /><.input
|
||||
field={@form[:book_editions_registry_id]}
|
||||
type="text"
|
||||
label="Book editions registry"
|
||||
/>
|
||||
<% end %>
|
||||
<%= if @form.source.type == :update do %>
|
||||
@@ -50,11 +41,78 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
|
||||
<:actions>
|
||||
<.button phx-disable-with="Saving...">Save Book</.button>
|
||||
</:actions>
|
||||
|
||||
<pre>all:<%= inspect(@form, pretty: true) %></pre>
|
||||
<pre>Bids:<%= inspect(@form[:bids], pretty: true) %></pre>
|
||||
</.simple_form>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def bids_inputs(assigns) do
|
||||
~H"""
|
||||
<h2>Book Ids</h2>
|
||||
|
||||
<table class="w-full">
|
||||
<thead class="border-b border-zinc-100">
|
||||
<tr>
|
||||
<th class="text-left font-medium text-sm pb-1 px-3">Type</th>
|
||||
<th class="text-left font-medium text-sm pb-1 px-3" colspan="2">Id</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody phx-hook="bidSort" id="bidSort" phx-target={@myself}>
|
||||
<.inputs_for :let={bid_form} field={@form[:bids]}>
|
||||
<tr data-id={bid_form.index}>
|
||||
<td class="px-3 w-24">
|
||||
<label for={bid_form[:type].id} class="hidden">Type</label>
|
||||
<.input field={bid_form[:type]} />
|
||||
</td>
|
||||
<td class="px-3">
|
||||
<label for={bid_form[:bid].id} class="hidden">Id</label>
|
||||
<.input field={bid_form[:bid]} />
|
||||
</td>
|
||||
</tr>
|
||||
</.inputs_for>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<.button_link phx-click="add-bid" phx-target={@myself} kind="primary" size="sm" inverse>
|
||||
Add Book Id.
|
||||
</.button_link>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("add-bid", params, socket) do
|
||||
|
||||
socket =
|
||||
update(socket, :form, fn form ->
|
||||
AshPhoenix.Form.add_form(form, :bids)
|
||||
end)
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("remove-bid", %{"path" => path}, socket) do
|
||||
socket =
|
||||
update(socket, :form, fn form ->
|
||||
AshPhoenix.Form.remove_form(form, path)
|
||||
end)
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("reorder-bids", %{"order" => order}, socket) do
|
||||
socket =
|
||||
update(socket, :form, fn form ->
|
||||
AshPhoenix.Form.sort_forms(form, [:bids], order)
|
||||
end)
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def update(assigns, socket) do
|
||||
{:ok,
|
||||
@@ -93,6 +151,10 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
|
||||
AshPhoenix.Form.for_update(book, :update, as: "book", actor: socket.assigns.current_user)
|
||||
else
|
||||
AshPhoenix.Form.for_create(DecentralisedBookIndex.Metadata.Book, :create,
|
||||
transform_params: fn _form, params, _context ->
|
||||
Map.put(params, "author_roles", ["d880b385-5df6-4127-a49c-16f9070ea066"])
|
||||
Map.put(params, "publishers", ["9ca6f110-8ef4-494b-9979-f5b016d0f45e"])
|
||||
end,
|
||||
as: "book",
|
||||
actor: socket.assigns.current_user
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user