Skip to content
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

feat: Add alert info to trip planner output #2269

Merged
merged 21 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
915ccd1
improvement: don't fetch trip from the V3 API to define the itinerary…
thecristen Dec 11, 2024
9d5e36d
[WIP] feat: Add alert info to trip planner output
thecristen Dec 11, 2024
b4e7a31
feat: Make alert component a collapsible summary/detail
joshlarson Dec 13, 2024
69c9439
refactor: Extract alert to its own component
joshlarson Dec 13, 2024
72f9b4e
fix: Little style fixes for alert component
joshlarson Dec 13, 2024
d83d92a
feat: Make alerts show up in the right place
joshlarson Dec 13, 2024
9a9555d
refactor+fix: Extract <.alert_group /> to its own component and fix a…
joshlarson Dec 13, 2024
03a41c0
cleanup: Move margin and grid-col classes out of <.alert_group />
joshlarson Dec 13, 2024
907b194
Merge remote-tracking branch 'origin/main' into cbjdl/trip-planner-al…
joshlarson Dec 16, 2024
4e327b2
refactor: Move &group_alerts/2 into TripPlan.Alerts module
joshlarson Dec 17, 2024
f23772f
fix: Stop alert layout
joshlarson Dec 17, 2024
8225f4e
refactor: Clean up variable names for grouped alerts
joshlarson Dec 17, 2024
50ada41
fix: Disambiguate stops versus alerts for CSS grouping
joshlarson Dec 17, 2024
832bf60
cleanup: Remove unused alias
joshlarson Dec 17, 2024
02c63dc
tests: Add test for &Alerts.by_mode_and_stops/2
joshlarson Dec 17, 2024
61c70b5
cleanup: Replace &Enum.group_by/2 with &Enum.split_with/2
joshlarson Dec 17, 2024
b611b08
refactor: Move &ItineraryDetail.alerts_for_leg/2 into Alerts module
joshlarson Dec 17, 2024
857f7e0
cleanup: Remove &ItineraryDetail.alerts_for_leg/2 straggler
joshlarson Dec 17, 2024
fca04c1
fix: Alerts tests
joshlarson Dec 17, 2024
e293a16
cleanup: Pare down stop ID logic and remove &Leg.stop_ids_{from, to}/1
joshlarson Dec 17, 2024
bf902d8
fix tests
thecristen Dec 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions lib/dotcom/trip_plan/alerts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ defmodule Dotcom.TripPlan.Alerts do
* at the times they'll be travelling
"""

import Routes.Route, only: [is_external?: 1]

alias Alerts.Alert
alias Alerts.InformedEntity, as: IE
alias Dotcom.TripPlan.{Itinerary, Leg, TransitDetail}

@spec from_itinerary(Itinerary.t()) :: [Alerts.Alert.t()]
def from_itinerary(itinerary) do
itinerary.start
|> Alerts.Repo.all()
|> Alerts.Match.match(
entities(itinerary),
itinerary.start
)
end

@doc "Filters a list of Alerts to those relevant to the Itinerary"
@spec filter_for_itinerary([Alert.t()], Itinerary.t()) :: [Alert.t()]
def filter_for_itinerary(alerts, itinerary) do
Expand All @@ -35,30 +47,36 @@ defmodule Dotcom.TripPlan.Alerts do
|> Enum.uniq()
end

defp leg_entities(%Leg{mode: mode} = leg) do
def leg_entities(%Leg{mode: mode} = leg) do
for entity <- mode_entities(mode),
stop_id <- Leg.stop_ids(leg) do
%{entity | stop: stop_id}
end
end

defp mode_entities(%TransitDetail{route: route, trip: %{id: trip_id}}) do
trip =
if is_nil(route.external_agency_name) do
Schedules.Repo.trip(trip_id)
end
def leg_entities_from(%Leg{mode: mode} = leg) do
for entity <- mode_entities(mode),
stop_id <- Leg.stop_ids_from(leg) do
%{entity | stop: stop_id}
end
end

route_type =
if route do
route.type
end
def leg_entities_to(%Leg{mode: mode} = leg) do
for entity <- mode_entities(mode),
stop_id <- Leg.stop_ids_to(leg) do
%{entity | stop: stop_id}
end
end

direction_id =
if trip do
trip.direction_id
end
defp mode_entities(%TransitDetail{route: route}) when is_external?(route) do
[]
end

[%IE{route_type: route_type, route: route.id, trip: trip_id, direction_id: direction_id}]
defp mode_entities(%TransitDetail{
route: %{id: route_id, type: route_type},
trip: %{id: trip_id, direction_id: direction_id}
}) do
[%IE{route_type: route_type, route: route_id, trip: trip_id, direction_id: direction_id}]
end

defp mode_entities(_) do
Expand Down
18 changes: 18 additions & 0 deletions lib/dotcom/trip_plan/leg.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ defmodule Dotcom.TripPlan.Leg do
end
end

@doc "Returns the stop ID for the beginning of the leg if available"
@spec stop_ids_from(t) :: [Stops.Stop.id_t()]
def stop_ids_from(%__MODULE__{from: from}) do
for %NamedPosition{stop: stop} <- [from],
stop do
stop.id
end
end

@doc "Returns the stop ID for the end of the leg if available"
@spec stop_ids_to(t) :: [Stops.Stop.id_t()]
def stop_ids_to(%__MODULE__{to: to}) do
for %NamedPosition{stop: stop} <- [to],
stop do
stop.id
end
end

thecristen marked this conversation as resolved.
Show resolved Hide resolved
@spec stop_is_silver_line_airport?([t], atom) :: boolean()
def stop_is_silver_line_airport?([], _), do: false

Expand Down
38 changes: 38 additions & 0 deletions lib/dotcom_web/components/trip_planner/alert_group.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule DotcomWeb.Components.TripPlanner.AlertGroup do
@moduledoc """
A component to display an alert for a route or location
"""

use DotcomWeb, :component

attr :alerts, :list, required: true
attr :class, :string, default: ""

def alert_group(assigns) do
~H"""
<%= if @alerts do %>
<div :for={alert <- @alerts} class={@class}>
<.alert alert={alert} />
</div>
<% end %>
"""
end

defp alert(assigns) do
~H"""
<details class="group">
<summary class="flex items-center gap-1.5 mb-1">
<.icon name="triangle-exclamation" class="w-3 h-3" />
<span>
<span class="text-sm">{Phoenix.Naming.humanize(@alert.effect)}</span>
<span class="group-open:hidden cursor-pointer btn-link text-xs">Show Details</span>
<span class="hidden group-open:inline cursor-pointer btn-link text-xs">Hide Details</span>
</span>
</summary>
<div class="bg-white p-2 text-sm">
{@alert.header}
</div>
</details>
"""
end
end
24 changes: 17 additions & 7 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,9 @@ 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

def itinerary_detail(
%{
Expand Down Expand Up @@ -77,16 +79,14 @@ defmodule DotcomWeb.Components.TripPlanner.ItineraryDetail do

defp specific_itinerary_detail(assigns) do
assigns =
assign(
assigns,
:segments,
LegToSegmentHelper.legs_to_segments(assigns.itinerary.legs)
)
assigns
|> assign_new(:alerts, fn -> Alerts.from_itinerary(assigns.itinerary) end)
|> assign(:segments, LegToSegmentHelper.legs_to_segments(assigns.itinerary.legs))

~H"""
<div class="mt-4">
<div :for={segment <- @segments}>
<.segment segment={segment} />
<.segment segment={segment} alerts={@alerts} />
</div>
</div>
"""
Expand Down Expand Up @@ -115,7 +115,17 @@ defmodule DotcomWeb.Components.TripPlanner.ItineraryDetail do
assigns = assign(assigns, :leg, leg)

~H"""
<.transit_leg leg={@leg} />
<.transit_leg leg={@leg} alerts={alerts_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
34 changes: 18 additions & 16 deletions lib/dotcom_web/components/trip_planner/place.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ defmodule DotcomWeb.Components.TripPlanner.Place do

use DotcomWeb, :component

import DotcomWeb.Components.TripPlanner.AlertGroup, only: [alert_group: 1]

alias Routes.Route
alias Stops.Stop

attr :place, :map, required: true
attr :time, :any, required: true
attr :route, :map, default: nil
attr :alerts, :list, default: []

def place(assigns) do
stop_url = stop_url(assigns.route, assigns.place.stop)
Expand All @@ -22,24 +25,23 @@ defmodule DotcomWeb.Components.TripPlanner.Place do
})

