defmodule MicrowavepropWeb.ContactMapLive do @moduledoc false use MicrowavepropWeb, :live_view import Ecto.Query alias Microwaveprop.Radio.Contact alias Microwaveprop.Repo @impl true def mount(_params, _session, socket) do contacts = load_contacts() {:ok, assign(socket, page_title: "Contact Map", contacts_json: Jason.encode!(contacts), contact_count: length(contacts) )} end defp load_contacts do from(c in Contact, where: not is_nil(c.pos1) and not is_nil(c.pos2), select: %{ pos1: c.pos1, pos2: c.pos2, band: c.band } ) |> Repo.all() |> Enum.map(fn c -> lat1 = c.pos1["lat"] lon1 = c.pos1["lon"] || c.pos1["lng"] lat2 = c.pos2["lat"] lon2 = c.pos2["lon"] || c.pos2["lng"] band = if c.band, do: Decimal.to_integer(c.band), else: 0 if lat1 && lon1 && lat2 && lon2 do [lat1, lon1, lat2, lon2, band] end end) |> Enum.reject(&is_nil/1) end @impl true def render(assigns) do ~H"""
Contact Map
{Integer.to_string(@contact_count)} contacts
""" end end