feat(admin): surface flagged contacts on the contact-edits page

Flagged contacts were only reachable from the contact detail page;
once the Flag column came off the /contacts index there was no
at-a-glance view of what's been flagged. Add a second table on the
admin contact-edits page (below the pending-edits table) that lists
every flagged contact newest-first with a direct View link. Hidden
entirely when nothing is flagged.
This commit is contained in:
Graham McIntire 2026-04-19 12:40:19 -05:00
parent 0942d0bbfb
commit 6f63f3bb5e
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59
3 changed files with 124 additions and 0 deletions

View file

@ -454,6 +454,19 @@ defmodule Microwaveprop.Radio do
|> Repo.update!()
end
@doc """
Returns all flagged-invalid contacts, newest first. Admin-only
viewer the admin contact-edits page uses this to surface flagged
QSOs for review alongside pending edit requests.
"""
@spec list_flagged_contacts() :: [Contact.t()]
def list_flagged_contacts do
Contact
|> where([c], c.flagged_invalid == true)
|> order_by([c], desc: c.inserted_at, desc: c.id)
|> Repo.all()
end
@spec change_contact(Contact.t(), map()) :: Ecto.Changeset.t()
def change_contact(contact, attrs \\ %{}) do
Contact.submission_changeset(contact, attrs)

View file

@ -48,6 +48,7 @@ defmodule MicrowavepropWeb.Admin.ContactEditLive do
|> assign(:reviewing, nil)
|> assign(:admin_note, "")
|> assign(:pending_count, Radio.pending_edit_count())
|> assign(:flagged_contacts, Radio.list_flagged_contacts())
|> assign(:data_provider, {Radio, :pending_edits_query, []})}
end
@ -253,6 +254,42 @@ defmodule MicrowavepropWeb.Admin.ContactEditLive do
streams={@streams}
actions={actions()}
/>
<%= if @flagged_contacts != [] do %>
<div class="mt-10">
<h2 class="text-lg font-semibold mb-2">
Flagged contacts <span class="text-sm opacity-60">({length(@flagged_contacts)})</span>
</h2>
<div class="overflow-x-auto rounded-box border border-base-300">
<table class="table table-sm">
<thead>
<tr>
<th>Stations</th>
<th>Band</th>
<th>QSO (UTC)</th>
<th>Added (UTC)</th>
<th></th>
</tr>
</thead>
<tbody>
<%= for c <- @flagged_contacts do %>
<tr>
<td class="font-semibold">{c.station1} / {c.station2}</td>
<td>{format_band(c.band)}</td>
<td class="opacity-70">{format_ts(c.qso_timestamp)}</td>
<td class="opacity-70">{format_ts(c.inserted_at)}</td>
<td>
<.link navigate={~p"/contacts/#{c.id}"} class="btn btn-xs btn-ghost">
View
</.link>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
<% end %>
</Layouts.app>
"""
end

View file

@ -0,0 +1,74 @@
defmodule MicrowavepropWeb.Admin.ContactEditLiveTest do
use MicrowavepropWeb.ConnCase, async: false
import Microwaveprop.AccountsFixtures
import Phoenix.LiveViewTest
alias Microwaveprop.Accounts
alias Microwaveprop.Radio.Contact
alias Microwaveprop.Repo
defp admin_fixture do
user = user_fixture()
{:ok, user} = Accounts.admin_update_user(user, %{is_admin: true})
user
end
defp create_contact(attrs \\ %{}) do
default = %{
station1: "W5XD",
station2: "K5TR",
qso_timestamp: ~U[2026-03-28 18:00:00Z],
mode: "CW",
band: Decimal.new("1296"),
grid1: "EM12",
grid2: "EM00",
pos1: %{"lat" => 32.9, "lon" => -97.0},
pos2: %{"lat" => 30.3, "lon" => -97.7},
distance_km: Decimal.new("295")
}
merged = Map.merge(default, attrs)
# `flagged_invalid` isn't a user-facing submission field, so the
# changeset's cast drops it. Set it directly via change/2.
{direct_attrs, castable} = Map.split(merged, [:flagged_invalid])
{:ok, contact} =
%Contact{}
|> Contact.changeset(castable)
|> Ecto.Changeset.change(direct_attrs)
|> Repo.insert()
contact
end
describe "flagged contacts section" do
test "hidden when no contacts are flagged", %{conn: conn} do
_ok = create_contact(%{station1: "W5OK"})
conn = log_in_user(conn, admin_fixture())
{:ok, _lv, html} = live(conn, ~p"/admin/contact-edits")
refute html =~ "Flagged contacts"
end
test "lists flagged contacts with station callsigns for admins", %{conn: conn} do
_ok = create_contact(%{station1: "W5OK"})
_flagged = create_contact(%{station1: "W5BAD", flagged_invalid: true})
conn = log_in_user(conn, admin_fixture())
{:ok, _lv, html} = live(conn, ~p"/admin/contact-edits")
assert html =~ "Flagged contacts"
assert html =~ "W5BAD"
end
test "does not leak flagged contacts into the pending-edits table", %{conn: conn} do
_flagged = create_contact(%{station1: "W5BAD", flagged_invalid: true})
conn = log_in_user(conn, admin_fixture())
{:ok, _lv, html} = live(conn, ~p"/admin/contact-edits")
# The flagged list mentions W5BAD, but only inside the flagged
# section — the pending-edits table above should stay empty.
assert html =~ "0 pending"
end
end
end