Skip to content

Commit

Permalink
wip: actually get editing working?
Browse files Browse the repository at this point in the history
  • Loading branch information
lemald committed Nov 13, 2024
1 parent b648a7a commit ad0629d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 63 deletions.
31 changes: 28 additions & 3 deletions lib/arrow/shuttles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Arrow.Shuttles do
alias Arrow.Gtfs.Route, as: GtfsRoute
alias Arrow.Gtfs.Stop, as: GtfsStop
alias Arrow.Shuttles.KML
alias Arrow.Shuttles.RouteStop
alias Arrow.Shuttles.Shape
alias Arrow.Shuttles.ShapesUpload
alias Arrow.Shuttles.ShapeUpload
Expand Down Expand Up @@ -267,7 +268,7 @@ defmodule Arrow.Shuttles do
"""
def get_shuttle!(id) do
Repo.get!(Shuttle, id) |> Repo.preload(@preloads)
Repo.get!(Shuttle, id) |> Repo.preload(@preloads) |> populate_display_stop_ids()
end

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

case created_shuttle do
{:ok, shuttle} -> {:ok, Repo.preload(shuttle, @preloads)}
{:ok, shuttle} -> {:ok, shuttle |> Repo.preload(@preloads) |> populate_display_stop_ids()}
err -> err
end
end
Expand All @@ -313,7 +314,7 @@ defmodule Arrow.Shuttles do
|> Repo.update()

case updated_shuttle do
{:ok, shuttle} -> {:ok, Repo.preload(shuttle, @preloads)}
{:ok, shuttle} -> {:ok, shuttle |> Repo.preload(@preloads) |> populate_display_stop_ids()}
err -> err
end
end
Expand All @@ -331,6 +332,30 @@ defmodule Arrow.Shuttles do
Shuttle.changeset(shuttle, attrs)
end

@spec populate_display_stop_ids(map()) :: map()
defp populate_display_stop_ids(shuttle) do
%{
shuttle
| routes:
Enum.map(shuttle.routes, fn route ->
%{
route
| route_stops:
Enum.map(route.route_stops, fn route_stop ->
Map.put(
route_stop,
:display_stop_id,
case route_stop do
%RouteStop{stop: %Stop{stop_id: stop_id}} -> stop_id
%RouteStop{gtfs_stop_id: gtfs_stop_id} -> gtfs_stop_id
end
)
end)
}
end)
}
end

def list_disruptable_routes do
query = from(r in GtfsRoute, where: r.type in [:light_rail, :heavy_rail])
Repo.all(query)
Expand Down
35 changes: 29 additions & 6 deletions lib/arrow/shuttles/route_stop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ defmodule Arrow.Shuttles.RouteStop do
use Ecto.Schema
import Ecto.Changeset

alias Arrow.Gtfs.Stop, as: GtfsStop
alias Arrow.Shuttles
alias Arrow.Shuttles.Stop

schema "shuttle_route_stops" do
field :direction_id, Ecto.Enum, values: [:"0", :"1"]
field :stop_sequence, :integer
field :time_to_next_stop, :decimal
field :display_stop_id, :string, virtual: true
belongs_to :shuttle_route, Arrow.Shuttles.Route
belongs_to :stop, Arrow.Shuttles.Stop
belongs_to :gtfs_stop, Arrow.Gtfs.Stop, type: :string
Expand All @@ -16,11 +21,29 @@ defmodule Arrow.Shuttles.RouteStop do

@doc false
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])
|> assoc_constraint(:shuttle_route)
|> assoc_constraint(:stop)
|> assoc_constraint(:gtfs_stop)
{stop_id, gtfs_stop_id, error} =
case Shuttles.stop_or_gtfs_stop_for_stop_id(
attrs["display_stop_id"] || route_stop.display_stop_id
) do
%Stop{id: id} -> {id, nil, nil}
%GtfsStop{id: id} -> {nil, id, nil}
nil -> {nil, nil, "not a valid stop ID"}
end

change =
route_stop
|> cast(attrs, [:direction_id, :stop_id, :gtfs_stop_id, :stop_sequence, :time_to_next_stop])
|> change(stop_id: stop_id)
|> change(gtfs_stop_id: gtfs_stop_id)
|> validate_required([:direction_id, :stop_sequence])
|> assoc_constraint(:shuttle_route)
|> assoc_constraint(:stop)
|> assoc_constraint(:gtfs_stop)

if is_nil(error) do
change
else
add_error(change, :display_stop_id, error)
end
end
end
58 changes: 4 additions & 54 deletions lib/arrow_web/live/shuttle_live/shuttle_live.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
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 @@ -106,16 +104,7 @@ defmodule ArrowWeb.ShuttleViewLive do
<.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[:display_stop_id]} label="Stop ID" />
<.input field={f_route_stop[:time_to_next_stop]} type="number" label="Time to next stop" />
Expand Down Expand Up @@ -188,7 +177,7 @@ defmodule ArrowWeb.ShuttleViewLive do
end

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

form =
socket.assigns.shuttle
Expand All @@ -199,7 +188,7 @@ defmodule ArrowWeb.ShuttleViewLive do
end

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

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

Expand All @@ -216,7 +205,7 @@ defmodule ArrowWeb.ShuttleViewLive do
end

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

case Shuttles.create_shuttle(shuttle_params) do
{:ok, shuttle} ->
Expand Down Expand Up @@ -245,43 +234,4 @@ defmodule ArrowWeb.ShuttleViewLive do
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 ad0629d

Please sign in to comment.