Convert /admin/contact-edits list to live_table
The pending-edits list on the admin review page now sorts through live_table. Columns for contact, submitted-by user, changed-fields summary, and submitted timestamp are rendered via 2-arity custom renderers so the Ecto-preloaded :contact and :user associations stay accessible — live_table's data_provider path skips select_columns so the full struct (with preloads) reaches the renderer. The review drawer, diff table, approve/reject flow, and note textarea all stay as-is; after approve/reject the page push_patches back to its current path so live_table re-runs handle_params and the list refreshes. Pending count in the header now reads Radio.pending_edit_count/0 instead of length(@edits) since we no longer hold the full list in socket state.
This commit is contained in:
parent
f9264908db
commit
606340e1c7
2 changed files with 83 additions and 55 deletions
|
|
@ -732,6 +732,19 @@ defmodule Microwaveprop.Radio do
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Base query for pending contact edits with `:user` and `:contact`
|
||||||
|
preloaded. Used as a `live_table` `data_provider` so sort/paginate
|
||||||
|
can be applied on top.
|
||||||
|
"""
|
||||||
|
@spec pending_edits_query() :: Ecto.Query.t()
|
||||||
|
def pending_edits_query do
|
||||||
|
from e in ContactEdit,
|
||||||
|
as: :resource,
|
||||||
|
where: e.status == :pending,
|
||||||
|
preload: [:user, :contact]
|
||||||
|
end
|
||||||
|
|
||||||
@spec list_contact_edits(Ecto.UUID.t()) :: [ContactEdit.t()]
|
@spec list_contact_edits(Ecto.UUID.t()) :: [ContactEdit.t()]
|
||||||
def list_contact_edits(contact_id) do
|
def list_contact_edits(contact_id) do
|
||||||
ContactEdit
|
ContactEdit
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,50 @@
|
||||||
defmodule MicrowavepropWeb.Admin.ContactEditLive do
|
defmodule MicrowavepropWeb.Admin.ContactEditLive do
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
use MicrowavepropWeb, :live_view
|
use MicrowavepropWeb, :live_view
|
||||||
|
use LiveTable.LiveResource, schema: Microwaveprop.Radio.ContactEdit
|
||||||
|
|
||||||
alias Microwaveprop.Radio
|
alias Microwaveprop.Radio
|
||||||
alias Microwaveprop.Radio.EditNotifier
|
alias Microwaveprop.Radio.EditNotifier
|
||||||
|
|
||||||
|
def fields do
|
||||||
|
[
|
||||||
|
id: %{label: "ID", hidden: true},
|
||||||
|
contact_id: %{label: "Contact", renderer: &contact_cell/2},
|
||||||
|
user_id: %{label: "Submitted By", renderer: &user_cell/2},
|
||||||
|
proposed_changes: %{label: "Fields Changed", renderer: &changed_fields_cell/1},
|
||||||
|
inserted_at: %{label: "Submitted", sortable: true, renderer: &format_ts/1}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
def filters, do: []
|
||||||
|
|
||||||
|
def actions do
|
||||||
|
[
|
||||||
|
review: fn %{record: edit} ->
|
||||||
|
assigns = %{edit: edit}
|
||||||
|
|
||||||
|
~H"""
|
||||||
|
<button
|
||||||
|
phx-click="review"
|
||||||
|
phx-value-id={@edit.id}
|
||||||
|
class="btn btn-primary btn-xs"
|
||||||
|
>
|
||||||
|
Review
|
||||||
|
</button>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(_params, _session, socket) do
|
def mount(_params, _session, socket) do
|
||||||
edits = Radio.list_pending_edits()
|
|
||||||
|
|
||||||
{:ok,
|
{:ok,
|
||||||
assign(socket,
|
socket
|
||||||
page_title: "Review Contact Edits",
|
|> assign(:page_title, "Review Contact Edits")
|
||||||
edits: edits,
|
|> assign(:reviewing, nil)
|
||||||
reviewing: nil,
|
|> assign(:admin_note, "")
|
||||||
admin_note: ""
|
|> assign(:pending_count, Radio.pending_edit_count())
|
||||||
)}
|
|> assign(:data_provider, {Radio, :pending_edits_query, []})}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
|
@ -43,8 +72,9 @@ defmodule MicrowavepropWeb.Admin.ContactEditLive do
|
||||||
|
|
||||||
{:noreply,
|
{:noreply,
|
||||||
socket
|
socket
|
||||||
|> assign(reviewing: nil, admin_note: "", edits: Radio.list_pending_edits())
|
|> assign(reviewing: nil, admin_note: "", pending_count: Radio.pending_edit_count())
|
||||||
|> put_flash(:info, "Edit approved and applied.")}
|
|> put_flash(:info, "Edit approved and applied.")
|
||||||
|
|> push_patch(to: "/" <> (socket.assigns[:current_path] || "admin/contact-edits"))}
|
||||||
|
|
||||||
{:error, _} ->
|
{:error, _} ->
|
||||||
{:noreply, put_flash(socket, :error, "Failed to approve edit.")}
|
{:noreply, put_flash(socket, :error, "Failed to approve edit.")}
|
||||||
|
|
@ -62,8 +92,9 @@ defmodule MicrowavepropWeb.Admin.ContactEditLive do
|
||||||
|
|
||||||
{:noreply,
|
{:noreply,
|
||||||
socket
|
socket
|
||||||
|> assign(reviewing: nil, admin_note: "", edits: Radio.list_pending_edits())
|
|> assign(reviewing: nil, admin_note: "", pending_count: Radio.pending_edit_count())
|
||||||
|> put_flash(:info, "Edit rejected.")}
|
|> put_flash(:info, "Edit rejected.")
|
||||||
|
|> push_patch(to: "/" <> (socket.assigns[:current_path] || "admin/contact-edits"))}
|
||||||
|
|
||||||
{:error, _} ->
|
{:error, _} ->
|
||||||
{:noreply, put_flash(socket, :error, "Failed to reject edit.")}
|
{:noreply, put_flash(socket, :error, "Failed to reject edit.")}
|
||||||
|
|
@ -114,13 +145,32 @@ defmodule MicrowavepropWeb.Admin.ContactEditLive do
|
||||||
defp format_proposed("qso_timestamp", val) when is_binary(val), do: val
|
defp format_proposed("qso_timestamp", val) when is_binary(val), do: val
|
||||||
defp format_proposed(_field, val), do: to_string(val)
|
defp format_proposed(_field, val), do: to_string(val)
|
||||||
|
|
||||||
|
defp contact_cell(_value, %{contact: contact}) when not is_nil(contact) do
|
||||||
|
assigns = %{contact: contact}
|
||||||
|
|
||||||
|
~H"""
|
||||||
|
<.link navigate={~p"/contacts/#{@contact.id}"} class="link link-primary">
|
||||||
|
{@contact.station1} / {@contact.station2}
|
||||||
|
</.link>
|
||||||
|
<div class="text-xs opacity-60">{format_band(@contact.band)}</div>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
defp contact_cell(_value, _record), do: "—"
|
||||||
|
|
||||||
|
defp user_cell(_value, %{user: %{callsign: callsign}}), do: callsign
|
||||||
|
defp user_cell(_value, _record), do: "—"
|
||||||
|
|
||||||
|
defp changed_fields_cell(changes) when is_map(changes), do: changed_fields_summary(changes)
|
||||||
|
defp changed_fields_cell(_), do: "—"
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def render(assigns) do
|
def render(assigns) do
|
||||||
~H"""
|
~H"""
|
||||||
<Layouts.app flash={@flash} current_scope={@current_scope}>
|
<Layouts.app flash={@flash} current_scope={@current_scope}>
|
||||||
<.header>
|
<.header>
|
||||||
Review Contact Edits
|
Review Contact Edits
|
||||||
<:subtitle>{length(@edits)} pending</:subtitle>
|
<:subtitle>{@pending_count} pending</:subtitle>
|
||||||
<:actions>
|
<:actions>
|
||||||
<.link navigate={~p"/admin/backfill"} class="btn btn-sm btn-ghost">
|
<.link navigate={~p"/admin/backfill"} class="btn btn-sm btn-ghost">
|
||||||
<.icon name="hero-arrow-left" class="w-4 h-4" /> Admin
|
<.icon name="hero-arrow-left" class="w-4 h-4" /> Admin
|
||||||
|
|
@ -192,48 +242,13 @@ defmodule MicrowavepropWeb.Admin.ContactEditLive do
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<%= if @edits == [] do %>
|
<.live_table
|
||||||
<div class="text-center py-12 opacity-50">
|
fields={fields()}
|
||||||
<.icon name="hero-check-circle" class="w-12 h-12 mx-auto mb-2" />
|
filters={filters()}
|
||||||
<p>No pending edits to review.</p>
|
options={@options}
|
||||||
</div>
|
streams={@streams}
|
||||||
<% else %>
|
actions={actions()}
|
||||||
<div class="overflow-x-auto rounded-box border border-base-300">
|
/>
|
||||||
<table class="table">
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>Contact</th>
|
|
||||||
<th>Submitted By</th>
|
|
||||||
<th>Fields Changed</th>
|
|
||||||
<th>Submitted</th>
|
|
||||||
<th></th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
<%= for edit <- @edits do %>
|
|
||||||
<tr>
|
|
||||||
<td>
|
|
||||||
<.link navigate={~p"/contacts/#{edit.contact.id}"} class="link link-primary">
|
|
||||||
{edit.contact.station1} / {edit.contact.station2}
|
|
||||||
</.link>
|
|
||||||
<div class="text-xs opacity-60">
|
|
||||||
{format_band(edit.contact.band)}
|
|
||||||
</div>
|
|
||||||
</td>
|
|
||||||
<td>{edit.user.callsign}</td>
|
|
||||||
<td class="text-sm">{changed_fields_summary(edit.proposed_changes)}</td>
|
|
||||||
<td class="text-sm">{format_ts(edit.inserted_at)}</td>
|
|
||||||
<td>
|
|
||||||
<button phx-click="review" phx-value-id={edit.id} class="btn btn-primary btn-xs">
|
|
||||||
Review
|
|
||||||
</button>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
<% end %>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
<% end %>
|
|
||||||
</Layouts.app>
|
</Layouts.app>
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue