defmodule LivebookWeb.UserComponent do
use LivebookWeb, :live_component
import LivebookWeb.UserHelpers
alias Livebook.EctoTypes.HexColor
alias Livebook.Users
@impl true
def update(assigns, socket) do
socket = assign(socket, assigns)
user = socket.assigns.user
changeset = Users.change_user(user)
{:ok, assign(socket, changeset: changeset, valid?: changeset.valid?, user: user)}
end
@impl true
def render(assigns) do
~H"""
User profile
<.user_avatar user={@user} class="h-20 w-20" text_class="text-3xl" />
<.form
:let={f}
for={@changeset}
phx-submit={@on_save |> JS.push("save")}
phx-change="validate"
phx-target={@myself}
id="user_form"
phx-hook="UserForm"
>
<.input_wrapper form={f} field={:name}>
Display name
<%= text_input(f, :name, class: "input", spellcheck: "false") %>
<.input_wrapper form={f} field={:hex_color}>
Cursor color
<.hex_color_input
form={f}
field={:hex_color}
randomize={JS.push("randomize_color", target: @myself)}
/>
"""
end
@impl true
def handle_event("randomize_color", %{}, socket) do
hex_color = HexColor.random(except: [socket.assigns.user.hex_color])
handle_event("validate", %{"user" => %{"hex_color" => hex_color}}, socket)
end
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
end
{:noreply, assign(socket, changeset: changeset, valid?: changeset.valid?, user: user)}
end
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
end
end