96 lines
3 KiB
Elixir
96 lines
3 KiB
Elixir
defmodule MvWeb.UserLive.Form do
|
|
use MvWeb, :live_view
|
|
|
|
@impl true
|
|
def render(assigns) do
|
|
~H"""
|
|
<Layouts.app flash={@flash}>
|
|
<.header>
|
|
{@page_title}
|
|
<:subtitle>{gettext("Use this form to manage user records in your database.")}</:subtitle>
|
|
</.header>
|
|
|
|
<.form for={@form} id="user-form" phx-change="validate" phx-submit="save">
|
|
<.input field={@form[:email]} label={gettext("Email")} required type="email" />
|
|
|
|
<%= if @user do %>
|
|
<div class="mt-4 p-4 bg-blue-50 rounded-lg">
|
|
<p class="text-sm text-blue-800">
|
|
<strong>{gettext("Note")}:</strong> {gettext("Password can only be changed through authentication functions.")}
|
|
</p>
|
|
</div>
|
|
<% else %>
|
|
<div class="mt-4 p-4 bg-yellow-50 rounded-lg">
|
|
<p class="text-sm text-yellow-800">
|
|
<strong>{gettext("Note")}:</strong> {gettext("Users created here will need to set their password through the authentication system.")}
|
|
</p>
|
|
</div>
|
|
<% end %>
|
|
|
|
<.button phx-disable-with={gettext("Saving...")} variant="primary">{gettext("Save User")}</.button>
|
|
<.button navigate={return_path(@return_to, @user)}>{gettext("Cancel")}</.button>
|
|
</.form>
|
|
</Layouts.app>
|
|
"""
|
|
end
|
|
|
|
@impl true
|
|
def mount(params, _session, socket) do
|
|
user =
|
|
case params["id"] do
|
|
nil -> nil
|
|
id -> Ash.get!(Mv.Accounts.User, id, domain: Mv.Accounts)
|
|
end
|
|
|
|
action = if is_nil(user), do: gettext("New"), else: gettext("Edit")
|
|
page_title = action <> " " <> gettext("User")
|
|
|
|
{:ok,
|
|
socket
|
|
|> assign(:return_to, return_to(params["return_to"]))
|
|
|> assign(user: user)
|
|
|> assign(:page_title, page_title)
|
|
|> assign_form()}
|
|
end
|
|
|
|
defp return_to("show"), do: "show"
|
|
defp return_to(_), do: "index"
|
|
|
|
@impl true
|
|
def handle_event("validate", %{"user" => user_params}, socket) do
|
|
{:noreply, assign(socket, form: AshPhoenix.Form.validate(socket.assigns.form, user_params))}
|
|
end
|
|
|
|
def handle_event("save", %{"user" => user_params}, socket) do
|
|
case AshPhoenix.Form.submit(socket.assigns.form, params: user_params) do
|
|
{:ok, user} ->
|
|
notify_parent({:saved, user})
|
|
|
|
socket =
|
|
socket
|
|
|> put_flash(:info, "User #{socket.assigns.form.source.type}d successfully")
|
|
|> push_navigate(to: return_path(socket.assigns.return_to, user))
|
|
|
|
{:noreply, socket}
|
|
|
|
{:error, form} ->
|
|
{:noreply, assign(socket, form: form)}
|
|
end
|
|
end
|
|
|
|
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
|
|
|
|
defp assign_form(%{assigns: %{user: user}} = socket) do
|
|
form =
|
|
if user do
|
|
AshPhoenix.Form.for_update(user, :update_user, domain: Mv.Accounts, as: "user")
|
|
else
|
|
AshPhoenix.Form.for_create(Mv.Accounts.User, :create_user, domain: Mv.Accounts, as: "user")
|
|
end
|
|
|
|
assign(socket, form: to_form(form))
|
|
end
|
|
|
|
defp return_path("index", _user), do: ~p"/users"
|
|
defp return_path("show", user), do: ~p"/users/#{user.id}"
|
|
end
|