forked from HSLdevcom/digitransit-ui
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathRouteAlertsRow.js
240 lines (228 loc) · 6.8 KB
/
RouteAlertsRow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* eslint-disable jsx-a11y/no-noninteractive-tabindex */
import cx from 'classnames';
import capitalize from 'lodash/capitalize';
import moment from 'moment-timezone';
import PropTypes from 'prop-types';
import React from 'react';
import { intlShape } from 'react-intl';
import Link from 'found/Link';
import ExternalLink from './ExternalLink';
import Icon from './Icon';
import RouteNumber from './RouteNumber';
import ServiceAlertIcon from './ServiceAlertIcon';
import { PREFIX_ROUTES, PREFIX_STOPS } from '../util/path';
import { mapAlertSource } from '../util/alertUtils';
export const getTimePeriod = ({ currentTime, startTime, endTime, intl }) => {
const at = intl.formatMessage({
id: 'at-time',
});
const defaultFormat = `D.M.YYYY [${at}] HH:mm`;
const start = capitalize(
startTime.calendar(currentTime, {
lastDay: `[${intl.formatMessage({ id: 'yesterday' })} ${at}] HH:mm`,
sameDay: `[${intl.formatMessage({ id: 'today' })} ${at}] HH:mm`,
nextDay: `[${intl.formatMessage({ id: 'tomorrow' })} ${at}] HH:mm`,
lastWeek: defaultFormat,
nextWeek: defaultFormat,
sameElse: defaultFormat,
}),
);
if (!endTime) {
return start;
}
const end = endTime.calendar(startTime, {
sameDay: 'HH:mm',
nextDay: defaultFormat,
nextWeek: defaultFormat,
sameElse: defaultFormat,
});
return `${start} - ${end}`;
};
export default function RouteAlertsRow(
{
color,
currentTime,
description,
endTime,
entityIdentifier,
entityMode,
entityType,
expired,
severityLevel,
startTime,
url,
gtfsIds,
showRouteNameLink,
header,
source,
},
{ intl, config },
) {
const showTime = startTime && endTime && currentTime;
const gtfsIdList = gtfsIds ? gtfsIds.split(',') : [];
const routeLinks =
entityType === 'route' && entityIdentifier && gtfsIds
? entityIdentifier.split(',').map((identifier, i) => (
<Link
key={gtfsIdList[i]}
to={`/${PREFIX_ROUTES}/${gtfsIdList[i]}/${PREFIX_STOPS}`}
className={cx('route-alert-row-link', entityMode)}
style={{ color }}
>
{' '}
{identifier}
</Link>
))
: [];
const stopLinks =
entityType === 'stop' && entityIdentifier && gtfsIds
? entityIdentifier.split(',').map((identifier, i) => (
<Link
key={gtfsIdList[i]}
to={`/${PREFIX_STOPS}/${gtfsIdList[i]}`}
className={cx('route-alert-row-link', entityMode)}
>
{' '}
{identifier}
</Link>
))
: [];
const checkedUrl =
url && (url.match(/^[a-zA-Z]+:\/\//) ? url : `http://${url}`);
if (!description && !header) {
return null;
}
let genericCancellation;
if (!description) {
if (typeof header === 'string') {
genericCancellation = header;
} else if (header.props) {
const {
headsign,
routeMode,
shortName,
scheduledDepartureTime,
} = header.props;
if (headsign && routeMode && shortName && scheduledDepartureTime) {
const mode = intl.formatMessage({ id: routeMode.toLowerCase() });
genericCancellation = intl.formatMessage(
{ id: 'generic-cancelation' },
{
mode,
route: shortName,
headsign,
time: moment.unix(scheduledDepartureTime).format('HH:mm'),
},
);
}
}
}
return (
<div
className={cx('route-alert-row', { expired })}
role="listitem"
tabIndex={0}
>
{(entityType === 'route' && entityMode && (
<RouteNumber
alertSeverityLevel={severityLevel}
color={color}
mode={entityMode}
/>
)) ||
(entityType === 'stop' && (
<div className="route-number">
{severityLevel === 'INFO' ? (
<Icon img="icon-icon_info" className="stop-disruption info" />
) : (
<Icon
img="icon-icon_caution"
className="stop-disruption warning"
/>
)}
</div>
)) || (
<div className="route-number">
<ServiceAlertIcon severityLevel={severityLevel} />
</div>
)}
<div className="route-alert-contents">
{mapAlertSource(config, intl.locale, source)}
{(entityIdentifier || showTime) && (
<div className="route-alert-top-row">
{entityIdentifier &&
((entityType === 'route' &&
showRouteNameLink &&
routeLinks.length > 0 && <>{routeLinks} </>) ||
(!showRouteNameLink && (
<div
className={cx('route-alert-entityid', entityMode)}
style={{ color }}
>
{entityIdentifier}{' '}
</div>
)) ||
(entityType === 'stop' &&
showRouteNameLink &&
stopLinks.length > 0 && <>{stopLinks} </>) ||
(!showRouteNameLink && (
<div className={entityMode}>{entityIdentifier}</div>
)))}
{showTime && (
<>
{getTimePeriod({
currentTime: moment.unix(currentTime),
startTime: moment.unix(startTime),
endTime: description ? moment.unix(endTime) : undefined,
intl,
})}
</>
)}
</div>
)}
{(description || genericCancellation) && (
<div className="route-alert-body">
{description || genericCancellation}
{url && (
<ExternalLink className="route-alert-url" href={checkedUrl}>
{intl.formatMessage({ id: 'extra-info' })}
</ExternalLink>
)}
</div>
)}
</div>
</div>
);
}
RouteAlertsRow.propTypes = {
color: PropTypes.string,
currentTime: PropTypes.number,
description: PropTypes.string,
endTime: PropTypes.number,
entityIdentifier: PropTypes.string,
entityMode: PropTypes.string,
entityType: PropTypes.oneOf(['route', 'stop']),
expired: PropTypes.bool,
severityLevel: PropTypes.string,
startTime: PropTypes.number,
url: PropTypes.string,
gtfsIds: PropTypes.string,
showRouteNameLink: PropTypes.bool,
header: PropTypes.oneOfType([PropTypes.node, PropTypes.string]),
source: PropTypes.string,
};
RouteAlertsRow.contextTypes = {
config: PropTypes.object.isRequired,
intl: intlShape.isRequired,
};
RouteAlertsRow.defaultProps = {
currentTime: moment().unix(),
endTime: undefined,
expired: false,
entityIdentifier: undefined,
entityMode: undefined,
entityType: 'route',
severityLevel: undefined,
startTime: undefined,
header: undefined,
};