add user fields

This commit is contained in:
Inhji 2023-07-03 20:16:50 +02:00
parent f10ef9ff9c
commit 7d2c3aceef
5 changed files with 94 additions and 0 deletions

View file

@ -116,6 +116,19 @@ defmodule Chiya.Accounts do
User.email_changeset(user, attrs, validate_email: false) User.email_changeset(user, attrs, validate_email: false)
end end
@doc """
Returns an `%Ecto.Changeset{}` for changing the user profile.
## Examples
iex> change_user_profile(user)
%Ecto.Changeset{data: %User{}}
"""
def change_user_profile(user, attrs \\ %{}) do
User.profile_changeset(user, attrs)
end
@doc """ @doc """
Returns an `%Ecto.Changeset{}` for changing the user avatar. Returns an `%Ecto.Changeset{}` for changing the user avatar.
@ -149,6 +162,15 @@ defmodule Chiya.Accounts do
|> Ecto.Changeset.apply_action(:update) |> Ecto.Changeset.apply_action(:update)
end end
@doc """
Updates the user profile.
"""
def update_user_profile(user, attrs) do
user
|> User.profile_changeset(attrs)
|> Repo.update()
end
@doc """ @doc """
Updates the user email using the given token. Updates the user email using the given token.

View file

@ -4,6 +4,10 @@ defmodule Chiya.Accounts.User do
import Ecto.Changeset import Ecto.Changeset
schema "users" do schema "users" do
field :name, :string
field :handle, :string
field :bio, :string
field :email, :string field :email, :string
field :password, :string, virtual: true, redact: true field :password, :string, virtual: true, redact: true
field :hashed_password, :string, redact: true field :hashed_password, :string, redact: true
@ -116,6 +120,14 @@ defmodule Chiya.Accounts.User do
end end
end end
@doc """
Returns an `%Ecto.Changeset{}` for tracking user profile changes.
"""
def profile_changeset(user, attrs) do
user
|> cast(attrs, [:name, :handle, :bio])
end
@doc """ @doc """
A user changeset for changing the password. A user changeset for changing the password.

View file

@ -13,6 +13,9 @@ defmodule ChiyaWeb.UserProfileLive do
</.header> </.header>
<.list> <.list>
<:item title="Name"><%= @current_user.name %></:item>
<:item title="Handle"><%= @current_user.handle %></:item>
<:item title="Bio"><%= @current_user.bio %></:item>
<:item title="Email"><%= @current_user.email %></:item> <:item title="Email"><%= @current_user.email %></:item>
</.list> </.list>
""" """

View file

@ -29,6 +29,25 @@ defmodule ChiyaWeb.UserSettingsLive do
<.line /> <.line />
<.header>Change Profile</.header>
<.simple_form
for={@profile_form}
id="profile_form"
phx-submit="update_profile"
phx-change="validate_profile"
>
<.input field={@profile_form[:name]} label="Name" required />
<.input field={@profile_form[:handle]} label="Handle" required />
<.input field={@profile_form[:bio]} type="textarea" label="Bio" required />
<:actions>
<.button phx-disable-with="Changing...">Change Email</.button>
</:actions>
</.simple_form>
<.line />
<.header>Change Email</.header> <.header>Change Email</.header>
<.simple_form <.simple_form
@ -107,6 +126,7 @@ defmodule ChiyaWeb.UserSettingsLive do
email_changeset = Accounts.change_user_email(user) email_changeset = Accounts.change_user_email(user)
password_changeset = Accounts.change_user_password(user) password_changeset = Accounts.change_user_password(user)
image_changeset = Accounts.change_user_image(user) image_changeset = Accounts.change_user_image(user)
profile_changeset = Accounts.change_user_profile(user)
socket = socket =
socket socket
@ -116,6 +136,7 @@ defmodule ChiyaWeb.UserSettingsLive do
|> assign(:current_email, user.email) |> assign(:current_email, user.email)
|> assign(:email_form, to_form(email_changeset)) |> assign(:email_form, to_form(email_changeset))
|> assign(:password_form, to_form(password_changeset)) |> assign(:password_form, to_form(password_changeset))
|> assign(:profile_form, to_form(profile_changeset))
|> assign(:image_form, to_form(image_changeset)) |> assign(:image_form, to_form(image_changeset))
|> assign(:trigger_submit, false) |> assign(:trigger_submit, false)
|> assign(:uploaded_files, []) |> assign(:uploaded_files, [])
@ -150,6 +171,31 @@ defmodule ChiyaWeb.UserSettingsLive do
|> assign(:user, Accounts.get_user!(user.id))} |> assign(:user, Accounts.get_user!(user.id))}
end end
def handle_event("validate_profile", params, socket) do
%{"user" => user_params} = params
profile_form =
socket.assigns.current_user
|> Accounts.change_user_profile(user_params)
|> Map.put(:action, :validate)
|> to_form()
{:noreply, assign(socket, profile_form: profile_form)}
end
def handle_event("update_profile", params, socket) do
%{"user" => user_params} = params
user = socket.assigns.current_user
case Accounts.update_user_profile(user, user_params) do
{:ok, _updated_user} ->
{:noreply, socket |> put_flash(:info, "User updated!")}
{:error, changeset} ->
{:noreply, assign(socket, :profile_form, to_form(Map.put(changeset, :action, :insert)))}
end
end
def handle_event("validate_email", params, socket) do def handle_event("validate_email", params, socket) do
%{"current_password" => password, "user" => user_params} = params %{"current_password" => password, "user" => user_params} = params

View file

@ -0,0 +1,11 @@
defmodule Chiya.Repo.Migrations.AddUserFields do
use Ecto.Migration
def change do
alter table(:users) do
add :handle, :string
add :name, :string
add :bio, :text
end
end
end