diff --git a/lib/arrow_web/components/stop_input.ex b/lib/arrow_web/components/stop_input.ex index eba3af68..1693037b 100644 --- a/lib/arrow_web/components/stop_input.ex +++ b/lib/arrow_web/components/stop_input.ex @@ -38,11 +38,27 @@ defmodule ArrowWeb.StopInput do allow_clear={true} target={@myself} options={@options} + value_mapper={&stop_value_mapper(&1, assigns.field)} /> """ end + defp stop_value_mapper(text, field) do + stop = + case Map.get(field.form.source.changes, :display_stop) do + %Arrow.Gtfs.Stop{} = stop -> stop + %Arrow.Shuttles.Stop{} = stop -> stop + _ -> Shuttles.stop_or_gtfs_stop_for_stop_id(text) + end + + if stop == nil do + {text, text} + else + option_for_stop(stop) + end + end + def handle_event("live_select_change", %{"id" => live_select_id, "text" => text}, socket) do new_opts = if String.length(text) < 2 do diff --git a/lib/arrow_web/live/shuttle_live/shuttle_live.ex b/lib/arrow_web/live/shuttle_live/shuttle_live.ex index 40c1f41f..fc6ab66c 100644 --- a/lib/arrow_web/live/shuttle_live/shuttle_live.ex +++ b/lib/arrow_web/live/shuttle_live/shuttle_live.ex @@ -556,7 +556,7 @@ defmodule ArrowWeb.ShuttleViewLive do existing_stops |> List.delete_at(old) |> List.insert_at(new, moved_route_stop) - |> Enum.reduce({[], 0}, fn route_stop, {route_stop_changes, stop_sequence} -> + |> Enum.reduce({[], 1}, fn route_stop, {route_stop_changes, stop_sequence} -> {route_stop_changes ++ [Arrow.Shuttles.RouteStop.changeset(route_stop, %{stop_sequence: stop_sequence})], stop_sequence + 1} diff --git a/test/arrow_web/live/shuttle_live/shuttle_live_test.exs b/test/arrow_web/live/shuttle_live/shuttle_live_test.exs index 352d2466..38150f26 100644 --- a/test/arrow_web/live/shuttle_live/shuttle_live_test.exs +++ b/test/arrow_web/live/shuttle_live/shuttle_live_test.exs @@ -329,7 +329,11 @@ defmodule ArrowWeb.ShuttleLiveTest do [%{id: stop_id1}, %{id: stop_id2}, %{id: stop_id3}] = [gtfs_stop1, gtfs_stop2, gtfs_stop3] - assert [%{gtfs_stop_id: ^stop_id2}, %{gtfs_stop_id: ^stop_id1}, %{gtfs_stop_id: ^stop_id3}] = + assert [ + %{gtfs_stop_id: ^stop_id2, stop_sequence: 1, display_stop_id: ^stop_id2}, + %{gtfs_stop_id: ^stop_id1, stop_sequence: 2, display_stop_id: ^stop_id1}, + %{gtfs_stop_id: ^stop_id3, stop_sequence: 3, display_stop_id: ^stop_id3} + ] = direction_0_route.route_stops end