defmodule ServerWeb.AdminHostsLive do use ServerWeb, :live_view alias Server.{Hosts, Repo, Schema.Host} @impl true def mount(_params, _session, socket) do {:ok, socket |> assign(:hosts, Hosts.list_all()) |> assign(:new_token, nil) |> assign(:error, nil)} end @impl true def handle_event("create", %{"host" => %{"name" => name}}, socket) do case Hosts.create_host(name) do {:ok, {host, token}} -> {:noreply, socket |> assign(:hosts, Hosts.list_all()) |> assign(:new_token, %{name: host.name, token: token}) |> assign(:error, nil)} {:error, cs} -> {:noreply, assign(socket, :error, changeset_message(cs))} end end def handle_event("rotate", %{"id" => id}, socket) do %Host{} = host = Repo.get!(Host, id) {:ok, {_, token}} = Hosts.rotate_token(host) {:noreply, socket |> assign(:hosts, Hosts.list_all()) |> assign(:new_token, %{name: host.name, token: token})} end def handle_event("delete", %{"id" => id}, socket) do %Host{} = host = Repo.get!(Host, id) {:ok, _} = Hosts.delete_host(host) {:noreply, assign(socket, :hosts, Hosts.list_all())} end defp changeset_message(cs) do cs.errors |> Enum.map_join(", ", fn {k, {msg, _}} -> "#{k}: #{msg}" end) end @impl true def render(assigns) do ~H"""
{@error}
Token for {@new_token.name} (shown once):
{@new_token.token}
| Name | Status | Agent | Last seen | Actions |
|---|---|---|---|---|
| {h.name} | {h.status} | {h.agent_version || "—"} | {format_seen(h.last_seen_at)} | |
| No hosts yet. | ||||