mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-10-18 09:27:14 +08:00
Don't validate changesets on mount (#1724)
This commit is contained in:
parent
a69cdf4986
commit
36e2d2b277
13 changed files with 57 additions and 32 deletions
|
@ -36,6 +36,14 @@ defmodule Livebook.Hubs.Enterprise do
|
|||
"""
|
||||
@spec change_hub(t(), map()) :: Ecto.Changeset.t()
|
||||
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
|
||||
|> changeset(attrs)
|
||||
|> Map.put(:action, :validate)
|
||||
|
|
|
@ -42,6 +42,14 @@ defmodule Livebook.Hubs.Fly do
|
|||
"""
|
||||
@spec change_hub(t(), map()) :: Ecto.Changeset.t()
|
||||
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
|
||||
|> changeset(attrs)
|
||||
|> Map.put(:action, :validate)
|
||||
|
|
|
@ -24,6 +24,14 @@ defmodule Livebook.Hubs.Personal do
|
|||
"""
|
||||
@spec change_hub(t(), map()) :: Ecto.Changeset.t()
|
||||
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
|
||||
|> changeset(attrs)
|
||||
|> Map.put(:action, :validate)
|
||||
|
|
|
@ -161,8 +161,7 @@ defmodule Livebook.Settings do
|
|||
With success, notifies interested processes about environment variable
|
||||
data change. Otherwise, it will return an error tuple with changeset.
|
||||
"""
|
||||
@spec set_env_var(EnvVar.t(), map()) ::
|
||||
{:ok, EnvVar.t()} | {:error, Ecto.Changeset.t()}
|
||||
@spec set_env_var(EnvVar.t(), map()) :: {:ok, EnvVar.t()} | {:error, Ecto.Changeset.t()}
|
||||
def set_env_var(%EnvVar{} = env_var \\ %EnvVar{}, attrs) do
|
||||
changeset = EnvVar.changeset(env_var, attrs)
|
||||
|
||||
|
@ -200,9 +199,7 @@ defmodule Livebook.Settings do
|
|||
"""
|
||||
@spec change_env_var(EnvVar.t(), map()) :: Ecto.Changeset.t()
|
||||
def change_env_var(%EnvVar{} = env_var, attrs \\ %{}) do
|
||||
env_var
|
||||
|> EnvVar.changeset(attrs)
|
||||
|> Map.put(:action, :validate)
|
||||
EnvVar.changeset(env_var, attrs)
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
|
|
@ -8,9 +8,7 @@ defmodule Livebook.Users do
|
|||
"""
|
||||
@spec change_user(User.t(), map()) :: Ecto.Changeset.t()
|
||||
def change_user(%User{} = user, attrs \\ %{}) do
|
||||
user
|
||||
|> User.changeset(attrs)
|
||||
|> Map.put(:action, :validate)
|
||||
User.changeset(user, attrs)
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
@ -19,8 +17,10 @@ defmodule Livebook.Users do
|
|||
With success, notifies interested processes about user data change.
|
||||
Otherwise, it will return an error tuple with changeset.
|
||||
"""
|
||||
@spec update_user(Ecto.Changeset.t()) :: {:ok, User.t()} | {:error, Ecto.Changeset.t()}
|
||||
def update_user(%Ecto.Changeset{} = changeset) do
|
||||
@spec update_user(User.t(), map()) :: {:ok, User.t()} | {:error, Ecto.Changeset.t()}
|
||||
def update_user(%User{} = user, attrs \\ %{}) do
|
||||
changeset = User.changeset(user, attrs)
|
||||
|
||||
with {:ok, user} <- Ecto.Changeset.apply_action(changeset, :update) do
|
||||
broadcast_change(user)
|
||||
{:ok, user}
|
||||
|
|
|
@ -11,7 +11,7 @@ defmodule LivebookWeb.EnvVarComponent do
|
|||
do: {assigns.env_var, :edit},
|
||||
else: {%EnvVar{}, :new}
|
||||
|
||||
changeset = EnvVar.changeset(env_var)
|
||||
changeset = Settings.change_env_var(env_var)
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|
@ -69,6 +69,11 @@ defmodule LivebookWeb.EnvVarComponent do
|
|||
|
||||
@impl true
|
||||
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
|
||||
|
|
|
@ -39,9 +39,7 @@ defmodule LivebookWeb.UserHook do
|
|||
connect_params = get_connect_params(socket) || %{}
|
||||
attrs = connect_params["user_data"] || session["user_data"] || %{}
|
||||
|
||||
changeset = User.changeset(user, attrs)
|
||||
|
||||
case Livebook.Users.update_user(changeset) do
|
||||
case Livebook.Users.update_user(user, attrs) do
|
||||
{:ok, user} -> user
|
||||
{:error, _changeset} -> user
|
||||
end
|
||||
|
|
|
@ -68,6 +68,6 @@ defmodule LivebookWeb.Hub.Edit.EnterpriseComponent do
|
|||
end
|
||||
|
||||
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
|
||||
|
|
|
@ -138,7 +138,7 @@ defmodule LivebookWeb.Hub.Edit.FlyComponent do
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
# EnvVar component callbacks
|
||||
|
|
|
@ -69,6 +69,6 @@ defmodule LivebookWeb.Hub.Edit.PersonalComponent do
|
|||
end
|
||||
|
||||
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
|
||||
|
|
|
@ -123,7 +123,7 @@ defmodule LivebookWeb.Hub.New.EnterpriseComponent do
|
|||
case EnterpriseClient.send_request(pid, session_request) do
|
||||
{:session, session_response} ->
|
||||
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)}
|
||||
|
||||
|
@ -160,6 +160,6 @@ defmodule LivebookWeb.Hub.New.EnterpriseComponent do
|
|||
end
|
||||
|
||||
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
|
||||
|
|
|
@ -79,7 +79,7 @@ defmodule LivebookWeb.Hub.New.FlyComponent do
|
|||
{:ok, apps} ->
|
||||
opts = select_options(apps)
|
||||
base = %Fly{access_token: token, hub_emoji: "🚀"}
|
||||
changeset = Fly.change_hub(base)
|
||||
changeset = Fly.validate_hub(base)
|
||||
|
||||
{:noreply,
|
||||
assign(socket, changeset: changeset, base: base, select_options: opts, apps: apps)}
|
||||
|
@ -87,7 +87,7 @@ defmodule LivebookWeb.Hub.New.FlyComponent do
|
|||
{:error, _} ->
|
||||
changeset =
|
||||
%Fly{}
|
||||
|> Fly.change_hub(%{access_token: token})
|
||||
|> Fly.validate_hub(%{access_token: token})
|
||||
|> add_error(:access_token, "is invalid")
|
||||
|
||||
{:noreply,
|
||||
|
@ -118,7 +118,7 @@ defmodule LivebookWeb.Hub.New.FlyComponent do
|
|||
application_id = params["application_id"]
|
||||
selected_app = Enum.find(socket.assigns.apps, &(&1.application_id == application_id))
|
||||
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,
|
||||
assign(socket, changeset: changeset, selected_app: selected_app, select_options: opts)}
|
||||
|
|
|
@ -12,7 +12,7 @@ defmodule LivebookWeb.UserComponent do
|
|||
user = socket.assigns.user
|
||||
changeset = Users.change_user(user)
|
||||
|
||||
{:ok, assign(socket, changeset: changeset, valid?: changeset.valid?, user: user)}
|
||||
{:ok, assign(socket, changeset: changeset, user: user)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
|
@ -44,7 +44,7 @@ defmodule LivebookWeb.UserComponent do
|
|||
<button
|
||||
class="button-base button-blue flex space-x-1 justify-center items-center"
|
||||
type="submit"
|
||||
disabled={not @valid?}
|
||||
disabled={not @changeset.valid?}
|
||||
>
|
||||
<.remix_icon icon="save-line" />
|
||||
<span>Save</span>
|
||||
|
@ -62,7 +62,10 @@ defmodule LivebookWeb.UserComponent do
|
|||
end
|
||||
|
||||
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 =
|
||||
if changeset.valid? do
|
||||
|
@ -71,18 +74,16 @@ defmodule LivebookWeb.UserComponent do
|
|||
socket.assigns.user
|
||||
end
|
||||
|
||||
{:noreply, assign(socket, changeset: changeset, valid?: changeset.valid?, user: user)}
|
||||
{:noreply, assign(socket, changeset: changeset, 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
|
||||
case Users.update_user(socket.assigns.user, params) do
|
||||
{:ok, user} ->
|
||||
{:noreply, assign(socket, changeset: changeset, valid?: changeset.valid?, user: user)}
|
||||
{:noreply, assign(socket, changeset: Users.change_user(user), user: user)}
|
||||
|
||||
{:error, changeset} ->
|
||||
{:noreply, assign(socket, changeset: changeset, valid?: changeset.valid?)}
|
||||
{:noreply, assign(socket, changeset: changeset)}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue