Update the Book form and core components for the bids' nested form.
This commit is contained in:
@@ -176,6 +176,69 @@ defmodule DecentralisedBookIndexWeb.CoreComponents do
|
|||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
attr :kind, :string,
|
||||||
|
values: ~w(base primary error),
|
||||||
|
default: "base"
|
||||||
|
|
||||||
|
attr :inverse, :boolean, default: false
|
||||||
|
attr :size, :string, values: ~w(sm xs md), default: "md"
|
||||||
|
attr :class, :string, default: ""
|
||||||
|
attr :rest, :global, include: ~w(navigate disabled patch)
|
||||||
|
|
||||||
|
slot :inner_block
|
||||||
|
|
||||||
|
def button_link(assigns) do
|
||||||
|
assigns =
|
||||||
|
assign(assigns, :theme, button_styles(assigns.kind, assigns.inverse, assigns.size))
|
||||||
|
|
||||||
|
~H"""
|
||||||
|
<.link
|
||||||
|
class={[
|
||||||
|
@theme,
|
||||||
|
@rest[:disabled] && "opacity-60 grayscale pointer-events-none",
|
||||||
|
@class
|
||||||
|
]}
|
||||||
|
{@rest}
|
||||||
|
>
|
||||||
|
{render_slot(@inner_block)}
|
||||||
|
</.link>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
def button_styles(kind, inverse, size) do
|
||||||
|
theme =
|
||||||
|
case {kind, inverse} do
|
||||||
|
{"base", false} ->
|
||||||
|
"bg-gray-100"
|
||||||
|
|
||||||
|
{"base", true} ->
|
||||||
|
"border border-gray-500 text-gray-600"
|
||||||
|
|
||||||
|
{"primary", false} ->
|
||||||
|
"bg-primary-600 hover:bg-primary-700 text-white"
|
||||||
|
|
||||||
|
{"primary", true} ->
|
||||||
|
"border border-primary-700 text-primary-700 hover:bg-primary-50 font-semibold"
|
||||||
|
|
||||||
|
{"error", false} ->
|
||||||
|
"bg-error-700 hover:bg-error-800 text-white"
|
||||||
|
|
||||||
|
{"error", true} ->
|
||||||
|
"text-error-600 underline"
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
""
|
||||||
|
end
|
||||||
|
|
||||||
|
[
|
||||||
|
"phx-submit-loading:opacity-75 rounded-lg font-medium leading-none inline-block",
|
||||||
|
size == "md" && "py-3 px-5 text-sm",
|
||||||
|
size == "sm" && "py-2 px-3 text-sm",
|
||||||
|
size == "xs" && "py-2 px-2 text-xs",
|
||||||
|
theme
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Renders a simple form.
|
Renders a simple form.
|
||||||
|
|
||||||
|
|||||||
@@ -18,19 +18,14 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
|
|||||||
phx-submit="save"
|
phx-submit="save"
|
||||||
>
|
>
|
||||||
<%= if @form.source.type == :create do %>
|
<%= 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
|
<.input
|
||||||
field={@form[:author_roles]}
|
|
||||||
type="select"
|
|
||||||
multiple
|
|
||||||
label="Author roles"
|
|
||||||
options={[]}
|
|
||||||
/>
|
|
||||||
<.input field={@form[:title]} type="text" label="Title" /><.input
|
|
||||||
field={@form[:description]}
|
field={@form[:description]}
|
||||||
type="text"
|
type="text"
|
||||||
label="Description"
|
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]}
|
field={@form[:language]}
|
||||||
type="text"
|
type="text"
|
||||||
label="Language"
|
label="Language"
|
||||||
@@ -38,10 +33,6 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
|
|||||||
field={@form[:published]}
|
field={@form[:published]}
|
||||||
type="date"
|
type="date"
|
||||||
label="Published"
|
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 %>
|
<% end %>
|
||||||
<%= if @form.source.type == :update do %>
|
<%= if @form.source.type == :update do %>
|
||||||
@@ -50,11 +41,78 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
|
|||||||
<:actions>
|
<:actions>
|
||||||
<.button phx-disable-with="Saving...">Save Book</.button>
|
<.button phx-disable-with="Saving...">Save Book</.button>
|
||||||
</:actions>
|
</:actions>
|
||||||
|
|
||||||
|
<pre>all:<%= inspect(@form, pretty: true) %></pre>
|
||||||
|
<pre>Bids:<%= inspect(@form[:bids], pretty: true) %></pre>
|
||||||
</.simple_form>
|
</.simple_form>
|
||||||
</div>
|
</div>
|
||||||
"""
|
"""
|
||||||
end
|
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
|
@impl true
|
||||||
def update(assigns, socket) do
|
def update(assigns, socket) do
|
||||||
{:ok,
|
{:ok,
|
||||||
@@ -93,6 +151,10 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
|
|||||||
AshPhoenix.Form.for_update(book, :update, as: "book", actor: socket.assigns.current_user)
|
AshPhoenix.Form.for_update(book, :update, as: "book", actor: socket.assigns.current_user)
|
||||||
else
|
else
|
||||||
AshPhoenix.Form.for_create(DecentralisedBookIndex.Metadata.Book, :create,
|
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",
|
as: "book",
|
||||||
actor: socket.assigns.current_user
|
actor: socket.assigns.current_user
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user