-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Convert itinerary legs to segments for easier rendering #2262
Changes from 4 commits
683aa7e
abdfa43
bd7318e
3452b0d
6db42ba
5caf9b7
b200b37
37de8f1
8052970
6726e81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule DotcomWeb.Components.TripPlanner.LegToSegmentHelper do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sold on either this name or this location for this file, but I couldn't think of a better one off the top of my head. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it's not technically a rendered component, you can move this to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💥 5caf9b7 💥 |
||
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_leg]) do | ||
[last_leg, {:location_segment, %{time: leg.stop, place: leg.to}}] | ||
end | ||
|
||
defp append_end_location([first_leg | rest_of_legs]) do | ||
[first_leg | append_end_location(rest_of_legs)] | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule DotcomWeb.Components.TripPlanner.LegToSegmentHelperTest do | ||
@moduledoc false | ||
|
||
use ExUnit.Case, async: true | ||
|
||
alias Dotcom.TripPlan.{Leg, PersonalDetail, TransitDetail} | ||
alias DotcomWeb.Components.TripPlanner.LegToSegmentHelper | ||
|
||
test "works for a typical walking-transit-walking itinerary and puts a location on either end" do | ||
assert [ | ||
{:location_segment, _}, | ||
{:walking_segment, _}, | ||
{:transit_segment, _}, | ||
{:walking_segment, _}, | ||
{:location_segment, _} | ||
] = | ||
LegToSegmentHelper.legs_to_segments([ | ||
%Leg{mode: %PersonalDetail{}}, | ||
%Leg{mode: %TransitDetail{}}, | ||
%Leg{mode: %PersonalDetail{}} | ||
]) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kinda wanna let this slide, but 40 seems kinda excessive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh oops - didn't mean to include this change. I've been using that to get more itineraries so that the one I'm looking for is more likely to actually show up.