This commit is contained in:
parent
d01ae6af96
commit
7199929257
10 changed files with 778 additions and 0 deletions
77
lib/mv_web/property_live/form_component.ex
Normal file
77
lib/mv_web/property_live/form_component.ex
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
defmodule MvWeb.PropertyLive.FormComponent do
|
||||
use MvWeb, :live_component
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div>
|
||||
<.header>
|
||||
{@title}
|
||||
<:subtitle>Use this form to manage property records in your database.</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.simple_form
|
||||
for={@form}
|
||||
id="property-form"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
>
|
||||
<.input field={@form[:value]} type="text" label="Value" /><.input
|
||||
field={@form[:member_id]}
|
||||
type="text"
|
||||
label="Member"
|
||||
/><.input field={@form[:property_type_id]} type="text" label="Property type" />
|
||||
|
||||
<:actions>
|
||||
<.button phx-disable-with="Saving...">Save Property</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def update(assigns, socket) do
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign_form()}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"property" => property_params}, socket) do
|
||||
{:noreply,
|
||||
assign(socket, form: AshPhoenix.Form.validate(socket.assigns.form, property_params))}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"property" => property_params}, socket) do
|
||||
case AshPhoenix.Form.submit(socket.assigns.form, params: property_params) do
|
||||
{:ok, property} ->
|
||||
notify_parent({:saved, property})
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> put_flash(:info, "Property #{socket.assigns.form.source.type}d successfully")
|
||||
|> push_patch(to: socket.assigns.patch)
|
||||
|
||||
{:noreply, socket}
|
||||
|
||||
{:error, form} ->
|
||||
{:noreply, assign(socket, form: form)}
|
||||
end
|
||||
end
|
||||
|
||||
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
|
||||
|
||||
defp assign_form(%{assigns: %{property: property}} = socket) do
|
||||
form =
|
||||
if property do
|
||||
AshPhoenix.Form.for_update(property, :update, as: "property")
|
||||
else
|
||||
AshPhoenix.Form.for_create(Mv.Membership.Property, :create, as: "property")
|
||||
end
|
||||
|
||||
assign(socket, form: to_form(form))
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue