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
|
|
|
|
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">
|
|
|
|
<div>
|
|
|
|
<div class="input-label">Display name</div>
|
2022-08-23 05:12:54 +08:00
|
|
|
<%= text_input(f, :name, class: "input", spellcheck: "false") %>
|
|
|
|
<%= error_tag(f, :name) %>
|
2021-05-04 02:03:19 +08:00
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<div class="input-label">Cursor color</div>
|
|
|
|
<div class="flex space-x-4 items-center">
|
2022-08-02 21:51:02 +08:00
|
|
|
<div
|
|
|
|
class="border-[3px] rounded-lg p-1 flex justify-center items-center"
|
2022-08-23 05:12:54 +08:00
|
|
|
style={"border-color: #{hex_color(@changeset)}"}
|
2022-08-02 21:51:02 +08:00
|
|
|
>
|
2022-08-23 05:12:54 +08:00
|
|
|
<div class="rounded h-5 w-5" style={"background-color: #{hex_color(@changeset)}"}>
|
2021-05-04 02:03:19 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-12-30 05:06:19 +08:00
|
|
|
<div class="relative grow">
|
2022-08-02 21:51:02 +08:00
|
|
|
<%= text_input(f, :hex_color,
|
|
|
|
class: "input",
|
|
|
|
spellcheck: "false",
|
|
|
|
maxlength: 7
|
|
|
|
) %>
|
2021-07-07 20:32:49 +08:00
|
|
|
<button
|
|
|
|
class="icon-button absolute right-2 top-1"
|
|
|
|
type="button"
|
|
|
|
phx-click="randomize_color"
|
2022-08-02 21:51:02 +08:00
|
|
|
phx-target={@myself}
|
|
|
|
>
|
2021-07-07 20:32:49 +08:00
|
|
|
<.remix_icon icon="refresh-line" class="text-xl" />
|
2021-05-04 02:03:19 +08:00
|
|
|
</button>
|
2022-08-23 05:12:54 +08:00
|
|
|
<%= error_tag(f, :hex_color) %>
|
2021-05-04 02:03:19 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
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
|
2022-08-23 05:12:54 +08:00
|
|
|
|
|
|
|
defp hex_color(changeset), do: Ecto.Changeset.get_field(changeset, :hex_color)
|
2021-05-04 02:03:19 +08:00
|
|
|
end
|