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""" <.header> Rover Location <:subtitle> {status_label(@location.status)} {@grid} <:actions> <.link navigate={~p"/rover-locations"} class="btn btn-ghost btn-sm"> <.icon name="hero-arrow-left" class="w-4 h-4" /> Back
Grid
{@grid}
Coordinates
{Float.round(@location.lat, 6)}, {Float.round(@location.lon, 6)}
Status
{status_label(@location.status)}
Added
{Calendar.strftime(@location.inserted_at, "%Y-%m-%d %H:%M UTC")}
Submitted by
<.link navigate={~p"/u/#{@location.user.callsign}"} class="link link-primary font-mono" > {@location.user.callsign}

Notes

{@location.notes}

""" end end