Rover.update_location/3 and delete_location/2 now resolve any record when the actor is_admin. The index + show LiveViews surface Edit/Delete on every row for admin users, matching the existing owner UX.
161 lines
4.8 KiB
Elixir
161 lines
4.8 KiB
Elixir
defmodule MicrowavepropWeb.RoverLocationsLive.Show do
|
|
@moduledoc "Detail page for a single rover location, with a map + marker."
|
|
use MicrowavepropWeb, :live_view
|
|
|
|
alias Microwaveprop.Accounts.User
|
|
alias Microwaveprop.Radio.Maidenhead
|
|
alias Microwaveprop.Repo
|
|
alias Microwaveprop.Rover
|
|
alias Microwaveprop.Rover.Location
|
|
|
|
@impl true
|
|
def mount(%{"id" => id}, _session, socket) do
|
|
case load_location(id) do
|
|
%Location{} = loc ->
|
|
{:ok,
|
|
assign(socket,
|
|
page_title: "Rover Location",
|
|
location: loc,
|
|
grid: Maidenhead.from_latlon(loc.lat, loc.lon, 10)
|
|
)}
|
|
|
|
nil ->
|
|
{:ok,
|
|
socket
|
|
|> put_flash(:error, "Location not found.")
|
|
|> push_navigate(to: ~p"/rover-locations")}
|
|
end
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("delete", _params, socket) do
|
|
case current_user(socket.assigns[:current_scope]) do
|
|
%User{} = user ->
|
|
case Rover.delete_location(user, socket.assigns.location.id) do
|
|
{:ok, _} ->
|
|
{:noreply,
|
|
socket
|
|
|> put_flash(:info, "Location removed.")
|
|
|> push_navigate(to: ~p"/rover-locations")}
|
|
|
|
{:error, :not_found} ->
|
|
{:noreply, put_flash(socket, :error, "You can only delete your own locations.")}
|
|
end
|
|
|
|
_ ->
|
|
{:noreply, put_flash(socket, :error, "Sign in required.")}
|
|
end
|
|
end
|
|
|
|
defp load_location(id) do
|
|
case Ecto.UUID.cast(id) do
|
|
{:ok, uuid} -> Location |> Repo.get(uuid) |> Repo.preload(:user)
|
|
:error -> nil
|
|
end
|
|
end
|
|
|
|
defp current_user(scope) do
|
|
case scope do
|
|
%{user: %User{} = user} -> user
|
|
_ -> nil
|
|
end
|
|
end
|
|
|
|
defp can_modify?(scope, %Location{user_id: user_id}) do
|
|
case current_user(scope) do
|
|
%User{is_admin: true} -> true
|
|
%User{id: ^user_id} when not is_nil(user_id) -> true
|
|
_ -> false
|
|
end
|
|
end
|
|
|
|
defp status_label(:ideal), do: "Ideal"
|
|
defp status_label(:off_limits), do: "Off Limits"
|
|
defp status_label(_), do: ""
|
|
|
|
defp status_class(:ideal), do: "badge badge-success"
|
|
defp status_class(:off_limits), do: "badge badge-error"
|
|
defp status_class(_), do: "badge"
|
|
|
|
@impl true
|
|
def render(assigns) do
|
|
~H"""
|
|
<Layouts.app flash={@flash} current_scope={@current_scope} max_width="max-w-5xl">
|
|
<.header>
|
|
Rover Location
|
|
<:subtitle>
|
|
<span class={status_class(@location.status)}>{status_label(@location.status)}</span>
|
|
{@grid}
|
|
</:subtitle>
|
|
<:actions>
|
|
<.link navigate={~p"/rover-locations"} class="btn btn-ghost btn-sm">
|
|
<.icon name="hero-arrow-left" class="w-4 h-4" /> Back
|
|
</.link>
|
|
<button
|
|
:if={can_modify?(@current_scope, @location)}
|
|
type="button"
|
|
phx-click="delete"
|
|
data-confirm="Delete this location?"
|
|
class="btn btn-ghost btn-sm text-error"
|
|
>
|
|
<.icon name="hero-trash" class="w-4 h-4" /> Delete
|
|
</button>
|
|
</:actions>
|
|
</.header>
|
|
|
|
<div class="card bg-base-100 border border-base-300 p-4 mb-4">
|
|
<dl class="grid grid-cols-1 md:grid-cols-2 gap-x-6 gap-y-2 text-sm">
|
|
<div>
|
|
<dt class="text-base-content/60">Grid</dt>
|
|
<dd class="font-mono">{@grid}</dd>
|
|
</div>
|
|
<div>
|
|
<dt class="text-base-content/60">Coordinates</dt>
|
|
<dd class="font-mono">
|
|
{Float.round(@location.lat, 6)}, {Float.round(@location.lon, 6)}
|
|
</dd>
|
|
</div>
|
|
<div>
|
|
<dt class="text-base-content/60">Status</dt>
|
|
<dd>
|
|
<span class={status_class(@location.status)}>
|
|
{status_label(@location.status)}
|
|
</span>
|
|
</dd>
|
|
</div>
|
|
<div>
|
|
<dt class="text-base-content/60">Added</dt>
|
|
<dd>{Calendar.strftime(@location.inserted_at, "%Y-%m-%d %H:%M UTC")}</dd>
|
|
</div>
|
|
<div :if={@location.user}>
|
|
<dt class="text-base-content/60">Submitted by</dt>
|
|
<dd>
|
|
<.link
|
|
navigate={~p"/u/#{@location.user.callsign}"}
|
|
class="link link-primary font-mono"
|
|
>
|
|
{@location.user.callsign}
|
|
</.link>
|
|
</dd>
|
|
</div>
|
|
</dl>
|
|
|
|
<div :if={@location.notes && @location.notes != ""} class="mt-4">
|
|
<h3 class="text-sm text-base-content/60 mb-1">Notes</h3>
|
|
<p class="whitespace-pre-line text-sm">{@location.notes}</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div
|
|
id={"location-map-#{@location.id}"}
|
|
phx-hook="LocationMap"
|
|
phx-update="ignore"
|
|
data-lat={@location.lat}
|
|
data-lon={@location.lon}
|
|
class="h-[70vh] rounded border border-base-300 z-0"
|
|
>
|
|
</div>
|
|
</Layouts.app>
|
|
"""
|
|
end
|
|
end
|