Don't validate changesets on mount (#1724)

This commit is contained in:
Jonatan Kłosko 2023-02-23 19:00:03 +01:00 committed by GitHub
parent a69cdf4986
commit 36e2d2b277
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 57 additions and 32 deletions

View file

@ -36,6 +36,14 @@ defmodule Livebook.Hubs.Enterprise do
""" """
@spec change_hub(t(), map()) :: Ecto.Changeset.t() @spec change_hub(t(), map()) :: Ecto.Changeset.t()
def change_hub(%__MODULE__{} = enterprise, attrs \\ %{}) do def change_hub(%__MODULE__{} = enterprise, attrs \\ %{}) do
changeset(enterprise, attrs)
end
@doc """
Returns changeset with applied validations.
"""
@spec validate_hub(t(), map()) :: Ecto.Changeset.t()
def validate_hub(%__MODULE__{} = enterprise, attrs \\ %{}) do
enterprise enterprise
|> changeset(attrs) |> changeset(attrs)
|> Map.put(:action, :validate) |> Map.put(:action, :validate)

View file

@ -42,6 +42,14 @@ defmodule Livebook.Hubs.Fly do
""" """
@spec change_hub(t(), map()) :: Ecto.Changeset.t() @spec change_hub(t(), map()) :: Ecto.Changeset.t()
def change_hub(%__MODULE__{} = fly, attrs \\ %{}) do def change_hub(%__MODULE__{} = fly, attrs \\ %{}) do
changeset(fly, attrs)
end
@doc """
Returns changeset with applied validations.
"""
@spec validate_hub(t(), map()) :: Ecto.Changeset.t()
def validate_hub(%__MODULE__{} = fly, attrs \\ %{}) do
fly fly
|> changeset(attrs) |> changeset(attrs)
|> Map.put(:action, :validate) |> Map.put(:action, :validate)

View file

@ -24,6 +24,14 @@ defmodule Livebook.Hubs.Personal do
""" """
@spec change_hub(t(), map()) :: Ecto.Changeset.t() @spec change_hub(t(), map()) :: Ecto.Changeset.t()
def change_hub(%__MODULE__{} = personal, attrs \\ %{}) do def change_hub(%__MODULE__{} = personal, attrs \\ %{}) do
changeset(personal, attrs)
end
@doc """
Returns changeset with applied validations.
"""
@spec validate_hub(t(), map()) :: Ecto.Changeset.t()
def validate_hub(%__MODULE__{} = personal, attrs \\ %{}) do
personal personal
|> changeset(attrs) |> changeset(attrs)
|> Map.put(:action, :validate) |> Map.put(:action, :validate)

View file

@ -161,8 +161,7 @@ defmodule Livebook.Settings do
With success, notifies interested processes about environment variable With success, notifies interested processes about environment variable
data change. Otherwise, it will return an error tuple with changeset. data change. Otherwise, it will return an error tuple with changeset.
""" """
@spec set_env_var(EnvVar.t(), map()) :: @spec set_env_var(EnvVar.t(), map()) :: {:ok, EnvVar.t()} | {:error, Ecto.Changeset.t()}
{:ok, EnvVar.t()} | {:error, Ecto.Changeset.t()}
def set_env_var(%EnvVar{} = env_var \\ %EnvVar{}, attrs) do def set_env_var(%EnvVar{} = env_var \\ %EnvVar{}, attrs) do
changeset = EnvVar.changeset(env_var, attrs) changeset = EnvVar.changeset(env_var, attrs)
@ -200,9 +199,7 @@ defmodule Livebook.Settings do
""" """
@spec change_env_var(EnvVar.t(), map()) :: Ecto.Changeset.t() @spec change_env_var(EnvVar.t(), map()) :: Ecto.Changeset.t()
def change_env_var(%EnvVar{} = env_var, attrs \\ %{}) do def change_env_var(%EnvVar{} = env_var, attrs \\ %{}) do
env_var EnvVar.changeset(env_var, attrs)
|> EnvVar.changeset(attrs)
|> Map.put(:action, :validate)
end end
@doc """ @doc """

View file

