From dd870828fd6ad44c2a51403e39c471a66cd0cea9 Mon Sep 17 00:00:00 2001 From: Eddie Maldonado Date: Wed, 13 Nov 2024 14:34:59 -0500 Subject: [PATCH] feat: ability to add and remove stops --- lib/arrow/shuttles.ex | 4 +- lib/arrow/shuttles/route.ex | 9 ++++- .../live/shuttle_live/shuttle_live.ex | 38 +++++++++++++++---- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/lib/arrow/shuttles.ex b/lib/arrow/shuttles.ex index 236bf029..bcbd4704 100644 --- a/lib/arrow/shuttles.ex +++ b/lib/arrow/shuttles.ex @@ -366,7 +366,9 @@ defmodule Arrow.Shuttles do stop from GTFS. Prefers the Arrow-created stop if both are present. """ - @spec stop_or_gtfs_stop_for_stop_id(String.t()) :: Stop.t() | GtfsStop.t() | nil + @spec stop_or_gtfs_stop_for_stop_id(String.t() | nil) :: Stop.t() | GtfsStop.t() | nil + def stop_or_gtfs_stop_for_stop_id(nil), do: nil + def stop_or_gtfs_stop_for_stop_id(id) do case Repo.get_by(Stop, stop_id: id) do nil -> Repo.get(GtfsStop, id) diff --git a/lib/arrow/shuttles/route.ex b/lib/arrow/shuttles/route.ex index bdf42dbb..983ef89b 100644 --- a/lib/arrow/shuttles/route.ex +++ b/lib/arrow/shuttles/route.ex @@ -14,7 +14,8 @@ defmodule Arrow.Shuttles.Route do has_many :route_stops, Arrow.Shuttles.RouteStop, foreign_key: :shuttle_route_id, - preload_order: [asc: :stop_sequence] + preload_order: [asc: :stop_sequence], + on_replace: :delete timestamps(type: :utc_datetime) end @@ -23,7 +24,11 @@ defmodule Arrow.Shuttles.Route do def changeset(route, attrs) do route |> cast(attrs, [:direction_id, :direction_desc, :destination, :waypoint, :suffix, :shape_id]) - |> cast_assoc(:route_stops, with: &Arrow.Shuttles.RouteStop.changeset/2) + |> cast_assoc(:route_stops, + with: &Arrow.Shuttles.RouteStop.changeset/2, + sort_param: :route_stops_sort, + drop_param: :route_stops_drop + ) |> validate_required([:direction_id, :direction_desc, :destination]) |> assoc_constraint(:shape) end diff --git a/lib/arrow_web/live/shuttle_live/shuttle_live.ex b/lib/arrow_web/live/shuttle_live/shuttle_live.ex index 6b79d630..e5dbaa4f 100644 --- a/lib/arrow_web/live/shuttle_live/shuttle_live.ex +++ b/lib/arrow_web/live/shuttle_live/shuttle_live.ex @@ -106,6 +106,19 @@ defmodule ArrowWeb.ShuttleViewLive do <.inputs_for :let={f_route_stop} field={f_route[:route_stops]}> <.input field={f_route_stop[:display_stop_id]} label="Stop ID" /> <.input field={f_route_stop[:time_to_next_stop]} type="number" label="Time to next stop" /> + + "[]"} + /> + "[]"} /> + <:actions> <.button>Save Shuttle @@ -185,7 +207,7 @@ defmodule ArrowWeb.ShuttleViewLive do end def handle_event("edit", params, socket) do - shuttle_params = params |> combine_params() + shuttle_params = params |> combine_params() |> IO.inspect() shuttle = Shuttles.get_shuttle!(socket.assigns.shuttle.id) @@ -226,12 +248,14 @@ defmodule ArrowWeb.ShuttleViewLive do shuttle_params |> Map.get("routes") |> Map.new(fn {route_index, route} -> - {route_index, - Map.put( - route, - "route_stops", - routes_with_stops_params[route_index]["route_stops"] || [] - )} + route_stop_fields = + Map.take(routes_with_stops_params[route_index], [ + "route_stops", + "route_stops_drop", + "route_stops_sort" + ]) + + {route_index, Map.merge(route, route_stop_fields)} end) } end