Skip to content

Commit

Permalink
Display stopOnRoute alert in StopPage
Browse files Browse the repository at this point in the history
  • Loading branch information
hbruch committed Dec 4, 2024
1 parent 110ed8b commit 33deb47
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 19 deletions.
62 changes: 46 additions & 16 deletions app/component/AlertRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,29 @@ export const getTimePeriod = ({ currentTime, startTime, endTime, intl }) => {
return `${start} - ${end}`;
};

const getColor = entities => {
const getRouteFromEntities = entities => {
if (Array.isArray(entities)) {
const routeEntities = getEntitiesOfType(entities, AlertEntityType.Route);
return routeEntities.length > 0 && `#${routeEntities[0].color}`;
const stopOnRouteEntities = getEntitiesOfType(
entities,
AlertEntityType.StopOnRoute,
);
const route =
(routeEntities.length > 0 && routeEntities[0]) ||
(stopOnRouteEntities.length > 0 && stopOnRouteEntities[0].route);
return route || null;
}
return null;
};

const getColor = entities => {
const route = getRouteFromEntities(entities);
return (route && `#${route.color}`) || null;
};

const getMode = entities => {
if (Array.isArray(entities)) {
const routeEntities = getEntitiesOfType(entities, AlertEntityType.Route);
return routeEntities.length > 0 && getRouteMode(routeEntities[0]);
}
return 'bus';
const route = getRouteFromEntities(entities);
return (route && getRouteMode(route)) || 'bus';
};

const getGtfsIds = entities => entities?.map(entity => entity.gtfsId) || [];
Expand All @@ -69,6 +78,7 @@ const getEntityIdentifiers = entities =>
?.map(
entity =>
entity.shortName ||
entity?.route?.shortName ||
(entity.code ? `${entity.name} (${entity.code})` : entity.name),
)
.filter(identifier => identifier);
Expand All @@ -84,6 +94,17 @@ const getEntitiesWithUniqueIdentifiers = entities => {
return Object.values(entitiesByIdentifier);
};

const getEntityType = entities => {
const entityTypes = [
AlertEntityType.Stop,
AlertEntityType.Route,
AlertEntityType.StopOnRoute,
];
return entityTypes.find(
entityType => getEntitiesOfType(entities, entityType).length > 0,
);
};

export default function AlertRow(
{
currentTime,
Expand Down Expand Up @@ -112,15 +133,16 @@ export default function AlertRow(
const gtfsIdList = getGtfsIds(uniqueEntities);
const entityIdentifiers = getEntityIdentifiers(uniqueEntities);

const entityType =
getEntitiesOfType(uniqueEntities, AlertEntityType.Stop).length > 0
? AlertEntityType.Stop
: AlertEntityType.Route;
const entityType = getEntityType(uniqueEntities);

const routeColor =
entityType === AlertEntityType.Route && getColor(uniqueEntities);
(entityType === AlertEntityType.Route ||
entityType === AlertEntityType.StopOnRoute) &&
getColor(uniqueEntities);
const routeMode =
entityType === AlertEntityType.Route && getMode(uniqueEntities);
(entityType === AlertEntityType.Route ||
entityType === AlertEntityType.StopOnRoute) &&
getMode(uniqueEntities);

const routeLinks =
entityType === AlertEntityType.Route && entityIdentifiers && gtfsIdList
Expand Down Expand Up @@ -168,7 +190,8 @@ export default function AlertRow(

return (
<div className="alert-row" role="listitem">
{(entityType === AlertEntityType.Route && (
{((entityType === AlertEntityType.Route ||
entityType === AlertEntityType.StopOnRoute) && (
<RouteNumber
alertSeverityLevel={severityLevel}
color={routeColor}
Expand Down Expand Up @@ -196,7 +219,8 @@ export default function AlertRow(
<div className="alert-top-row">
{entityIdentifiers &&
entityIdentifiers.length > 0 &&
((entityType === AlertEntityType.Route &&
(((entityType === AlertEntityType.Route ||
entityType === AlertEntityType.StopOnRoute) &&
showLinks &&
routeLinks.length > 0 && <>{routeLinks} </>) ||
(!showLinks && (
Expand Down Expand Up @@ -246,7 +270,13 @@ AlertRow.propTypes = {
entities: PropTypes.arrayOf(
PropTypes.shape({
__typename: PropTypes.string.isRequired,
gtfsId: PropTypes.string.isRequired,
gtfsId: PropTypes.string,
route: PropTypes.shape({
gtfsId: PropTypes.string,
}),
stop: PropTypes.shape({
gtfsId: PropTypes.string,
}),
}),
),
severityLevel: PropTypes.string,
Expand Down
6 changes: 5 additions & 1 deletion app/component/StopAlerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export const isRelevantEntity = (entity, stopIds, routeIds) =>
stopIds.includes(entity.gtfsId)) ||
// eslint-disable-next-line no-underscore-dangle
(entity.__typename === AlertEntityType.Route &&
routeIds.includes(entity.gtfsId));
routeIds.includes(entity.gtfsId)) ||
// eslint-disable-next-line no-underscore-dangle
(entity.__typename === AlertEntityType.StopOnRoute &&
routeIds.includes(entity.route.gtfsId) &&
stopIds.includes(entity.stop.gtfsId));

export const getRouteIdsForStop = stop =>
uniq(stop?.routes.map(route => route.gtfsId));
Expand Down
14 changes: 13 additions & 1 deletion app/component/StopAlertsContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const containerComponent = createFragmentContainer(StopAlertsContainer, {
}
gtfsId
locationType
alerts(types: [STOP, ROUTES]) {
alerts(types: [STOP, ROUTES, STOP_ON_ROUTES]) {
id
alertDescriptionText
alertHash
Expand All @@ -42,6 +42,18 @@ const containerComponent = createFragmentContainer(StopAlertsContainer, {
... on Stop {
gtfsId
}
... on StopOnRoute {
stop {
gtfsId
}
route {
color
type
mode
shortName
gtfsId
}
}
}
}
stoptimes: stoptimesWithoutPatterns(
Expand Down
2 changes: 1 addition & 1 deletion app/component/StopPageTabContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const containerComponent = createFragmentContainer(StopPageTabContainer, {
id
gtfsId
code
alerts(types: [STOP, ROUTES]) {
alerts(types: [STOP, ROUTES, STOP_ON_ROUTES]) {
id
alertSeverityLevel
effectiveEndDate
Expand Down

0 comments on commit 33deb47

Please sign in to comment.