Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Settings: Allow to upload avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Ceolin committed Nov 6, 2023
1 parent 3421bda commit 0f8e382
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 1 deletion.
17 changes: 17 additions & 0 deletions lib/accounts/user_live/user_settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule UneebeeWeb.Live.UserSettings do
use UneebeeWeb, :live_view

alias Uneebee.Accounts
alias UneebeeWeb.Components.Upload

# When users change their email address, we send them a link to confirm their new email.
# That link contains a `token` parameter that we use to confirm their email when they
Expand Down Expand Up @@ -128,11 +129,27 @@ defmodule UneebeeWeb.Live.UserSettings do
end
end

@impl Phoenix.LiveView
def handle_info({Upload, :user_avatar, new_path}, socket) do
case Accounts.update_user_settings(socket.assigns.current_user, %{avatar: new_path}) do
{:ok, updated_user} ->
{:noreply,
socket
|> assign(current_user: updated_user)
|> put_flash(:info, gettext("Avatar updated successfully!"))}

{:error, _changeset} ->
{:noreply, put_flash(socket, :error, gettext("Could not update avatar!"))}
end
end

defp get_changeset(:profile, user), do: Accounts.change_user_settings(user)
defp get_changeset(:email, user), do: Accounts.change_user_email(user)
defp get_changeset(:password, user), do: Accounts.change_user_password(user)
defp get_changeset(_live_action, user), do: Accounts.change_user_settings(user)

defp get_page_title(:profile), do: gettext("Change profile")
defp get_page_title(:email), do: gettext("Change email")
defp get_page_title(:password), do: gettext("Change password")
defp get_page_title(:avatar), do: gettext("Avatar")
end
3 changes: 3 additions & 0 deletions lib/accounts/user_live/user_settings.html.heex
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<.live_component :if={@live_action == :avatar} module={Upload} id={:user_avatar} current_img={@current_user.avatar} label={gettext("Avatar")} />

<.simple_form
:if={@live_action != :avatar}
unstyled
for={@form}
phx-trigger-action={if @live_action == :password, do: @trigger_submit, else: nil}
Expand Down
2 changes: 1 addition & 1 deletion lib/accounts/user_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ defmodule Uneebee.Accounts.User do
@spec settings_changeset(Ecto.Schema.t(), map(), Keyword.t()) :: Ecto.Changeset.t()
def settings_changeset(user, attrs, opts \\ []) do
user
|> cast(attrs, [:first_name, :last_name, :language, :username])
|> cast(attrs, [:avatar, :first_name, :last_name, :language, :username])
|> validate_settings()
|> validate_username(opts)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/layouts/templates/app.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<%= gettext("Profile") %>
</.dashboard_menu_item>

<.dashboard_menu_item active={@active_page == :usersettings_avatar} navigate={~p"/users/settings/avatar"}>
<%= gettext("Avatar") %>
</.dashboard_menu_item>

<.dashboard_menu_item active={@active_page == :usersettings_email} navigate={~p"/users/settings/email"}>
<%= gettext("Email") %>
</.dashboard_menu_item>
Expand Down
1 change: 1 addition & 0 deletions lib/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ defmodule UneebeeWeb.Router do
live "/medals", MedalList

live "/users/settings", UserSettings, :profile
live "/users/settings/avatar", UserSettings, :avatar
live "/users/settings/email", UserSettings, :email
live "/users/settings/password", UserSettings, :password
live "/users/settings/confirm_email/:token", UserSettings, :confirm_email
Expand Down
18 changes: 18 additions & 0 deletions test/accounts/user_live/user_settings_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule UneebeeWeb.UserSettingsLiveTest do
import Phoenix.LiveViewTest
import Uneebee.Fixtures.Accounts
import Uneebee.Fixtures.Gamification
import UneebeeWeb.TestHelpers.Upload

alias Uneebee.Accounts
alias Uneebee.Gamification
Expand Down Expand Up @@ -180,6 +181,23 @@ defmodule UneebeeWeb.UserSettingsLiveTest do
end
end

describe "/users/settings/avatar" do
setup :register_and_log_in_user

test "uploads avatar", %{conn: conn, user: user} do
{:ok, lv, _html} = live(conn, ~p"/users/settings/avatar")

assert user.avatar == nil

assert has_element?(lv, ~s|li[aria-current=page] a:fl-icontains("settings")|)
assert has_element?(lv, ~s|li[aria-current=page] a:fl-icontains("avatar")|)
assert_file_upload(lv, "user_avatar")

updated_user = Accounts.get_user!(user.id)
assert String.starts_with?(updated_user.avatar, "/uploads/")
end
end

describe "/users/settings/email" do
setup :register_and_log_in_user

Expand Down

0 comments on commit 0f8e382

Please sign in to comment.