2021-05-04 02:03:19 +08:00
|
|
|
defmodule LivebookWeb.UserComponent do
|
|
|
|
use LivebookWeb, :live_component
|
|
|
|
|
2024-01-26 12:47:56 +08:00
|
|
|
import LivebookWeb.UserComponents
|
2021-05-04 02:03:19 +08:00
|
|
|
|
2022-08-23 05:12:54 +08:00
|
|
|
alias Livebook.EctoTypes.HexColor
|
|
|
|
alias Livebook.Users
|
2021-05-04 02:03:19 +08:00
|
|
|
|
|
|
|
@impl true
|
|
|
|
def update(assigns, socket) do
|
|
|
|
socket = assign(socket, assigns)
|
|
|
|
user = socket.assigns.user
|
2022-08-23 05:12:54 +08:00
|
|
|
changeset = Users.change_user(user)
|
2021-05-04 02:03:19 +08:00
|
|
|
|
2023-02-24 02:00:03 +08:00
|
|
|
{:ok, assign(socket, changeset: changeset, user: user)}
|
2021-05-04 02:03:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
2021-07-07 20:32:49 +08:00
|
|
|
~H"""
|
2021-05-04 02:03:19 +08:00
|
|
|
<div class="p-6 flex flex-col space-y-5">
|
|
|
|
<h3 class="text-2xl font-semibold text-gray-800">
|
|
|
|
User profile
|
|
|
|
</h3>
|
|
|
|
<div class="flex justify-center">
|
2022-08-23 05:12:54 +08:00
|
|
|
<.user_avatar user={@user} class="h-20 w-20" text_class="text-3xl" />
|
2021-05-04 02:03:19 +08:00
|
|
|
</div>
|
2022-08-02 21:51:02 +08:00
|
|
|
<.form
|
2022-10-04 14:46:55 +08:00
|
|
|
:let={f}
|
2022-08-23 05:12:54 +08:00
|
|
|
for={@changeset}
|
2022-03-02 07:43:06 +08:00
|
|
|
phx-submit={@on_save |> JS.push("save")}
|
2021-07-07 20:32:49 +08:00
|
|
|
phx-change="validate"
|
|
|
|
phx-target={@myself}
|
|
|
|
id="user_form"
|
2022-08-02 21:51:02 +08:00
|
|
|
phx-hook="UserForm"
|
|
|
|
>
|
2021-05-04 02:03:19 +08:00
|
|
|
<div class="flex flex-col space-y-5">
|
2023-06-21 06:25:25 +08:00
|
|
|
<.text_field
|
|
|
|
field={f[:name]}
|
|
|
|
label="Display name"
|
|
|
|
spellcheck="false"
|
2023-09-18 19:41:58 +08:00
|
|
|
disabled={Livebook.Config.identity_provider_read_only?()}
|
2023-06-21 06:25:25 +08:00
|
|
|
/>
|
2023-06-30 05:10:00 +08:00
|
|
|
<%= if @user.email do %>
|
|
|
|
<.text_field field={f[:email]} label="email" spellcheck="false" disabled="true" />
|
|
|
|
<% end %>
|
2023-02-23 02:34:54 +08:00
|
|
|
<.hex_color_field
|
|
|
|
field={f[:hex_color]}
|
|
|
|
label="Cursor color"
|
|
|
|
randomize={JS.push("randomize_color", target: @myself)}
|
|
|
|
/>
|
2024-02-09 02:27:16 +08:00
|
|
|
<.button type="submit" disabled={not @changeset.valid?}>
|
2021-07-07 20:32:49 +08:00
|
|
|
<.remix_icon icon="save-line" />
|
2021-05-04 02:03:19 +08:00
|
|
|
<span>Save</span>
|
2024-02-09 02:27:16 +08:00
|
|
|
</.button>
|
2021-05-04 02:03:19 +08:00
|
|
|
</div>
|
2021-07-07 20:32:49 +08:00
|
|
|
</.form>
|
2021-05-04 02:03:19 +08:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("randomize_color", %{}, socket) do
|
2022-08-23 05:12:54 +08:00
|
|
|
hex_color = HexColor.random(except: [socket.assigns.user.hex_color])
|
|
|
|
handle_event("validate", %{"user" => %{"hex_color" => hex_color}}, socket)
|
2021-05-04 02:03:19 +08:00
|
|
|
end
|
|
|
|
|
2022-08-23 05:12:54 +08:00
|
|
|
def handle_event("validate", %{"user" => params}, socket) do
|
2023-02-24 02:00:03 +08:00
|
|
|
changeset =
|
|
|
|
socket.assigns.user
|
|
|
|
|> Users.change_user(params)
|
|
|
|
|> Map.put(:action, :validate)
|
2022-08-23 05:12:54 +08:00
|
|
|
|
|
|
|
user =
|
|
|
|
if changeset.valid? do
|
|
|
|
Ecto.Changeset.apply_action!(changeset, :update)
|
|
|
|
else
|
|
|
|
socket.assigns.user
|
2021-05-04 02:03:19 +08:00
|
|
|
end
|
|
|
|
|
2023-02-24 02:00:03 +08:00
|
|
|
{:noreply, assign(socket, changeset: changeset, user: user)}
|
2021-05-04 02:03:19 +08:00
|
|
|
end
|
|
|
|
|
2022-08-23 05:12:54 +08:00
|
|
|
def handle_event("save", %{"user" => params}, socket) do
|
2023-02-24 02:00:03 +08:00
|
|
|
case Users.update_user(socket.assigns.user, params) do
|
2022-08-23 05:12:54 +08:00
|
|
|
{:ok, user} ->
|
2023-02-24 02:00:03 +08:00
|
|
|
{:noreply, assign(socket, changeset: Users.change_user(user), user: user)}
|
2022-08-23 05:12:54 +08:00
|
|
|
|
|
|
|
{:error, changeset} ->
|
2023-02-24 02:00:03 +08:00
|
|
|
{:noreply, assign(socket, changeset: changeset)}
|
2022-08-23 05:12:54 +08:00
|
|
|
end
|
2021-05-04 02:03:19 +08:00
|
|
|
end
|
|
|
|
end
|