Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shuttles): Fix indexes changing to 0-based and displayed stop names changing when stops are reordered #1090

Merged
merged 5 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/arrow_web/components/stop_input.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,27 @@ defmodule ArrowWeb.StopInput do
allow_clear={true}
target={@myself}
options={@options}
value_mapper={&stop_value_mapper(&1, assigns.field)}
/>
</div>
"""
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)
Copy link
Contributor

@meagharty meagharty Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not blocking: I'd love to have :display_stop / stop_or_gtfs_stop_for_stop_id consolidated in some way but I think this function hints at the (existing) issue where we were deal with the changes and the committed values across new and edit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree - I feel like display_stop is probably unnecessary since the stop is probably loaded in the changeset by the time the user has entered it. In the interest of keeping this PR small and making iterative improvements, I'm going to hold off on that for now.

end
end

def handle_event("live_select_change", %{"id" => live_select_id, "text" => text}, socket) do
new_opts =
if String.length(text) < 2 do
Expand Down
2 changes: 1 addition & 1 deletion lib/arrow_web/live/shuttle_live/shuttle_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
6 changes: 5 additions & 1 deletion test/arrow_web/live/shuttle_live/shuttle_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading