Uncommitted work from previous session: - Add list_users_select/0 helper for admin select dropdowns - Add regenerate_token/1 context function - Preload assigned_by association on monitor queries - Add admin nav link to beacon monitors - Fix JS event targets (remove redundant target: @myself) - Add reassign user form to admin monitor show page - Add regenerate token button to admin monitor show page - Show assigned_by admin on monitor detail page - Add beacon monitors section to user management edit page - Add unassign monitor capability from user management - Add full test coverage for all new functionality - Fix pre-existing /account route warning (redirect → /users/settings)
151 lines
5.1 KiB
Elixir
151 lines
5.1 KiB
Elixir
defmodule MicrowavepropWeb.UserManagementLive.Edit do
|
|
@moduledoc "Admin edit page for a single user (roles, suspension, monitors)."
|
|
use MicrowavepropWeb, :live_view
|
|
|
|
alias Microwaveprop.Accounts
|
|
alias Microwaveprop.BeaconMonitors
|
|
|
|
@impl true
|
|
def render(assigns) do
|
|
~H"""
|
|
<Layouts.app flash={@flash} current_scope={@current_scope}>
|
|
<.header>
|
|
Edit user
|
|
<:subtitle>{@user.callsign} · {@user.email}</:subtitle>
|
|
</.header>
|
|
|
|
<.form for={@form} id="user-form" phx-change="validate" phx-submit="save">
|
|
<.input field={@form[:callsign]} type="text" label="Callsign" required />
|
|
<.input field={@form[:name]} type="text" label="Name" required />
|
|
<.input field={@form[:email]} type="email" label="Email" required />
|
|
<label class="flex items-center gap-2 cursor-pointer mt-4">
|
|
<.input field={@form[:is_admin]} type="checkbox" label="Admin" />
|
|
</label>
|
|
<footer class="mt-4 flex gap-2">
|
|
<.button variant="primary" phx-disable-with="Saving...">Save</.button>
|
|
<.button navigate={~p"/users"}>Cancel</.button>
|
|
</footer>
|
|
</.form>
|
|
|
|
<div class="card bg-base-200 shadow-sm mt-8">
|
|
<div class="card-body">
|
|
<h2 class="card-title text-sm">Beacon monitors</h2>
|
|
<p class="text-xs opacity-70 mb-3">
|
|
Monitor hardware assigned to this user.
|
|
</p>
|
|
|
|
<%= if @monitors == [] do %>
|
|
<p class="text-sm opacity-70 mb-4">No monitors assigned to this user.</p>
|
|
<% else %>
|
|
<div class="overflow-x-auto mb-4">
|
|
<table class="table table-zebra table-sm">
|
|
<thead>
|
|
<tr>
|
|
<th>Name</th>
|
|
<th>Hardware</th>
|
|
<th>Last seen</th>
|
|
<th class="w-1"></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr :for={monitor <- @monitors}>
|
|
<td>
|
|
<.link
|
|
navigate={~p"/admin/beacon-monitors/#{monitor.id}"}
|
|
class="link link-hover font-semibold"
|
|
>
|
|
{monitor.name}
|
|
</.link>
|
|
</td>
|
|
<td class="text-sm">
|
|
{monitor.hardware_type}
|
|
<span :if={monitor.hardware_id} class="opacity-60 text-xs">
|
|
({monitor.hardware_id})
|
|
</span>
|
|
</td>
|
|
<td class="text-sm opacity-70">
|
|
<%= if monitor.last_seen_at do %>
|
|
{Calendar.strftime(monitor.last_seen_at, "%Y-%m-%d %H:%M UTC")}
|
|
<% else %>
|
|
never
|
|
<% end %>
|
|
</td>
|
|
<td>
|
|
<.link
|
|
phx-click={JS.push("unassign-monitor", value: %{id: monitor.id})}
|
|
data-confirm={"Remove monitor '#{monitor.name}' from #{@user.callsign}?"}
|
|
class="btn btn-xs btn-ghost text-error"
|
|
>
|
|
Remove
|
|
</.link>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<% end %>
|
|
</div>
|
|
</div>
|
|
</Layouts.app>
|
|
"""
|
|
end
|
|
|
|
@impl true
|
|
def mount(%{"id" => id}, _session, socket) do
|
|
case Accounts.get_user!(id) do
|
|
nil ->
|
|
{:ok,
|
|
socket
|
|
|> put_flash(:error, "User not found.")
|
|
|> push_navigate(to: ~p"/users")}
|
|
|
|
user ->
|
|
monitors = BeaconMonitors.list_monitors_for_user(user)
|
|
|
|
{:ok,
|
|
socket
|
|
|> assign(:page_title, "Edit user")
|
|
|> assign(:user, user)
|
|
|> assign(:monitors, monitors)
|
|
|> assign(:form, to_form(Accounts.change_admin_user(user)))}
|
|
end
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("validate", %{"user" => params}, socket) do
|
|
changeset = Accounts.change_admin_user(socket.assigns.user, params)
|
|
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("save", %{"user" => params}, socket) do
|
|
case Accounts.admin_update_user(socket.assigns.user, params) do
|
|
{:ok, _user} ->
|
|
{:noreply,
|
|
socket
|
|
|> put_flash(:info, "User updated")
|
|
|> push_navigate(to: ~p"/users")}
|
|
|
|
{:error, changeset} ->
|
|
{:noreply, assign(socket, form: to_form(changeset))}
|
|
end
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("unassign-monitor", %{"id" => monitor_id}, socket) do
|
|
user = socket.assigns.user
|
|
|
|
case BeaconMonitors.delete_monitor!(monitor_id) do
|
|
{:ok, monitor} ->
|
|
monitors = BeaconMonitors.list_monitors_for_user(user)
|
|
|
|
{:noreply,
|
|
socket
|
|
|> assign(:monitors, monitors)
|
|
|> put_flash(:info, "Monitor '#{monitor.name}' removed from #{user.callsign}.")}
|
|
|
|
{:error, :not_found} ->
|
|
{:noreply, put_flash(socket, :error, "Monitor not found.")}
|
|
end
|
|
end
|
|
end
|