Beacons: - Scaffolded with phx.gen.live then reworked so reads are public and mutations go through a live_session gated by the new :require_admin on_mount hook in UserAuth - Beacon schema stores frequency (MHz), callsign, grid, lat/lon, power (W), and height above ground (m); grid auto-derives from lat/lon when left blank via Maidenhead.from_latlon - Adds Maidenhead.from_latlon/3 so we can compute grids locally instead of hitting an external API Users admin page: - /users and /users/:id/edit (admin-only) for listing, editing (callsign/name/email/is_admin), and deleting other users - Adds Accounts.list_users, admin_update_user, delete_user, and a dedicated admin_changeset on the User schema - Nav gains a "Users" link for admins and a "Beacons" link for everyone
61 lines
1.9 KiB
Elixir
61 lines
1.9 KiB
Elixir
defmodule MicrowavepropWeb.UserManagementLive.Edit do
|
|
@moduledoc false
|
|
use MicrowavepropWeb, :live_view
|
|
|
|
alias Microwaveprop.Accounts
|
|
|
|
@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>
|
|
</Layouts.app>
|
|
"""
|
|
end
|
|
|
|
@impl true
|
|
def mount(%{"id" => id}, _session, socket) do
|
|
user = Accounts.get_user!(id)
|
|
|
|
{:ok,
|
|
socket
|
|
|> assign(:page_title, "Edit user")
|
|
|> assign(:user, user)
|
|
|> assign(:form, to_form(Accounts.change_admin_user(user)))}
|
|
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
|
|
|
|
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
|
|
end
|