Skip to content

Commit

Permalink
fixup: fix lat and lon inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
rudiejd committed Jan 6, 2025
1 parent 766f029 commit 38fb328
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/arrow_web/live/stop_live/stop_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 38fb328

Please sign in to comment.