~H"""
<.dynamic_tag
tag_name={@tag_name}
href={@stop_url}
class="bg-gray-bordered-background px-3 py-2 rounded-lg grid grid-cols-[1.5rem_auto_1fr] items-center gap-2 w-full hover:no-underline text-black"
>
<div class="bg-gray-bordered-background px-3 py-2 rounded-lg grid grid-cols-[1.5rem_auto_1fr] items-center gap-x-2 w-full">
<.location_icon route={@route} class="h-6 w-6" />
<strong class="flex items-center gap-2">
{@place.name}
<.icon
:if={!is_nil(@place.stop) and Stop.accessible?(@place.stop)}
type="icon-svg"
name="icon-accessible-default"
class="h-5 w-5 ml-0.5 shrink-0"
aria-hidden="true"
/>
</strong>
<.dynamic_tag class="hover:no-underline text-black" tag_name={@tag_name} href={@stop_url}>
<strong class="flex items-center gap-2">
{@place.name}
<.icon
:if={!is_nil(@place.stop) and Stop.accessible?(@place.stop)}
type="icon-svg"
name="icon-accessible-default"
class="h-5 w-5 ml-0.5 shrink-0"
aria-hidden="true"
/>
</strong>
</.dynamic_tag>
<time class="text-right no-wrap">{format_time(@time)}</time>
</.dynamic_tag>
<.alert_group class="col-start-2 mr-4" alerts={@alerts} />
</div>
"""
end

