defmodule LivebookWeb.SessionLive.SecretsComponent do use LivebookWeb, :live_component @impl true def mount(socket) do {:ok, assign(socket, data: %{"label" => "", "value" => ""})} end @impl true def update(assigns, socket) do socket = assign(socket, assigns) {:ok, socket} end @impl true def render(assigns) do ~H"""

Add secret

Enter the secret name and its value.

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