defmodule ChesstrainerWeb.UserLive.Profile do
use ChesstrainerWeb, :live_view
alias Chesstrainer.Accounts
alias Chesstrainer.Accounts.Scope
alias Chesstrainer.Attempts
@recent_attempts 25
@tabs [
%{id: :account, label: "Account", path: "/users/profile"},
%{id: :settings, label: "Settings", path: "/users/profile/settings"},
%{id: :data, label: "Data", path: "/users/profile/data"}
]
@impl true
def render(assigns) do
~H"""
<.header>
Profile
<:subtitle>Manage your account, site settings and training data.
<.link
:for={tab <- @tabs}
id={"profile-tab-#{tab.id}"}
patch={tab.path}
class={[
"border-b-2 -mb-px px-3 py-2 text-sm font-semibold",
if(@live_action == tab.id,
do: "border-primary text-primary",
else: "border-transparent text-base-content/60 hover:text-base-content"
)
]}
aria-current={@live_action == tab.id && "page"}
>
{tab.label}
<%= case @live_action do %>
<% :account -> %>
Signed in as
{@current_scope.user.username}
{@current_scope.user.email}
Rating:
{round(@current_scope.user.rating)}
We will send a confirmation link to the new address.
<.form
for={@email_form}
id="email_form"
phx-submit="update_email"
phx-change="validate_email"
>
<.input
field={@email_form[:email]}
type="email"
label="Email"
autocomplete="username"
spellcheck="false"
required
/>
<.button variant="primary" phx-disable-with="Changing...">Change Email
Choose a new password for this account.
<.form
for={@password_form}
id="password_form"
action={~p"/users/update-password"}
method="post"
phx-change="validate_password"
phx-submit="update_password"
phx-trigger-action={@trigger_submit}
>
<.input
field={@password_form[:password]}
type="password"
label="New password"
autocomplete="new-password"
spellcheck="false"
required
/>
<.input
field={@password_form[:password_confirmation]}
type="password"
label="Confirm new password"
autocomplete="new-password"
spellcheck="false"
/>
<.button variant="primary" phx-disable-with="Saving...">
Save Password
This action is permanent and cannot be undone.
Deleting your account permanently removes your profile, rating and training history.
Nothing can be recovered.
<.button
id="delete_account_button"
variant="danger"
phx-click="delete_account"
data-confirm="This permanently deletes your account and all of your data. Nothing can be recovered. Are you sure?"
>
Delete my account
<% :settings -> %>
<%!-- Keep section for when board themes are added etc --%>
<%!--
<.header>
Appearance
<:subtitle>Choose how the site looks on this device.
--%>
Your current plan.
You are on the {plan_label(@current_scope)} plan.
Premium membership is not available yet. When it arrives it will add session
analytics, weak-theme reports and the full puzzle search.
More site settings will appear here as they become available.
<% :data -> %>
Your score and solve history across every puzzle.
<.stat label="Rating" value={round(@current_scope.user.rating)} />
<.stat label="Attempts" value={@stats.attempts} />
<.stat label="Solved" value={@stats.solved} />
<.stat label="Solve rate" value={"#{@stats.solve_rate}%"} />
Download your profile details and every recorded attempt.
<.button id="export-csv" href={~p"/users/profile/export/csv"}>
<.icon name="hero-arrow-down-tray" class="w-4 h-4" /> Download CSV
<.button id="export-json" href={~p"/users/profile/export/json"}>
<.icon name="hero-arrow-down-tray" class="w-4 h-4" /> Download JSON
The last {@recent_attempts} puzzles you finished.
Played
Puzzle
Outcome
{Calendar.strftime(attempt.inserted_at, "%d %b %Y")}
<.link
navigate={~p"/endgames/#{attempt.endgame.public_code}/play"}
class="font-semibold"
>
{attempt.endgame.key}
{attempt.outcome}
No attempts yet. Play a puzzle to start building your history.
Deeper training analytics, once Premium is available.
Session-by-session performance
Failed puzzles and weak themes
<% end %>
"""
end
@impl true
def mount(%{"token" => token}, _session, socket) do
socket =
case Accounts.update_user_email(socket.assigns.current_scope.user, token) do
{:ok, _user} ->
put_flash(socket, :info, "Email changed successfully.")
{:error, _} ->
put_flash(socket, :error, "Email change link is invalid or it has expired.")
end
{:ok, push_navigate(socket, to: ~p"/users/profile")}
end
def mount(_params, _session, socket) do
{:ok,
socket
|> assign(:page_title, "Profile")
|> assign(:tabs, @tabs)
|> assign(:recent_attempts, @recent_attempts)}
end
@impl true
def handle_params(_params, _uri, socket) do
socket = load_section(socket)
if socket.assigns.live_action == :account and not sudo_mode?(socket) do
{:noreply,
socket
|> put_flash(:error, "You must re-authenticate to access this page.")
|> redirect(to: ~p"/users/log-in")}
else
{:noreply, socket}
end
end
@impl true
def handle_event("validate_email", params, socket) do
%{"user" => user_params} = params
email_form =
socket.assigns.current_scope.user
|> Accounts.change_user_email(user_params, validate_unique: false)
|> Map.put(:action, :validate)
|> to_form()
{:noreply, assign(socket, email_form: email_form)}
end
def handle_event("update_email", params, socket) do
%{"user" => user_params} = params
user = socket.assigns.current_scope.user
true = Accounts.sudo_mode?(user)
case Accounts.change_user_email(user, user_params) do
%{valid?: true} = changeset ->
Accounts.deliver_user_update_email_instructions(
Ecto.Changeset.apply_action!(changeset, :insert),
user.email,
&url(~p"/users/settings/confirm-email/#{&1}")
)
info = "A link to confirm your email change has been sent to the new address."
{:noreply, socket |> put_flash(:info, info)}
changeset ->
{:noreply, assign(socket, :email_form, to_form(changeset, action: :insert))}
end
end
def handle_event("validate_password", params, socket) do
%{"user" => user_params} = params
password_form =
socket.assigns.current_scope.user
|> Accounts.change_user_password(user_params, hash_password: false)
|> Map.put(:action, :validate)
|> to_form()
{:noreply, assign(socket, password_form: password_form)}
end
def handle_event("update_password", params, socket) do
%{"user" => user_params} = params
user = socket.assigns.current_scope.user
true = Accounts.sudo_mode?(user)
case Accounts.change_user_password(user, user_params) do
%{valid?: true} = changeset ->
{:noreply, assign(socket, trigger_submit: true, password_form: to_form(changeset))}
changeset ->
{:noreply, assign(socket, password_form: to_form(changeset, action: :insert))}
end
end
def handle_event("delete_account", _params, socket) do
user = socket.assigns.current_scope.user
true = Accounts.sudo_mode?(user)
{:ok, _user} = Accounts.delete_user(user)
{:noreply,
socket
|> put_flash(:info, "Your account has been deleted.")
|> redirect(to: ~p"/")}
end
defp load_section(socket) do
case socket.assigns.live_action do
:account -> assign_account(socket)
:data -> assign_data(socket)
_ -> socket
end
end
defp assign_account(socket) do
user = socket.assigns.current_scope.user
email_changeset = Accounts.change_user_email(user, %{}, validate_unique: false)
password_changeset = Accounts.change_user_password(user, %{}, hash_password: false)
socket
|> assign(:current_email, user.email)
|> assign(:email_form, to_form(email_changeset))
|> assign(:password_form, to_form(password_changeset))
|> assign(:trigger_submit, false)
end
defp assign_data(socket) do
user = socket.assigns.current_scope.user
stats = Attempts.stats_for_user(user.id)
solve_rate = if stats.attempts > 0, do: round(stats.solved * 100 / stats.attempts), else: 0
socket
|> assign(:stats, Map.put(stats, :solve_rate, solve_rate))
|> assign(:attempts, Attempts.list_attempts_for_user(user.id, limit: @recent_attempts))
end
defp sudo_mode?(socket) do
Accounts.sudo_mode?(socket.assigns.current_scope.user, -10)
end
defp plan_label(scope), do: if(Scope.premium?(scope), do: "Premium", else: "Free")
attr :label, :string, required: true
attr :value, :any, required: true
defp stat(assigns) do
~H"""
{@label}
{@value}
"""
end
end