Skip to content

Commit

Permalink
fix(shuttles): call Changeset.apply_action after uploading xlsx
Browse files Browse the repository at this point in the history
  • Loading branch information
meagharty committed Jan 10, 2025
1 parent 59aae6a commit babb617
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions lib/arrow_web/live/shuttle_live/shuttle_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -538,32 +538,6 @@ defmodule ArrowWeb.ShuttleViewLive do
end
end

defp update_route_changeset_with_uploaded_stops(route_changeset, stop_ids, direction_id) do
if Ecto.Changeset.get_field(route_changeset, :direction_id) == direction_id do
new_route_stops =
stop_ids
|> Enum.with_index()
|> Enum.map(fn {stop_id, i} ->
Arrow.Shuttles.RouteStop.changeset(
%Arrow.Shuttles.RouteStop{},
%{
direction_id: direction_id,
stop_sequence: i,
display_stop_id: Integer.to_string(stop_id)
}
)
end)

Ecto.Changeset.put_assoc(
route_changeset,
:route_stops,
new_route_stops
)
else
route_changeset
end
end

@spec get_stop_travel_times(list({:ok, any()})) ::
{:ok, list(number())} | {:error, any()}
defp get_stop_travel_times(stop_coordinates) do
Expand Down Expand Up @@ -687,8 +661,31 @@ defmodule ArrowWeb.ShuttleViewLive do
end
end

defp update_route_changeset_with_uploaded_stops(route_changeset, stop_ids, direction_id) do
new_route_stops =
stop_ids
|> Enum.with_index()
|> Enum.map(fn {stop_id, i} ->
Arrow.Shuttles.RouteStop.changeset(
%Arrow.Shuttles.RouteStop{},
%{
direction_id: direction_id,
stop_sequence: i,
display_stop_id: Integer.to_string(stop_id)
}
)
end)

Ecto.Changeset.put_assoc(
route_changeset,
:route_stops,
new_route_stops
)
end

defp populate_stop_ids(socket, stop_ids) do
changeset = socket.assigns.form.source

existing_routes = Ecto.Changeset.get_assoc(changeset, :routes)

new_routes =
Expand All @@ -704,6 +701,13 @@ defmodule ArrowWeb.ShuttleViewLive do

changeset = Ecto.Changeset.put_assoc(changeset, :routes, new_routes)

socket |> assign(:form, to_form(changeset)) |> update_map(changeset)
case Ecto.Changeset.apply_action(changeset, :update) do
{:error, changeset} ->
socket |> assign(:form, to_form(changeset)) |> update_map(changeset)

{:ok, shuttle} ->
new_changeset = Shuttles.change_shuttle(shuttle)
socket |> assign(:form, to_form(new_changeset)) |> update_map(new_changeset)
end
end
end

0 comments on commit babb617

Please sign in to comment.