prop/lib/microwaveprop_web/live/user_management_live/index.ex
Graham McInitre 49ade78766 fix: wire pending_edits_query as data_provider for contact edit review table
The LiveTable on /admin/contact-edits used the bare ContactEdit schema
as its data source, which caused three symptoms:
- '0 pending' counter but stale approved/rejected edits still visible
- Blank contact/submitted-by cells (select_columns stripped preloaded
  associations, cell renderers received flat maps with no :contact/:user)
- Approve/reject didn't remove the row from the table

Fix: assign {Radio, :pending_edits_query, []} as the data_provider in
mount so handle_params threads it to stream_resources. The query variant
of list_resources preserves preloaded associations and includes the
WHERE status = :pending filter.

Added two tests that verify the table rendering and edit removal.
2026-07-22 08:54:46 -05:00

111 lines
2.9 KiB
Elixir

defmodule MicrowavepropWeb.UserManagementLive.Index do
@moduledoc "Admin user list at `/admin/users`."
use MicrowavepropWeb, :live_view
use MicrowavepropWeb.LiveTableResource, schema: Microwaveprop.Accounts.User
alias Microwaveprop.Accounts
@spec table_options() :: map()
def table_options do
%{exports: %{formats: [:csv]}}
end
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :page_title, "Users")}
end
@spec fields() :: keyword()
def fields do
[
id: %{label: "ID", hidden: true},
callsign: %{label: "Callsign", sortable: true, searchable: true},
name: %{label: "Name", sortable: true, searchable: true},
email: %{label: "Email", sortable: true, searchable: true},
is_admin: %{label: "Admin", sortable: true, renderer: &admin_badge/1},
confirmed_at: %{label: "Confirmed", sortable: true, renderer: &confirmed_cell/1}
]
end
@spec filters() :: list()
def filters, do: []
@spec actions() :: keyword()
def actions do
[
edit: fn %{record: user} ->
assigns = %{user: user}
~H"""
<.link navigate={~p"/users/#{@user.id}/edit"} class="btn btn-xs btn-ghost">Edit</.link>
"""
end,
delete: fn %{record: user} ->
assigns = %{user: user}
~H"""
<.link
phx-click={JS.push("delete", value: %{id: @user.id})}
data-confirm={"Delete user #{@user.callsign}? This cannot be undone."}
class="btn btn-xs btn-ghost text-error"
>
Delete
</.link>
"""
end
]
end
@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>
<.live_table
fields={fields()}
filters={filters()}
options={@options}
streams={@streams}
actions={actions()}
/>
</Layouts.app>
"""
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
case Accounts.get_user!(id) do
nil ->
{:noreply, put_flash(socket, :error, "User not found.")}
user ->
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, :resources, user)}
end
end
end
defp admin_badge(true) do
assigns = %{}
~H|<span class="badge badge-primary badge-sm">admin</span>|
end
defp admin_badge(_), do: Phoenix.HTML.raw("<span class=\"opacity-50\">&mdash;</span>")
defp confirmed_cell(nil) do
assigns = %{}
~H|<span class="opacity-50">pending</span>|
end
defp confirmed_cell(%DateTime{} = dt), do: Calendar.strftime(dt, "%Y-%m-%d")
defp confirmed_cell(_), do: ""
end