defmodule LivebookWeb.SessionLive.SecretsComponent do use LivebookWeb, :live_component @impl true def render(assigns) do ~H"""

Add secret

Enter the secret name and its value.

<.form let={f} for={:secret} phx-submit="save" phx-change="validate" autocomplete="off" phx-target={@myself} >
Label (alphanumeric and underscore)
<%= text_input(f, :label, value: @secret["label"], class: "input", placeholder: "secret label", autofocus: true, aria_labelledby: "secret-label", spellcheck: "false" ) %>
Value
<%= text_input(f, :value, value: @secret["value"], class: "input", placeholder: "secret value", aria_labelledby: "secret-value", spellcheck: "false" ) %>
<%= live_patch("Cancel", to: @return_to, class: "button-base button-outlined-gray") %>
""" end @impl true def handle_event("save", %{"secret" => secret}, socket) do secret = %{label: String.upcase(secret["label"]), value: secret["value"]} Livebook.Session.put_secret(socket.assigns.session.pid, secret) {:noreply, assign(socket, secret: %{"label" => "", "value" => ""})} end def handle_event("validate", %{"secret" => secret}, socket) do {:noreply, assign(socket, secret: secret)} end defp valid?(secret) do String.match?(secret["label"], ~r/^\w+$/) and secret["value"] != "" end end