Skip to content

Commit

Permalink
wip: stops section of shuttle form
Browse files Browse the repository at this point in the history
  • Loading branch information
lemald committed Nov 12, 2024
1 parent c07583b commit b648a7a
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 9 deletions.
10 changes: 6 additions & 4 deletions lib/arrow/shuttles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ defmodule Arrow.Shuttles do
alias Arrow.Shuttles.ShapeUpload
alias Arrow.Shuttles.Stop

@preloads [routes: [:shape, route_stops: [:stop, :gtfs_stop]]]

@doc """
Returns the list of shapes.
Expand Down Expand Up @@ -247,7 +249,7 @@ defmodule Arrow.Shuttles do
"""
def list_shuttles do
Repo.all(Shuttle) |> Repo.preload(routes: [:shape, :route_stops])
Repo.all(Shuttle) |> Repo.preload(@preloads)
end

@doc """
Expand All @@ -265,7 +267,7 @@ defmodule Arrow.Shuttles do
"""
def get_shuttle!(id) do
Repo.get!(Shuttle, id) |> Repo.preload(routes: [:shape, :route_stops])
Repo.get!(Shuttle, id) |> Repo.preload(@preloads)
end

@doc """
Expand All @@ -287,7 +289,7 @@ defmodule Arrow.Shuttles do
|> Repo.insert()

case created_shuttle do
{:ok, shuttle} -> {:ok, Repo.preload(shuttle, routes: [:shape, :route_stops])}
{:ok, shuttle} -> {:ok, Repo.preload(shuttle, @preloads)}
err -> err
end
end
Expand All @@ -311,7 +313,7 @@ defmodule Arrow.Shuttles do
|> Repo.update()

case updated_shuttle do
{:ok, shuttle} -> {:ok, Repo.preload(shuttle, routes: [:shape, :route_stops])}
{:ok, shuttle} -> {:ok, Repo.preload(shuttle, @preloads)}
err -> err
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/arrow/shuttles/route.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ defmodule Arrow.Shuttles.Route do
field :waypoint, :string
belongs_to :shuttle, Arrow.Shuttles.Shuttle
belongs_to :shape, Arrow.Shuttles.Shape
has_many :route_stops, Arrow.Shuttles.RouteStop, foreign_key: :shuttle_route_id

has_many :route_stops, Arrow.Shuttles.RouteStop,
foreign_key: :shuttle_route_id,
preload_order: [asc: :stop_sequence]

timestamps(type: :utc_datetime)
end
Expand All @@ -20,6 +23,7 @@ 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)
|> validate_required([:direction_id, :direction_desc, :destination])
|> assoc_constraint(:shape)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/arrow/shuttles/route_stop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule Arrow.Shuttles.RouteStop do
def changeset(route_stop, attrs) do
route_stop
|> cast(attrs, [:direction_id, :stop_id, :gtfs_stop_id, :stop_sequence, :time_to_next_stop])
|> validate_required([:direction_id, :stop_sequence, :time_to_next_stop])
|> validate_required([:direction_id, :stop_sequence])
|> assoc_constraint(:shuttle_route)
|> assoc_constraint(:stop)
|> assoc_constraint(:gtfs_stop)
Expand Down
99 changes: 96 additions & 3 deletions lib/arrow_web/live/shuttle_live/shuttle_live.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
defmodule ArrowWeb.ShuttleViewLive do
use ArrowWeb, :live_view
import Phoenix.HTML.Form
alias Arrow.Gtfs.Stop, as: GtfsStop
alias Arrow.Shuttles
alias Arrow.Shuttles.Shuttle
alias Arrow.Shuttles.Stop

embed_templates "shuttle_live/*"

Expand Down Expand Up @@ -100,6 +102,36 @@ defmodule ArrowWeb.ShuttleViewLive do
</div>
</div>
</.inputs_for>
<h2>define stops</h2>
<.inputs_for :let={f_route} field={f[:routes]} as={:routes_with_stops}>
<h4>direction <%= input_value(f_route, :direction_id) %></h4>
<.inputs_for :let={f_route_stop} field={f_route[:route_stops]}>
<input
value={
stop_id_for_input(
input_value(f_route_stop, :stop),
input_value(f_route_stop, :gtfs_stop)
)
}
type="text"
name={input_name(f_route_stop, :stop_id)}
/>
<.input field={f_route_stop[:time_to_next_stop]} type="number" label="Time to next stop" />
<input
value={input_value(f_route_stop, :direction_id)}
type="hidden"
name={input_name(f_route_stop, :direction_id)}
/>
<input
value={input_value(f_route_stop, :stop_sequence)}
type="hidden"
name={input_name(f_route_stop, :stop_sequence)}
/>
</.inputs_for>
</.inputs_for>
<:actions>
<.button>Save Shuttle</.button>
</:actions>
Expand Down Expand Up @@ -155,7 +187,9 @@ defmodule ArrowWeb.ShuttleViewLive do
{:ok, socket}
end

def handle_event("validate", %{"shuttle" => shuttle_params}, socket) do
def handle_event("validate", params, socket) do
shuttle_params = params |> combine_params() |> rewrite_stop_id_params()

form =
socket.assigns.shuttle
|> Shuttles.change_shuttle(shuttle_params)
Expand All @@ -164,7 +198,9 @@ defmodule ArrowWeb.ShuttleViewLive do
{:noreply, assign(socket, form: form)}
end

def handle_event("edit", %{"shuttle" => shuttle_params}, socket) do
def handle_event("edit", params, socket) do
shuttle_params = params |> combine_params() |> rewrite_stop_id_params()

shuttle = Shuttles.get_shuttle!(socket.assigns.shuttle.id)

case Shuttles.update_shuttle(shuttle, shuttle_params) do
Expand All @@ -179,7 +215,9 @@ defmodule ArrowWeb.ShuttleViewLive do
end
end

def handle_event("create", %{"shuttle" => shuttle_params}, socket) do
def handle_event("create", params, socket) do
shuttle_params = params |> combine_params() |> rewrite_stop_id_params()

case Shuttles.create_shuttle(shuttle_params) do
{:ok, shuttle} ->
{:noreply,
Expand All @@ -191,4 +229,59 @@ defmodule ArrowWeb.ShuttleViewLive do
{:noreply, assign(socket, form: to_form(changeset))}
end
end

defp combine_params(%{
"shuttle" => shuttle_params,
"routes_with_stops" => routes_with_stops_params
}) do
%{
shuttle_params
| "routes" =>
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"])}
end)
}
end

defp rewrite_stop_id_params(%{"routes" => routes} = params) do
new_routes =
Map.new(routes, fn {route_index, route} ->
{route_index,
%{
route
| "route_stops" =>
route
|> Map.get("route_stops", %{})
|> Map.new(fn {stop_index, stop} ->
{stop_index,
case stop |> Map.get("stop_id") |> Shuttles.stop_or_gtfs_stop_for_stop_id() do
%Stop{} = arrow_stop ->
stop
|> Map.put("stop", arrow_stop)
|> Map.put("gtfs_stop", nil)
|> Map.delete("stop_id")

%GtfsStop{} = gtfs_stop ->
stop
|> Map.put("gtfs_stop", gtfs_stop)
|> Map.put("stop", nil)
|> Map.delete("stop_id")

_ ->
stop
end}
end)
}}
end)

%{params | "routes" => new_routes}
end

@spec stop_id_for_input(Stop.t() | nil, GtfsStop.t() | nil) :: String.t() | nil
defp stop_id_for_input(%Stop{stop_id: stop_id}, _gtfs_stop), do: stop_id
defp stop_id_for_input(_stop, %GtfsStop{id: id}), do: id
defp stop_id_for_input(_stop, _gtfs_stop), do: nil
end

0 comments on commit b648a7a

Please sign in to comment.