prop/lib/microwaveprop_web/live/user_management_live/index.ex
Graham McIntire f733ddb6ac
Convert /contacts list to live_table
Replaces the hand-rolled sort/search/pagination on /contacts with
LiveTable.LiveResource. Sortable columns for station1/2, grid1/2,
band, mode, distance_km, qso_timestamp, and inserted_at; searchable
across both stations, both grids, and mode. Custom renderers preserve
the enrichment summary badge (with per-status tooltip), the invalid
flag column, and the qso/inserted_at formatters.

The reciprocal-grouping block has been dropped per the brainstorming
decision — every QSO now shows as its own row, which avoids fighting
live_table's one-row-per-record model. A "View" action links to the
detail page instead of the previous row-click handler (live_table's
stream dom_ids are random UUIDs so row-click by record id is no
longer practical without forking the library).

Test updates:
- sort test switched to ?sort_params[station1]=asc (live_table URL
  format; the old ?sort_by/?sort_order params are gone)
- pair-search test removed since live_table does a single ILIKE
  across searchable fields and no longer supports the two-callsign
  intersection that Radio.list_contacts/1 used to do; single-term
  search is exercised instead
- pagination test asserts the presence of "Page" (live_table's
  pagination text is "Page N" without the total count)

Also converts the stray cond-with-single-clause in
UserManagementLive.Index's delete handler to if/else (credo fix).
2026-04-12 17:22:15 -05:00

99 lines
2.6 KiB
Elixir

defmodule MicrowavepropWeb.UserManagementLive.Index do
@moduledoc false
use MicrowavepropWeb, :live_view
use LiveTable.LiveResource, schema: Microwaveprop.Accounts.User
alias Microwaveprop.Accounts
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :page_title, "Users")}
end
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
def filters, do: []
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
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, :resources, user)}
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