Expand Down
47 changes: 42 additions & 5 deletions lib/dotcom_web/components/trip_planner/transit_leg.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,46 @@ defmodule DotcomWeb.Components.TripPlanner.TransitLeg do

use Phoenix.Component

import DotcomWeb.Components.TripPlanner.AlertGroup, only: [alert_group: 1]
import DotcomWeb.Components.RouteSymbols, only: [route_symbol: 1]
import DotcomWeb.Components.TripPlanner.Place
import MbtaMetro.Components.Icon, only: [icon: 1]
import Routes.Route, only: [is_external?: 1, is_shuttle?: 1]

alias Dotcom.TripPlan.TransitDetail
alias Alerts.Match
alias Dotcom.TripPlan.{Alerts, TransitDetail}
alias Routes.Route

@doc """
Renders a transit leg.

Must be given a `leg`
Must be given a `leg` and a list of `alerts`
"""

attr :alerts, :list, default: []
attr :leg, :any, required: true

def transit_leg(assigns) do
grouped_alerts = group_alerts(assigns.alerts, assigns.leg)
assigns = assign(assigns, grouped_alerts)

~H"""
<div class="bg-gray-bordered-background">
<.place
place={@leg.from}
time={@leg.start}
route={if(match?(%TransitDetail{}, @leg.mode), do: @leg.mode.route)}
alerts={@alerts_for_from}
/>

<div class={"bg-gray-bordered-background ml-5 border-l-8 #{leg_line_class(@leg.mode.route)}"}>
<%= if Enum.count(@leg.mode.intermediate_stops) < 2 do %>
<.leg_summary leg={@leg} />
<.leg_summary leg={@leg} alerts={@alerts_for_mode} />
<.leg_details leg={@leg} />
<% else %>
<details class="group">
<summary class="flex cursor-pointer list-none gap-2 relative">
<.leg_summary leg={@leg} />
<.leg_summary leg={@leg} alerts={@alerts_for_mode} />
<.icon
name="chevron-up"
class="group-open:rotate-180 w-4 h-4 absolute top-3 right-3 fill-brand-primary"
Expand All @@ -51,6 +59,7 @@ defmodule DotcomWeb.Components.TripPlanner.TransitLeg do
place={@leg.to}
time={@leg.stop}
route={if(match?(%TransitDetail{}, @leg.mode), do: @leg.mode.route)}
alerts={@alerts_for_to}
/>
</div>
"""
Expand Down Expand Up @@ -81,13 +90,14 @@ defmodule DotcomWeb.Components.TripPlanner.TransitLeg do
|> assign(:headsign, headsign(assigns.leg.mode))

~H"""
<div class="gap-x-1 py-2 grid grid-rows-2 grid-cols-[min-content_max-content] pl-4">
<div class="gap-x-1 py-2 grid grid-rows-2 grid-cols-[min-content_auto] pl-4">
<.route_symbol route={@leg.mode.route} />
<span class="font-semibold">{@headsign}</span>
<div class="text-sm col-start-2 row-start-2">
<.ride_message mode={@leg.mode} />
<span class="font-semibold">{@stops_count} {Inflex.inflect("stop", @stops_count)}</span>
</div>
<.alert_group class="col-start-2 mb-2 mr-4" alerts={@alerts} />
</div>
"""
end
Expand Down Expand Up @@ -172,4 +182,31 @@ defmodule DotcomWeb.Components.TripPlanner.TransitLeg do
</ul>
"""
end

defp group_alerts(alerts, leg) when is_list(alerts) do
grouped_alerts =
alerts
|> Enum.group_by(fn alert ->
alert.informed_entity.entities
|> Enum.any?(fn
%{stop: nil} -> false
_ -> true
end)
end)

route_alerts = grouped_alerts[false] || []
stop_alerts = grouped_alerts[true] || []

entities_from = Alerts.leg_entities_from(leg)
alerts_for_from = Match.match(stop_alerts, entities_from)

entities_to = Alerts.leg_entities_to(leg)
alerts_for_to = Match.match(stop_alerts, entities_to)

%{
alerts_for_mode: route_alerts,
alerts_for_from: alerts_for_from,
alerts_for_to: alerts_for_to
}
thecristen marked this conversation as resolved.
Show resolved Hide resolved
end
end
Loading