Skip to content

Commit

Permalink
refactor: Move &ItineraryDetail.alerts_for_leg/2 into Alerts module
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlarson committed Dec 17, 2024
1 parent 61c70b5 commit b611b08
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
12 changes: 11 additions & 1 deletion lib/dotcom/trip_plan/alerts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ defmodule Dotcom.TripPlan.Alerts do
)
end

@doc "Filters a list of Alerts to those relevant to the Leg"
@spec filter_for_leg([Alert.t()], Leg.t()) :: [Alert.t()]
def filter_for_leg(alerts, leg) do
Alerts.Match.match(
alerts,
leg_entities(leg),
leg.start
)
end

defp intermediate_entities(itinerary) do
itinerary
|> Itinerary.intermediate_stop_ids()
Expand All @@ -47,7 +57,7 @@ defmodule Dotcom.TripPlan.Alerts do
|> Enum.uniq()
end

def leg_entities(%Leg{mode: mode} = leg) do
defp leg_entities(%Leg{mode: mode} = leg) do
for entity <- mode_entities(mode),
stop_id <- Leg.stop_ids(leg) do
%{entity | stop: stop_id}
Expand Down
11 changes: 1 addition & 10 deletions lib/dotcom_web/components/trip_planner/itinerary_detail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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 Alerts.Match
alias Dotcom.TripPlan.LegToSegmentHelper
alias Dotcom.TripPlan.Alerts

Expand Down Expand Up @@ -112,17 +111,9 @@ defmodule DotcomWeb.Components.TripPlanner.ItineraryDetail do
assigns = assign(assigns, :leg, leg)

~H"""
<.transit_leg leg={@leg} alerts={alerts_for_leg(@alerts, @leg)} />
<.transit_leg leg={@leg} alerts={Alerts.filter_for_leg(@alerts, @leg)} />
"""
end

defp alerts_for_leg(alerts, leg) when is_list(alerts) do
Match.match(
alerts,
Alerts.leg_entities(leg),
leg.start
)
end

defp alerts_for_leg(_, _), do: []
end

0 comments on commit b611b08

Please sign in to comment.