defmodule LivebookWeb.EnvVarComponent do use LivebookWeb, :live_component alias Livebook.Settings alias Livebook.Settings.EnvVar @impl true def update(assigns, socket) do {env_var, operation} = if assigns.env_var, do: {assigns.env_var, :edit}, else: {%EnvVar{}, :new} changeset = Settings.change_env_var(env_var) {:ok, socket |> assign(assigns) |> assign(changeset: changeset, env_var: env_var, operation: operation)} end @impl true def render(assigns) do assigns = assign_new(assigns, :on_save, fn -> "save" end) ~H"""

<%= if @operation == :new, do: "Add environment variable", else: "Edit environment variable" %>

<%= @headline %>

<.form :let={f} id={"#{@id}-form"} for={@changeset} phx-submit={@on_save} phx-change={JS.push("validate", target: @myself)} autocomplete="off" spellcheck="false" >
<.input_wrapper form={f} field={:name} class="flex flex-col space-y-1">
Name (alphanumeric and underscore)
<%= text_input(f, :name, class: "input", autofocus: @operation == :new) %> <.input_wrapper form={f} field={:value} class="flex flex-col space-y-1">
Value
<%= text_input(f, :value, class: "input", autofocus: @operation == :edit) %> <%= text_input(f, :operation, type: "hidden", value: @operation) %>
<%= submit("Save", class: "button-base button-blue", disabled: not @changeset.valid?, phx_disabled_with: "Adding..." ) %> <%= live_patch("Cancel", to: @return_to, type: "button", class: "button-base button-outlined-gray" ) %>
""" end @impl true def handle_event("validate", %{"env_var" => attrs}, socket) do {:noreply, assign(socket, changeset: Settings.change_env_var(socket.assigns.env_var, attrs))} end end