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
81 lines
2.7 KiB
Elixir
81 lines
2.7 KiB
Elixir
defmodule MicrowavepropWeb.BeaconLive.Index do
|
|
@moduledoc false
|
|
use MicrowavepropWeb, :live_view
|
|
|
|
alias Microwaveprop.Beacons
|
|
|
|
@impl true
|
|
def render(assigns) do
|
|
~H"""
|
|
<Layouts.app flash={@flash} current_scope={@current_scope} max_width="max-w-6xl">
|
|
<.header>
|
|
Beacons
|
|
<:subtitle>Microwave beacons tracked by NTMS.</:subtitle>
|
|
<:actions>
|
|
<.button :if={admin?(@current_scope)} variant="primary" navigate={~p"/beacons/new"}>
|
|
<.icon name="hero-plus" /> New Beacon
|
|
</.button>
|
|
</:actions>
|
|
</.header>
|
|
|
|
<.table
|
|
id="beacons"
|
|
rows={@streams.beacons}
|
|
row_click={fn {_id, beacon} -> JS.navigate(~p"/beacons/#{beacon}") end}
|
|
>
|
|
<:col :let={{_id, beacon}} label="Frequency (MHz)">{beacon.frequency_mhz}</:col>
|
|
<:col :let={{_id, beacon}} label="Call">{beacon.callsign}</:col>
|
|
<:col :let={{_id, beacon}} label="Grid">{beacon.grid}</:col>
|
|
<:col :let={{_id, beacon}} label="Lat">{beacon.lat}</:col>
|
|
<:col :let={{_id, beacon}} label="Lon">{beacon.lon}</:col>
|
|
<:col :let={{_id, beacon}} label="Power (W)">{beacon.power_watts}</:col>
|
|
<:col :let={{_id, beacon}} label="Height AGL (m)">{beacon.height_m}</:col>
|
|
<:action :let={{_id, beacon}}>
|
|
<div class="sr-only">
|
|
<.link navigate={~p"/beacons/#{beacon}"}>Show</.link>
|
|
</div>
|
|
<.link :if={admin?(@current_scope)} navigate={~p"/beacons/#{beacon}/edit"}>Edit</.link>
|
|
</:action>
|
|
<:action :let={{id, beacon}}>
|
|
<.link
|
|
:if={admin?(@current_scope)}
|
|
phx-click={JS.push("delete", value: %{id: beacon.id}) |> hide("##{id}")}
|
|
data-confirm="Delete this beacon?"
|
|
>
|
|
Delete
|
|
</.link>
|
|
</:action>
|
|
</.table>
|
|
</Layouts.app>
|
|
"""
|
|
end
|
|
|
|
@impl true
|
|
def mount(_params, _session, socket) do
|
|
if connected?(socket), do: Beacons.subscribe_beacons()
|
|
|
|
{:ok,
|
|
socket
|
|
|> assign(:page_title, "Beacons")
|
|
|> stream(:beacons, Beacons.list_beacons())}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("delete", %{"id" => id}, socket) do
|
|
if admin?(socket.assigns.current_scope) do
|
|
beacon = Beacons.get_beacon!(id)
|
|
{:ok, _} = Beacons.delete_beacon(beacon)
|
|
{:noreply, stream_delete(socket, :beacons, beacon)}
|
|
else
|
|
{:noreply, put_flash(socket, :error, "Admins only.")}
|
|
end
|
|
end
|
|
|
|
@impl true
|
|
def handle_info({type, %Microwaveprop.Beacons.Beacon{}}, socket) when type in [:created, :updated, :deleted] do
|
|
{:noreply, stream(socket, :beacons, Beacons.list_beacons(), reset: true)}
|
|
end
|
|
|
|
defp admin?(%{user: %{is_admin: true}}), do: true
|
|
defp admin?(_), do: false
|
|
end
|