From a8852b55c88e0732777003b4155ad8a092457250 Mon Sep 17 00:00:00 2001 From: JD Date: Tue, 14 Jan 2025 17:19:04 -0500 Subject: [PATCH 1/5] fixup: add tests --- lib/arrow_web/components/stop_input.ex | 8 ++++++++ lib/arrow_web/live/shuttle_live/shuttle_live.ex | 2 +- test/arrow_web/live/shuttle_live/shuttle_live_test.exs | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/arrow_web/components/stop_input.ex b/lib/arrow_web/components/stop_input.ex index eba3af68..5ebf46ee 100644 --- a/lib/arrow_web/components/stop_input.ex +++ b/lib/arrow_web/components/stop_input.ex @@ -38,11 +38,19 @@ defmodule ArrowWeb.StopInput do allow_clear={true} target={@myself} options={@options} + value_mapper={&stop_value_mapper/1} /> """ end + defp stop_value_mapper(text) do + case Shuttles.stop_or_gtfs_stop_for_stop_id(text) do + nil -> {text, text} + stop -> 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..cdd0ebb3 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,7 @@ 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 From 05672a80c2317e17e21fc4dce64ce8cbb641650a Mon Sep 17 00:00:00 2001 From: JD Date: Tue, 14 Jan 2025 17:29:44 -0500 Subject: [PATCH 2/5] fixup: format --- test/arrow_web/live/shuttle_live/shuttle_live_test.exs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 cdd0ebb3..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, 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}] = + 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 From d6ff515e2683bd93cd810db45fa135f88dfe2f05 Mon Sep 17 00:00:00 2001 From: JD Date: Tue, 14 Jan 2025 18:12:55 -0500 Subject: [PATCH 3/5] fixup: stop value mapper uses stop from the changeset if present --- lib/arrow_web/components/stop_input.ex | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/arrow_web/components/stop_input.ex b/lib/arrow_web/components/stop_input.ex index 5ebf46ee..163a998f 100644 --- a/lib/arrow_web/components/stop_input.ex +++ b/lib/arrow_web/components/stop_input.ex @@ -6,6 +6,8 @@ defmodule ArrowWeb.StopInput do use ArrowWeb, :live_component + import Ecto.Changeset, only: [get_assoc: 2] + alias Arrow.Gtfs.Stop, as: GtfsStop alias Arrow.Shuttles alias Arrow.Shuttles.Stop @@ -30,6 +32,8 @@ defmodule ArrowWeb.StopInput do end ) + IO.inspect(assigns.field) + ~H"""
<.live_select @@ -38,16 +42,24 @@ defmodule ArrowWeb.StopInput do allow_clear={true} target={@myself} options={@options} - value_mapper={&stop_value_mapper/1} + value_mapper={&stop_value_mapper(&1, assigns.field)} />
""" end - defp stop_value_mapper(text) do - case Shuttles.stop_or_gtfs_stop_for_stop_id(text) do - nil -> {text, text} - stop -> option_for_stop(stop) + defp stop_value_mapper(text, field) do + stop = + case Map.get(field.form.source.changes, :display_stop) do + %Arrow.Gtfs.Stop{id: text} = stop -> stop |> dbg() + %Arrow.Shuttles.Stop{stop_id: text} = stop -> stop |> dbg() + _ -> Shuttles.stop_or_gtfs_stop_for_stop_id(text) |> dbg() + end + + if stop == nil do + {text, text} + else + option_for_stop(stop) end end From 795205bf59bfec871a1e4dabe7879a5b8055283b Mon Sep 17 00:00:00 2001 From: JD Date: Tue, 14 Jan 2025 18:14:28 -0500 Subject: [PATCH 4/5] fixup: format --- lib/arrow_web/components/stop_input.ex | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/arrow_web/components/stop_input.ex b/lib/arrow_web/components/stop_input.ex index 163a998f..42d834e9 100644 --- a/lib/arrow_web/components/stop_input.ex +++ b/lib/arrow_web/components/stop_input.ex @@ -32,8 +32,6 @@ defmodule ArrowWeb.StopInput do end ) - IO.inspect(assigns.field) - ~H"""
<.live_select From 3db3e64913c416fd420a4123d7f1233984c947c9 Mon Sep 17 00:00:00 2001 From: JD Date: Wed, 15 Jan 2025 11:17:48 -0500 Subject: [PATCH 5/5] fixup: fix warnings on build --- lib/arrow_web/components/stop_input.ex | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/arrow_web/components/stop_input.ex b/lib/arrow_web/components/stop_input.ex index 42d834e9..1693037b 100644 --- a/lib/arrow_web/components/stop_input.ex +++ b/lib/arrow_web/components/stop_input.ex @@ -6,8 +6,6 @@ defmodule ArrowWeb.StopInput do use ArrowWeb, :live_component - import Ecto.Changeset, only: [get_assoc: 2] - alias Arrow.Gtfs.Stop, as: GtfsStop alias Arrow.Shuttles alias Arrow.Shuttles.Stop @@ -49,9 +47,9 @@ defmodule ArrowWeb.StopInput do defp stop_value_mapper(text, field) do stop = case Map.get(field.form.source.changes, :display_stop) do - %Arrow.Gtfs.Stop{id: text} = stop -> stop |> dbg() - %Arrow.Shuttles.Stop{stop_id: text} = stop -> stop |> dbg() - _ -> Shuttles.stop_or_gtfs_stop_for_stop_id(text) |> dbg() + %Arrow.Gtfs.Stop{} = stop -> stop + %Arrow.Shuttles.Stop{} = stop -> stop + _ -> Shuttles.stop_or_gtfs_stop_for_stop_id(text) end if stop == nil do