Skip to content

Commit

Permalink
feat: ability to add and remove stops
Browse files Browse the repository at this point in the history
  • Loading branch information
lemald committed Nov 13, 2024
1 parent 8895ed8 commit dd87082
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
4 changes: 3 additions & 1 deletion lib/arrow/shuttles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions lib/arrow/shuttles/route.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
38 changes: 31 additions & 7 deletions lib/arrow_web/live/shuttle_live/shuttle_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
<button
type="button"
name={input_name(f_route, :route_stops_drop) <> "[]"}
value={f_route_stop.index}
phx-click={JS.dispatch("change")}
>
Delete stop
</button>
<input
value={f_route_stop.index}
type="hidden"
name={input_name(f_route, :route_stops_sort) <> "[]"}
/>
<input
value={input_value(f_route_stop, :direction_id)}
type="hidden"
Expand All @@ -117,6 +130,15 @@ defmodule ArrowWeb.ShuttleViewLive do
name={input_name(f_route_stop, :stop_sequence)}
/>
</.inputs_for>
<input type="hidden" name={input_name(f_route, :route_stops_drop) <> "[]"} />
<button
type="button"
name={input_name(f_route, :route_stops_sort) <> "[]"}
value="new"
phx-click={JS.dispatch("change")}
>
Add Another Stop
</button>
</.inputs_for>
<:actions>
<.button>Save Shuttle</.button>
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dd87082

Please sign in to comment.