diff --git a/lib/arrow_web/live/stop_live/stop_live.ex b/lib/arrow_web/live/stop_live/stop_live.ex index af262bbf..344686a6 100644 --- a/lib/arrow_web/live/stop_live/stop_live.ex +++ b/lib/arrow_web/live/stop_live/stop_live.ex @@ -126,19 +126,27 @@ defmodule ArrowWeb.StopViewLive do {:ok, socket} end + defp valid_float(str) do + case Float.parse(str) do + {float, _} -> float + _ -> nil + end + end + def handle_event("validate", %{"stop" => stop_params}, socket) do form = Stops.change_stop(socket.assigns.stop, stop_params) |> to_form(action: :validate) {existing_stops, existing_gtfs_stops} = case stop_params do - %{"stop_lat" => lat, "stop_lon" => lon, "stop_id" => stop_id} - when not is_nil(lat) and not is_nil(lon) and lat != "" and lon != "" -> - lat_float = String.to_float(lat) - lon_float = String.to_float(lon) - - {Stops.get_stops_within_mile(stop_id, {lat_float, lon_float}), - GtfsStop.get_stops_within_mile(stop_id, {lat_float, lon_float})} - + %{"stop_lat" => lat, "stop_lon" => lon, "stop_id" => stop_id} -> + float_lat = valid_float(lat) + float_lon = valid_float(lon) + if is_float(float_lat) and is_float(float_lon) do + {Stops.get_stops_within_mile(stop_id, {float_lat, float_lon}), + GtfsStop.get_stops_within_mile(stop_id, {float_lat, float_lon})} + else + {nil, nil} + end _ -> {nil, nil} end