exmr/lib/exmr_web/live/user_settings_live.ex

177 lines
5.4 KiB
Elixir
Raw Normal View History

2024-11-14 23:33:29 +02:00
defmodule ExmrWeb.UserSettingsLive do
use ExmrWeb, :live_view
alias Exmr.Users
def render(assigns) do
~H"""
<.header class="text-center">
2024-12-12 18:52:36 +02:00
{gettext("Account Settings")}
<:subtitle>{gettext("Manage your account email address and password settings")}</:subtitle>
2024-11-14 23:33:29 +02:00
</.header>
<div class="space-y-12 divide-y">
<div>
<.simple_form
for={@email_form}
id="email_form"
phx-submit="update_email"
phx-change="validate_email"
>
<.input field={@email_form[:email]} type="email" label={gettext("Email")} required />
2024-11-14 23:33:29 +02:00
<.input
field={@email_form[:current_password]}
name="current_password"
id="current_password_for_email"
type="password"
label={gettext("Current password")}
2024-11-14 23:33:29 +02:00
value={@email_form_current_password}
required
/>
<:actions>
<.button phx-disable-with={gettext("Changing...")}>
2024-12-12 18:52:36 +02:00
{gettext("Change Email")}
</.button>
2024-11-14 23:33:29 +02:00
</:actions>
</.simple_form>
</div>
<div>
<.simple_form
for={@password_form}
id="password_form"
action={~p"/users/log_in?_action=password_updated"}
method="post"
phx-change="validate_password"
phx-submit="update_password"
phx-trigger-action={@trigger_submit}
>
<input
name={@password_form[:email].name}
type="hidden"
id="hidden_user_email"
value={@current_email}
/>
<.input
field={@password_form[:password]}
type="password"
label={gettext("New password")}
required
/>
2024-11-14 23:33:29 +02:00
<.input
field={@password_form[:password_confirmation]}
type="password"
label={gettext("Confirm new password")}
2024-11-14 23:33:29 +02:00
/>
<.input
field={@password_form[:current_password]}
name="current_password"
type="password"
label={gettext("Current password")}
2024-11-14 23:33:29 +02:00
id="current_password_for_password"
value={@current_password}
required
/>
<:actions>
<.button phx-disable-with={gettext("Changing...")}>
2024-12-12 18:52:36 +02:00
{gettext("Change Password")}
</.button>
2024-11-14 23:33:29 +02:00
</:actions>
</.simple_form>
</div>
</div>
"""
end
def mount(%{"token" => token}, _session, socket) do
socket =
case Users.update_user_email(socket.assigns.current_user, token) do
:ok ->
put_flash(socket, :info, gettext("Email changed successfully."))
2024-11-14 23:33:29 +02:00
:error ->
put_flash(socket, :error, gettext("Email change link is invalid or it has expired."))
2024-11-14 23:33:29 +02:00
end
{:ok, push_navigate(socket, to: ~p"/users/settings")}
end
def mount(_params, _session, socket) do
user = socket.assigns.current_user
email_changeset = Users.change_user_email(user)
password_changeset = Users.change_user_password(user)
socket =
socket
|> assign(:current_password, nil)
|> assign(:email_form_current_password, nil)
|> assign(:current_email, user.email)
|> assign(:email_form, to_form(email_changeset))
|> assign(:password_form, to_form(password_changeset))
|> assign(:trigger_submit, false)
{:ok, socket}
end
def handle_event("validate_email", params, socket) do
%{"current_password" => password, "user" => user_params} = params
email_form =
socket.assigns.current_user
|> Users.change_user_email(user_params)
|> Map.put(:action, :validate)
|> to_form()
{:noreply, assign(socket, email_form: email_form, email_form_current_password: password)}
end
def handle_event("update_email", params, socket) do
%{"current_password" => password, "user" => user_params} = params
user = socket.assigns.current_user
case Users.apply_user_email(user, password, user_params) do
{:ok, applied_user} ->
Users.deliver_user_update_email_instructions(
applied_user,
user.email,
&url(~p"/users/settings/confirm_email/#{&1}")
)
info = gettext("A link to confirm your email change has been sent to the new address.")
2024-11-14 23:33:29 +02:00
{:noreply, socket |> put_flash(:info, info) |> assign(email_form_current_password: nil)}
{:error, changeset} ->
{:noreply, assign(socket, :email_form, to_form(Map.put(changeset, :action, :insert)))}
end
end
def handle_event("validate_password", params, socket) do
%{"current_password" => password, "user" => user_params} = params
password_form =
socket.assigns.current_user
|> Users.change_user_password(user_params)
|> Map.put(:action, :validate)
|> to_form()
{:noreply, assign(socket, password_form: password_form, current_password: password)}
end
def handle_event("update_password", params, socket) do
%{"current_password" => password, "user" => user_params} = params
user = socket.assigns.current_user
case Users.update_user_password(user, password, user_params) do
{:ok, user} ->
password_form =
user
|> Users.change_user_password(user_params)
|> to_form()
{:noreply, assign(socket, trigger_submit: true, password_form: password_form)}
{:error, changeset} ->
{:noreply, assign(socket, password_form: to_form(changeset))}
end
end
end