feat(contacts): show total contact count in list header
This commit is contained in:
parent
24eebcea63
commit
48cbf40b9d
2 changed files with 29 additions and 2 deletions
|
|
@ -3,6 +3,9 @@ defmodule MicrowavepropWeb.ContactLive.Index do
|
|||
use MicrowavepropWeb, :live_view
|
||||
use LiveTable.LiveResource, schema: Microwaveprop.Radio.Contact
|
||||
|
||||
alias Microwaveprop.Radio.Contact
|
||||
alias Microwaveprop.Repo
|
||||
|
||||
def table_options do
|
||||
%{
|
||||
sorting: %{default_sort: [inserted_at: :desc]},
|
||||
|
|
@ -45,7 +48,12 @@ defmodule MicrowavepropWeb.ContactLive.Index do
|
|||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, assign(socket, :page_title, "Contacts")}
|
||||
total = Repo.aggregate(Contact, :count, :id)
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(:page_title, "Contacts")
|
||||
|> assign(:total_contacts, total)}
|
||||
end
|
||||
|
||||
@enrichment_fields [
|
||||
|
|
@ -96,6 +104,14 @@ defmodule MicrowavepropWeb.ContactLive.Index do
|
|||
|
||||
defp flag_cell(_), do: ""
|
||||
|
||||
defp format_count(n) when is_integer(n) do
|
||||
n
|
||||
|> Integer.to_string()
|
||||
|> String.reverse()
|
||||
|> String.replace(~r/(\d{3})(?=\d)/, "\\1,")
|
||||
|> String.reverse()
|
||||
end
|
||||
|
||||
defp format_ts(nil), do: ""
|
||||
defp format_ts(%DateTime{} = dt), do: Calendar.strftime(dt, "%Y-%m-%d %H:%M")
|
||||
defp format_ts(%NaiveDateTime{} = dt), do: Calendar.strftime(dt, "%Y-%m-%d %H:%M")
|
||||
|
|
@ -107,7 +123,9 @@ defmodule MicrowavepropWeb.ContactLive.Index do
|
|||
<Layouts.app flash={@flash} current_scope={@current_scope} max_width="max-w-7xl">
|
||||
<.header>
|
||||
Contacts
|
||||
<:subtitle>Sort or search by callsign, grid, or mode.</:subtitle>
|
||||
<:subtitle>
|
||||
{format_count(@total_contacts)} total — sort or search by callsign, grid, or mode.
|
||||
</:subtitle>
|
||||
<:actions>
|
||||
<.link navigate={~p"/submit"} class="btn btn-primary">
|
||||
<.icon name="hero-plus" class="w-5 h-5" /> Submit Contact
|
||||
|
|
|
|||
|
|
@ -55,6 +55,15 @@ defmodule MicrowavepropWeb.ContactLiveTest do
|
|||
assert html =~ "Contacts"
|
||||
end
|
||||
|
||||
test "shows total contact count in subtitle", %{conn: conn} do
|
||||
create_contact()
|
||||
create_contact(%{station1: "N5AC"})
|
||||
create_contact(%{station1: "K5TR"})
|
||||
|
||||
{:ok, _lv, html} = live(conn, ~p"/contacts")
|
||||
assert html =~ "3 total"
|
||||
end
|
||||
|
||||
test "table shows contact data", %{conn: conn} do
|
||||
create_contact()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue