diff --git a/lib/dotcom/trip_plan/leg_to_segment_helper.ex b/lib/dotcom/trip_plan/leg_to_segment_helper.ex new file mode 100644 index 0000000000..0b6c6bbc0c --- /dev/null +++ b/lib/dotcom/trip_plan/leg_to_segment_helper.ex @@ -0,0 +1,42 @@ +defmodule Dotcom.TripPlan.LegToSegmentHelper do + @moduledoc """ + A simple algorithm to convert legs as returned by Open Trip Planner + into segments to be displayed by our trip planner tool. + """ + + alias Dotcom.TripPlan.{PersonalDetail, TransitDetail} + + def legs_to_segments(legs) do + legs + |> raw_convert_to_segments() + |> prepend_start_location() + |> append_end_location() + end + + defp raw_convert_to_segments(legs) do + Enum.map(legs, &to_segment/1) + end + + defp to_segment(%{mode: %PersonalDetail{}} = leg) do + {:walking_segment, leg} + end + + defp to_segment(%{mode: %TransitDetail{}} = leg) do + {:transit_segment, leg} + end + + defp prepend_start_location([{_, leg} | _] = segments) do + [ + {:location_segment, %{time: leg.start, place: leg.from}} + | segments + ] + end + + defp append_end_location([{_, leg} = last_segment]) do + [last_segment, {:location_segment, %{time: leg.stop, place: leg.to}}] + end + + defp append_end_location([first_segment | rest_of_segments]) do + [first_segment | append_end_location(rest_of_segments)] + end +end diff --git a/lib/dotcom_web/components/trip_planner/itinerary_detail.ex b/lib/dotcom_web/components/trip_planner/itinerary_detail.ex index bacab77c1a..ff7cd75721 100644 --- a/lib/dotcom_web/components/trip_planner/itinerary_detail.ex +++ b/lib/dotcom_web/components/trip_planner/itinerary_detail.ex @@ -10,7 +10,7 @@ defmodule DotcomWeb.Components.TripPlanner.ItineraryDetail do import DotcomWeb.Components.TripPlanner.TransitLeg, only: [transit_leg: 1] import DotcomWeb.Components.TripPlanner.WalkingLeg, only: [walking_leg: 1] - alias Dotcom.TripPlan.{PersonalDetail, TransitDetail} + alias Dotcom.TripPlan.LegToSegmentHelper def itinerary_detail( %{ @@ -77,27 +77,45 @@ defmodule DotcomWeb.Components.TripPlanner.ItineraryDetail do defp specific_itinerary_detail(assigns) do assigns = - assigns - |> assign(:start_place, List.first(assigns.itinerary.legs).from) - |> assign(:start_time, List.first(assigns.itinerary.legs).start) - |> assign(:end_place, List.last(assigns.itinerary.legs).to) - |> assign(:end_time, List.last(assigns.itinerary.legs).stop) + assign( + assigns, + :segments, + LegToSegmentHelper.legs_to_segments(assigns.itinerary.legs) + ) ~H"""