prop/lib/microwaveprop_web/live/user_management_live/index.ex
Graham McIntire ee9275e0b9 Add /beacons CRUD and /users admin page
Beacons:
- Scaffolded with phx.gen.live then reworked so reads are public
  and mutations go through a live_session gated by the new
  :require_admin on_mount hook in UserAuth
- Beacon schema stores frequency (MHz), callsign, grid, lat/lon,
  power (W), and height above ground (m); grid auto-derives from
  lat/lon when left blank via Maidenhead.from_latlon
- Adds Maidenhead.from_latlon/3 so we can compute grids locally
  instead of hitting an external API

Users admin page:
- /users and /users/:id/edit (admin-only) for listing, editing
  (callsign/name/email/is_admin), and deleting other users
- Adds Accounts.list_users, admin_update_user, delete_user, and
  a dedicated admin_changeset on the User schema
- Nav gains a "Users" link for admins and a "Beacons" link for
  everyone
2026-04-08 12:01:45 -05:00

70 lines
2.2 KiB
Elixir

defmodule MicrowavepropWeb.UserManagementLive.Index do
@moduledoc false
use MicrowavepropWeb, :live_view
alias Microwaveprop.Accounts
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope} max_width="max-w-6xl">
<.header>
Users
<:subtitle>Manage registered accounts and admin flags.</:subtitle>
</.header>
<.table id="users" rows={@streams.users}>
<:col :let={{_id, user}} label="Callsign">{user.callsign}</:col>
<:col :let={{_id, user}} label="Name">{user.name}</:col>
<:col :let={{_id, user}} label="Email">{user.email}</:col>
<:col :let={{_id, user}} label="Admin">
<%= if user.is_admin do %>
<span class="badge badge-primary badge-sm">admin</span>
<% else %>
<span class="opacity-50">&mdash;</span>
<% end %>
</:col>
<:col :let={{_id, user}} label="Confirmed">
<%= if user.confirmed_at do %>
{Calendar.strftime(user.confirmed_at, "%Y-%m-%d")}
<% else %>
<span class="opacity-50">pending</span>
<% end %>
</:col>
<:action :let={{_id, user}}>
<.link navigate={~p"/users/#{user.id}/edit"}>Edit</.link>
</:action>
<:action :let={{id, user}}>
<.link
:if={user.id != @current_scope.user.id}
phx-click={JS.push("delete", value: %{id: user.id}) |> hide("##{id}")}
data-confirm={"Delete user #{user.callsign}? This cannot be undone."}
>
Delete
</.link>
</:action>
</.table>
</Layouts.app>
"""
end
@impl true
def mount(_params, _session, socket) do
{:ok,
socket
|> assign(:page_title, "Users")
|> stream(:users, Accounts.list_users())}
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
user = Accounts.get_user!(id)
if user.id == socket.assigns.current_scope.user.id do
{:noreply, put_flash(socket, :error, "You cannot delete your own account here.")}
else
{:ok, _} = Accounts.delete_user(user)
{:noreply, stream_delete(socket, :users, user)}
end
end
end