@ -8,9 +8,7 @@ defmodule Livebook.Users do
""" """
@spec change_user(User.t(), map()) :: Ecto.Changeset.t() @spec change_user(User.t(), map()) :: Ecto.Changeset.t()
def change_user(%User{} = user, attrs \\ %{}) do def change_user(%User{} = user, attrs \\ %{}) do
user User.changeset(user, attrs)
|> User.changeset(attrs)
|> Map.put(:action, :validate)
end end
@doc """ @doc """
@ -19,8 +17,10 @@ defmodule Livebook.Users do
With success, notifies interested processes about user data change. With success, notifies interested processes about user data change.
Otherwise, it will return an error tuple with changeset. Otherwise, it will return an error tuple with changeset.
""" """
@spec update_user(Ecto.Changeset.t()) :: {:ok, User.t()} | {:error, Ecto.Changeset.t()} @spec update_user(User.t(), map()) :: {:ok, User.t()} | {:error, Ecto.Changeset.t()}
def update_user(%Ecto.Changeset{} = changeset) do def update_user(%User{} = user, attrs \\ %{}) do
changeset = User.changeset(user, attrs)
with {:ok, user} <- Ecto.Changeset.apply_action(changeset, :update) do with {:ok, user} <- Ecto.Changeset.apply_action(changeset, :update) do
broadcast_change(user) broadcast_change(user)
{:ok, user} {:ok, user}

View file

