2021-05-04 02:03:19 +08:00
|
|
|
defmodule LivebookWeb.UserComponent do
|
|
|
|
use LivebookWeb, :live_component
|
|
|
|
|
|
|
|
import LivebookWeb.UserHelpers
|
|
|
|
|
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
|
|
|
|
2022-08-23 05:12:54 +08:00
|
|
|
{:ok, assign(socket, changeset: changeset, valid?: changeset.valid?, 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">
|
2022-09-06 05:59:13 +08:00
|
|
|
<.input_wrapper form={f} field={:name}>
|
2021-05-04 02:03:19 +08:00
|
|
|
<div class="input-label">Display name</div>
|
2022-08-23 05:12:54 +08:00
|
|
|
<%= text_input(f, :name, class: "input", spellcheck: "false") %>
|
2022-09-06 05:59:13 +08:00
|
|
|
</.input_wrapper>
|
|
|
|
<.input_wrapper form={f} field={:hex_color}>
|
2021-05-04 02:03:19 +08:00
|
|
|
<div class="input-label">Cursor color</div>
|
2022-09-06 05:59:13 +08:00
|
|
|
<.hex_color_input
|
|
|
|
form={f}
|
|
|
|
field={:hex_color}
|
|
|
|
randomize={JS.push("randomize_color", target: @myself)}
|
|
|
|
/>
|
|
|
|
</.input_wrapper>
|
2021-07-07 20:32:49 +08:00
|
|
|
<button
|
2021-12-04 04:57:21 +08:00
|
|
|
class="button-base button-blue flex space-x-1 justify-center items-center"
|
2021-07-07 20:32:49 +08:00
|
|
|
type="submit"
|
2022-08-23 05:12:54 +08:00
|
|
|
disabled={not @valid?}
|
2022-08-02 21:51:02 +08:00
|
|
|
>
|
2021-07-07 20:32:49 +08:00
|
|
|
<.remix_icon icon="save-line" />
|
2021-05-04 02:03:19 +08:00
|
|
|
<span>Save</span>
|
|
|
|
</button>
|
|
|
|
</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
|
|
|
|
changeset = Users.change_user(socket.assigns.user, params)
|
|
|
|
|
|
|
|
user =
|
|
|
|
if changeset.valid? do
|
|
|
|
Ecto.Changeset.apply_action!(changeset, :update)
|
|
|
|
else
|
|
|
|
socket.assigns.user
|
2021-05-04 02:03:19 +08:00
|
|
|
end
|
|
|
|
|
2022-08-23 05:12:54 +08:00
|
|
|
{:noreply, assign(socket, changeset: changeset, valid?: changeset.valid?, 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
|
|
|
|
changeset = Users.change_user(socket.assigns.user, params)
|
|
|
|
|
|
|
|
case Users.update_user(changeset) do
|
|
|
|
{:ok, user} ->
|
|
|
|
{:noreply, assign(socket, changeset: changeset, valid?: changeset.valid?, user: user)}
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
{:noreply, assign(socket, changeset: changeset, valid?: changeset.valid?)}
|
|
|
|
end
|
2021-05-04 02:03:19 +08:00
|
|
|
end
|
|
|
|
end
|