defmodule LivebookWeb.UserComponent do use LivebookWeb, :live_component import LivebookWeb.UserHelpers alias Livebook.Users.User @impl true def update(assigns, socket) do socket = assign(socket, assigns) user = socket.assigns.user {:ok, assign(socket, data: user_to_data(user), valid: true, preview_user: user)} end defp user_to_data(user) do %{"name" => user.name || "", "hex_color" => user.hex_color} end @impl true def render(assigns) do ~H"""

User profile

<.user_avatar user={@preview_user} class="h-20 w-20" text_class="text-3xl" />
<.form let={f} for={:data} phx-submit={@on_save |> JS.push("save")} phx-change="validate" phx-target={@myself} id="user_form" phx-hook="UserForm">
Display name
<%= text_input f, :name, value: @data["name"], class: "input", spellcheck: "false" %>
Cursor color
<%= text_input f, :hex_color, value: @data["hex_color"], class: "input", spellcheck: "false", maxlength: 7 %>
""" end @impl true def handle_event("randomize_color", %{}, socket) do data = %{ socket.assigns.data | "hex_color" => User.random_hex_color(except: [socket.assigns.preview_user.hex_color]) } handle_event("validate", %{"data" => data}, socket) end def handle_event("validate", %{"data" => data}, socket) do {valid, user} = case User.change(socket.assigns.user, data) do {:ok, user} -> {true, user} {:error, _errors, user} -> {false, user} end {:noreply, assign(socket, data: data, valid: valid, preview_user: user)} end def handle_event("save", %{"data" => data}, socket) do {:ok, user} = User.change(socket.assigns.user, data) Livebook.Users.broadcast_change(user) {:noreply, socket} end end