@ -11,7 +11,7 @@ defmodule LivebookWeb.EnvVarComponent do
do: {assigns.env_var, :edit}, do: {assigns.env_var, :edit},
else: {%EnvVar{}, :new} else: {%EnvVar{}, :new}
changeset = EnvVar.changeset(env_var) changeset = Settings.change_env_var(env_var)
{:ok, {:ok,
socket socket
@ -69,6 +69,11 @@ defmodule LivebookWeb.EnvVarComponent do
@impl true @impl true
def handle_event("validate", %{"env_var" => attrs}, socket) do def handle_event("validate", %{"env_var" => attrs}, socket) do
{:noreply, assign(socket, changeset: Settings.change_env_var(socket.assigns.env_var, attrs))} changeset =
socket.assigns.env_var
|> Settings.change_env_var(attrs)
|> Map.put(:action, :validate)
{:noreply, assign(socket, changeset: changeset)}
end end
end end

View file

@ -39,9 +39,7 @@ defmodule LivebookWeb.UserHook do
connect_params = get_connect_params(socket) || %{} connect_params = get_connect_params(socket) || %{}
attrs = connect_params["user_data"] || session["user_data"] || %{} attrs = connect_params["user_data"] || session["user_data"] || %{}
changeset = User.changeset(user, attrs) case Livebook.Users.update_user(user, attrs) do
case Livebook.Users.update_user(changeset) do
{:ok, user} -> user {:ok, user} -> user
{:error, _changeset} -> user {:error, _changeset} -> user
end end

View file

@ -68,6 +68,6 @@ defmodule LivebookWeb.Hub.Edit.EnterpriseComponent do
end end
def handle_event("validate", %{"enterprise" => attrs}, socket) do def handle_event("validate", %{"enterprise" => attrs}, socket) do
{:noreply, assign(socket, changeset: Enterprise.change_hub(socket.assigns.hub, attrs))} {:noreply, assign(socket, changeset: Enterprise.validate_hub(socket.assigns.hub, attrs))}
end end
end end

View file

@ -138,7 +138,7 @@ defmodule LivebookWeb.Hub.Edit.FlyComponent do
end end
def handle_event("validate", %{"fly" => attrs}, socket) do def handle_event("validate", %{"fly" => attrs}, socket) do
{:noreply, assign(socket, changeset: Fly.change_hub(socket.assigns.hub, attrs))} {:noreply, assign(socket, changeset: Fly.validate_hub(socket.assigns.hub, attrs))}
end end
# EnvVar component callbacks # EnvVar component callbacks

View file

@ -69,6 +69,6 @@ defmodule LivebookWeb.Hub.Edit.PersonalComponent do
end end
def handle_event("validate", %{"personal" => attrs}, socket) do def handle_event("validate", %{"personal" => attrs}, socket) do
{:noreply, assign(socket, changeset: Personal.change_hub(socket.assigns.hub, attrs))} {:noreply, assign(socket, changeset: Personal.validate_hub(socket.assigns.hub, attrs))}
end end
end end

View file

@ -123,7 +123,7 @@ defmodule LivebookWeb.Hub.New.EnterpriseComponent do
case EnterpriseClient.send_request(pid, session_request) do case EnterpriseClient.send_request(pid, session_request) do
{:session, session_response} -> {:session, session_response} ->
base = %{base | external_id: session_response.id} base = %{base | external_id: session_response.id}
changeset = Enterprise.change_hub(base) changeset = Enterprise.validate_hub(base)
{:noreply, assign(socket, pid: pid, changeset: changeset, base: base)} {:noreply, assign(socket, pid: pid, changeset: changeset, base: base)}
@ -160,6 +160,6 @@ defmodule LivebookWeb.Hub.New.EnterpriseComponent do
end end
def handle_event("validate", %{"enterprise" => attrs}, socket) do def handle_event("validate", %{"enterprise" => attrs}, socket) do
{:noreply, assign(socket, changeset: Enterprise.change_hub(socket.assigns.base, attrs))} {:noreply, assign(socket, changeset: Enterprise.validate_hub(socket.assigns.base, attrs))}
end end
end end

View file

@ -79,7 +79,7 @@ defmodule LivebookWeb.Hub.New.FlyComponent do
{:ok, apps} -> {:ok, apps} ->
opts = select_options(apps) opts = select_options(apps)
base = %Fly{access_token: token, hub_emoji: "🚀"} base = %Fly{access_token: token, hub_emoji: "🚀"}
changeset = Fly.change_hub(base) changeset = Fly.validate_hub(base)
{:noreply, {:noreply,
assign(socket, changeset: changeset, base: base, select_options: opts, apps: apps)} assign(socket, changeset: changeset, base: base, select_options: opts, apps: apps)}
@ -87,7 +87,7 @@ defmodule LivebookWeb.Hub.New.FlyComponent do
{:error, _} -> {:error, _} ->
changeset = changeset =
%Fly{} %Fly{}
|> Fly.change_hub(%{access_token: token}) |> Fly.validate_hub(%{access_token: token})
|> add_error(:access_token, "is invalid") |> add_error(:access_token, "is invalid")
{:noreply, {:noreply,
@ -118,7 +118,7 @@ defmodule LivebookWeb.Hub.New.FlyComponent do
application_id = params["application_id"] application_id = params["application_id"]
selected_app = Enum.find(socket.assigns.apps, &(&1.application_id == application_id)) selected_app = Enum.find(socket.assigns.apps, &(&1.application_id == application_id))
opts = select_options(socket.assigns.apps) opts = select_options(socket.assigns.apps)
changeset = Fly.change_hub(selected_app || socket.assigns.base, params) changeset = Fly.validate_hub(selected_app || socket.assigns.base, params)
{:noreply, {:noreply,
assign(socket, changeset: changeset, selected_app: selected_app, select_options: opts)} assign(socket, changeset: changeset, selected_app: selected_app, select_options: opts)}

View file

@ -12,7 +12,7 @@ defmodule LivebookWeb.UserComponent do
user = socket.assigns.user user = socket.assigns.user
changeset = Users.change_user(user) changeset = Users.change_user(user)
{:ok, assign(socket, changeset: changeset, valid?: changeset.valid?, user: user)} {:ok, assign(socket, changeset: changeset, user: user)}
end end
@impl true @impl true
@ -44,7 +44,7 @@ defmodule LivebookWeb.UserComponent do
<button <button
class="button-base button-blue flex space-x-1 justify-center items-center" class="button-base button-blue flex space-x-1 justify-center items-center"
type="submit" type="submit"
disabled={not @valid?} disabled={not @changeset.valid?}
> >
<.remix_icon icon="save-line" /> <.remix_icon icon="save-line" />
<span>Save</span> <span>Save</span>
@ -62,7 +62,10 @@ defmodule LivebookWeb.UserComponent do
end end
def handle_event("validate", %{"user" => params}, socket) do def handle_event("validate", %{"user" => params}, socket) do
changeset = Users.change_user(socket.assigns.user, params) changeset =
socket.assigns.user
|> Users.change_user(params)
|> Map.put(:action, :validate)
user = user =
if changeset.valid? do if changeset.valid? do
@ -71,18 +74,16 @@ defmodule LivebookWeb.UserComponent do
socket.assigns.user socket.assigns.user
end end
{:noreply, assign(socket, changeset: changeset, valid?: changeset.valid?, user: user)} {:noreply, assign(socket, changeset: changeset, user: user)}
end end
def handle_event("save", %{"user" => params}, socket) do def handle_event("save", %{"user" => params}, socket) do
changeset = Users.change_user(socket.assigns.user, params) case Users.update_user(socket.assigns.user, params) do
case Users.update_user(changeset) do
{:ok, user} -> {:ok, user} ->
{:noreply, assign(socket, changeset: changeset, valid?: changeset.valid?, user: user)} {:noreply, assign(socket, changeset: Users.change_user(user), user: user)}
{:error, changeset} -> {:error, changeset} ->
{:noreply, assign(socket, changeset: changeset, valid?: changeset.valid?)} {:noreply, assign(socket, changeset: changeset)}
end end
end end
end end