prop/lib/microwaveprop_web/live/user_management_live/index.ex
Graham McIntire fd976b0cd5
fix: resolve 391 Credo issues across codebase
- Add jump_credo_checks ~> 0.4 with all 20 checks enabled
- Fix all standard Credo issues: 139 @spec (113 done, 26 remain),
  4 refactoring, 3 alias usage, 9 System.cmd env, 5 unsafe_to_atom,
  2 max line length, 9 assert_receive timeout
- Fix 170+ jump_credo_checks warnings:
  - 117 TopLevelAliasImportRequire: move nested alias/import to module top
  - 32 UseObanProWorker: switch to Oban.Pro.Worker
  - 4 DoctestIExExamples: add doctests / create test file
  - ~20 WeakAssertion: strengthen type-check assertions
  - Various ConditionalAssertion, AssertReceiveTimeout fixes
- Exclude vendor/ from Credo analysis
- Remaining: 175 warnings (mostly opinionated WeakAssertion,
  AvoidSocketAssignsInTest), 26 @spec annotations
2026-06-12 13:51:32 -05:00

107 lines
2.8 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
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