defmodule AshHqWeb.Pages.UserSettings do
@moduledoc "User settings page"
use Surface.LiveComponent
alias Surface.Components.Form
alias Surface.Components.Form.{
ErrorTag,
Field,
Label,
PasswordInput,
Submit,
TextArea,
TextInput
}
prop(current_user, :map, required: true)
data(email_form, :map)
data(password_form, :map)
data(merch_form, :map)
data(address, :string)
data(name, :string)
def render(assigns) do
~F"""
{#if !@current_user.confirmed_at}
{/if}
"""
end
@impl true
def update(assigns, socket) do
{:ok,
socket
|> assign(assigns)
|> assign_forms()}
end
defp assign_forms(socket) do
user =
AshHq.Accounts.load!(socket.assigns.current_user, [:address, :name],
actor: socket.assigns.current_user
)
merch_form_params = %{"address" => user.address, "name" => user.name}
assign(socket,
email_form:
AshPhoenix.Form.for_update(
socket.assigns.current_user,
:update_email,
as: "update_email",
api: AshHq.Accounts,
actor: socket.assigns.current_user
),
password_form:
AshPhoenix.Form.for_update(socket.assigns.current_user, :change_password,
as: "change_password",
api: AshHq.Accounts,
actor: socket.assigns.current_user
),
merch_form:
AshPhoenix.Form.for_update(socket.assigns.current_user, :update_merch_settings,
as: "update_merch",
api: AshHq.Accounts,
actor: socket.assigns.current_user
)
|> AshPhoenix.Form.validate(merch_form_params, errors: false)
)
end
@impl true
def handle_event("validate_update_email", %{"update_email" => params}, socket) do
{:noreply,
assign(socket,
email_form:
AshPhoenix.Form.validate(socket.assigns.email_form, params,
errors: socket.assigns.email_form.submitted_once?
)
)}
end
@impl true
def handle_event("save_update_email", %{"update_email" => params}, socket) do
case AshPhoenix.Form.submit(socket.assigns.email_form, params: params) do
{:ok, _result} ->
{:noreply,
socket
|> put_flash(:info, "Check your email to confirm your new address.")
|> push_redirect(to: "/users/settings")}
{:error, email_form} ->
{:noreply, assign(socket, email_form: email_form)}
end
end
@impl true
def handle_event("save_merch", %{"update_merch" => params}, socket) do
case AshPhoenix.Form.submit(socket.assigns.merch_form, params: params) do
{:ok, _result} ->
{:noreply,
socket
|> put_flash(:info, "Merch settings updated")
|> push_redirect(to: "/users/settings")}
{:error, merch_form} ->
{:noreply, assign(socket, merch_form: merch_form)}
end
end
def handle_event("validate_merch", %{"update_merch" => params}, socket) do
{:noreply,
assign(socket,
merch_form:
AshPhoenix.Form.validate(socket.assigns.merch_form, params,
errors: socket.assigns.merch_form.submitted_once?
)
)}
end
@impl true
def handle_event("validate_password", %{"change_password" => params}, socket) do
{:noreply,
assign(socket,
password_form:
AshPhoenix.Form.validate(socket.assigns.password_form, params,
errors: socket.assigns.email_form.submitted_once?
)
)}
end
@impl true
def handle_event("save_password", %{"change_password" => params}, socket) do
case AshPhoenix.Form.submit(socket.assigns.password_form, params: params) do
{:ok, _result} ->
{:noreply,
socket
|> put_flash(:info, "Password has been successfully changed.")
|> push_redirect(to: "/")}
{:error, password_form} ->
{:noreply, assign(socket, password_form: password_form)}
end
end
@impl true
def handle_event("resend_confirmation_instructions", _, socket) do
AshHq.Accounts.User.resend_confirmation_instructions!(socket.assigns.current_user,
actor: socket.assigns.current_user
)
{:noreply, socket}
end
end