-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathdefault-itinerary.js
442 lines (414 loc) · 12.9 KB
/
default-itinerary.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// This is a large file being touched by open PRs. It should be typescripted
// in a separate PR.
import { AccessibilityRating } from '@opentripplanner/itinerary-body'
import { connect } from 'react-redux'
import {
FormattedList,
FormattedMessage,
FormattedNumber,
FormattedTime,
injectIntl
} from 'react-intl'
import { Leaf } from '@styled-icons/fa-solid/Leaf'
import clone from 'clone'
import coreUtils from '@opentripplanner/core-utils'
import React from 'react'
import styled from 'styled-components'
import { ComponentContext } from '../../../util/contexts'
import { getFare, getFirstLegStartTime } from '../../../util/itinerary'
import { Icon, StyledIconWrapperTextAlign } from '../../util/styledIcon'
import { itineraryHasAccessibilityScore } from '../../../util/accessibility-routing'
import { localizeGradationMap } from '../utils'
import FieldTripGroupSize from '../../admin/field-trip-itinerary-group-size'
import FormattedDuration from '../../util/formatted-duration'
import ItineraryBody from '../line-itin/connected-itinerary-body'
import NarrativeItinerary from '../narrative-itinerary'
import RouteBlock from '../metro/route-block'
import SimpleRealtimeAnnotation from '../simple-realtime-annotation'
import { FlexIndicator } from './flex-indicator'
import { ItineraryDescription } from './itinerary-description'
import ItinerarySummary from './itinerary-summary'
const {
getItineraryCost,
isAdvanceBookingRequired,
isCoordinationRequired,
isFlex,
isTransitLeg
} = coreUtils.itinerary
const { ensureAtLeastOneMinute } = coreUtils.time
// Styled components
const LegIconWrapper = styled.div`
display: inline-block;
height: 20px;
padding-bottom: 6px;
padding-left: 2px;
width: 20px;
/* Equivalent of a single space before the leg icon. */
&::before {
content: '';
margin: 0 ${(props) => (props.noSpace ? '' : '0.125em')};
}
`
const DetailsHintButton = styled.button`
&.visible {
display: contents;
}
/* the width/height/overflow trick still renders a tiny button,
so make it invisible and out of the way, but still visible to
screen readers */
background: transparent;
border: none;
height: 0;
overflow: hidden;
position: absolute;
width: 0;
`
const DetailsHint = styled.div`
clear: both;
color: #685c5c;
cursor: pointer;
font-size: small;
text-align: center;
`
const ItinerarySummaryWrapper = styled.div`
display: flex;
justify-content: space-between;
`
const ITINERARY_ATTRIBUTES = [
{
alias: 'best',
id: 'duration',
order: 0,
render: (itinerary) => (
<FormattedDuration
duration={ensureAtLeastOneMinute(itinerary.duration)}
/>
)
},
{
alias: 'departureTime',
id: 'arrivalTime',
order: 1,
render: (itinerary, options) => {
if (options.alwaysShowBothTimes) {
return (
<FormattedMessage
id="common.itineraryDescriptions.timeStartEnd"
values={{
end: <FormattedTime value={itinerary.endTime} />,
start: <FormattedTime value={itinerary.startTime} />
}}
/>
)
}
if (options.selection === 'ARRIVALTIME') {
return <FormattedTime value={itinerary.endTime} />
}
if (options.selection !== 'DEPARTURETIME' && itinerary.allStartTimes) {
const allStartTimes = itinerary.allStartTimes.sort(
(a, b) => getFirstLegStartTime(a.legs) - getFirstLegStartTime(b.legs)
)
return (
<FormattedList
type="conjunction"
value={allStartTimes.map(({ legs }, index) => (
<FormattedTime key={index} value={getFirstLegStartTime(legs)} />
))}
/>
)
}
return <FormattedTime value={itinerary.startTime} />
}
},
{
id: 'cost',
order: 2,
render: (itinerary, options, defaultFareType) => {
const fare = getFare(itinerary, defaultFareType)
const currency = getItineraryCost(
itinerary.legs,
defaultFareType.mediumId,
defaultFareType.riderCategoryId
)?.currency
if (
fare === null ||
fare === undefined ||
fare < 0 ||
currency === undefined
) {
return (
<FormattedMessage id="common.itineraryDescriptions.fareUnknown" />
)
}
return (
<FormattedNumber
// Currency from itinerary fare or from config.
currency={fare.fareCurrency}
currencyDisplay="narrowSymbol"
// eslint-disable-next-line react/style-prop-object
style="currency"
value={fare.transitFare + fare.maxTNCFare}
/>
)
}
},
{
id: 'walkTime',
order: 3,
render: (itinerary, options) => {
const leg = clone(itinerary.legs[0])
if (isTransitLeg(leg)) {
leg.mode = 'WALK'
}
const { LegIcon } = options
return (
// FIXME: For CAR mode, walk time considers driving time.
<>
<FormattedDuration
duration={
itinerary.walkTime > 0
? ensureAtLeastOneMinute(itinerary.walkTime)
: itinerary.walkTime
}
includeSeconds={false}
/>
<LegIconWrapper noSpace>
<LegIcon leg={{ ...leg, mode: 'WALK' }} size={5} />
</LegIconWrapper>
</>
)
}
},
{
id: 'carbonEmission',
order: 4,
render: (itinerary, { co2Config }) => {
if (!co2Config?.enabled) return
const { cutoffPercentage, showIfHigher } = co2Config
const { co2VsBaseline } = itinerary
if (!showIfHigher && co2VsBaseline > 0) return
const ROUND_TO_NEAREST = 5
const co2VsBaselineRounded =
Math.round((co2VsBaseline * 100) / ROUND_TO_NEAREST) * ROUND_TO_NEAREST
if (Math.abs(co2VsBaseline * 100) <= cutoffPercentage) return // Only show greater than the cutoff
return (
<>
<FormattedMessage
id="common.itineraryDescriptions.relativeCo2"
values={{
co2: (
<FormattedNumber
style="unit"
unit="percent"
unitDisplay="narrow"
value={Math.abs(co2VsBaselineRounded)}
/>
),
isMore: co2VsBaselineRounded > 0
}}
/>
<StyledIconWrapperTextAlign>
<Icon Icon={Leaf} />
</StyledIconWrapperTextAlign>
</>
)
}
}
]
class DefaultItinerary extends NarrativeItinerary {
static contextType = ComponentContext
_onMouseEnter = () => {
const { active, index, setVisibleItinerary, visibleItinerary } = this.props
// Set this itinerary as visible if not already visible.
const visibleNotSet =
visibleItinerary === null || visibleItinerary === undefined
const isVisible =
visibleItinerary === index || (active === index && visibleNotSet)
if (typeof setVisibleItinerary === 'function' && !isVisible) {
setVisibleItinerary({ index })
}
}
_onMouseLeave = () => {
const { index, setVisibleItinerary, visibleItinerary } = this.props
if (
typeof setVisibleItinerary === 'function' &&
visibleItinerary === index
) {
setVisibleItinerary({ index: null })
}
}
_isSortingOnAttribute = (attribute) => {
const { sort } = this.props
if (sort && sort.type) {
const type = sort.type.toLowerCase()
return (
attribute.id.toLowerCase() === type ||
(attribute.alias && attribute.alias.toLowerCase() === type)
)
}
return false
}
// eslint-disable-next-line complexity
render() {
const {
accessibilityScoreGradationMap,
alwaysShowBothTimes,
co2Config,
configCosts,
defaultFareType,
expanded,
intl,
itinerary,
LegIcon,
setActiveLeg,
showRealtimeAnnotation,
showResultHeaders,
visible
} = this.props
const isFlexItinerary = itinerary.legs.some(isFlex)
const isCallAhead = itinerary.legs.some(isAdvanceBookingRequired)
const { SvgIcon } = this.context
const localizedGradationMapWithIcons = localizeGradationMap(
intl,
SvgIcon,
accessibilityScoreGradationMap
)
// Use first leg's agency as a fallback
const agency = itinerary.legs
.map((leg) => leg?.agencyName)
.filter((name) => !!name)[0]
let phone = `contact ${agency}`
if (isCallAhead) {
// Picking 0 ensures that if multiple flex legs with
// different phone numbers, the first leg is prioritized
phone = itinerary.legs
.map((leg) => leg.pickupBookingInfo?.contactInfo?.phoneNumber)
.filter((number) => !!number)[0]
}
const itineraryAttributeOptions = {
alwaysShowBothTimes,
co2Config,
configCosts,
LegIcon
}
const renderItineraryAttributes = (attribute) => {
return attribute.render(
itinerary,
itineraryAttributeOptions,
defaultFareType
)
}
return (
<div
className={`option default-itin${visible ? ' active' : ''}${
expanded ? ' expanded' : ''
}`}
onMouseEnter={this._onMouseEnter}
onMouseLeave={this._onMouseLeave}
// TODO: CORRECT THIS ARIA ROLE
role="presentation"
// TODO test this with a screen reader
// tabIndex={expanded ? 1 : 0}
>
<ItinerarySummaryWrapper
className="header"
// _onHeaderClick comes from super component (NarrativeItinerary).
onClick={this._onHeaderClick}
>
<div className="title">
{showResultHeaders && (
<h3>
<ItineraryDescription intl={intl} itinerary={itinerary} />
</h3>
)}
<ItinerarySummary itinerary={itinerary} LegIcon={LegIcon} />
{itineraryHasAccessibilityScore(itinerary) && (
<AccessibilityRating
gradationMap={localizedGradationMapWithIcons}
large
score={itinerary.accessibilityScore}
/>
)}
</div>
{isFlexItinerary && (
<FlexIndicator
isCallAhead={isCallAhead}
isContinuousDropoff={isCoordinationRequired}
phoneNumber={phone}
/>
)}
<ul
aria-label={intl.formatMessage({
id: 'components.ItinerarySummary.itineraryDetails'
})}
className="list-unstyled itinerary-attributes"
>
<FieldTripGroupSize itinerary={itinerary} />
{ITINERARY_ATTRIBUTES.sort((a, b) => {
const aSelected = this._isSortingOnAttribute(a)
const bSelected = this._isSortingOnAttribute(b)
if (aSelected) return -1
if (bSelected) return 1
return a.order - b.order
})
.filter((x) => renderItineraryAttributes(x) !== undefined)
.map((attribute) => {
const isSelected = this._isSortingOnAttribute(attribute)
if (isSelected) {
itineraryAttributeOptions.isSelected = true
itineraryAttributeOptions.selection = this.props.sort.type
}
return (
<li
className={`${attribute.id}${isSelected ? ' main' : ''}`}
key={attribute.id}
>
{renderItineraryAttributes(attribute)}
</li>
)
})}
</ul>
</ItinerarySummaryWrapper>
{!expanded && (
<DetailsHintButton
className={visible && !expanded && 'visible'}
onClick={this._onDirectClick}
>
<DetailsHint>
<FormattedMessage id="components.DefaultItinerary.clickDetails" />
</DetailsHint>
</DetailsHintButton>
)}
{visible && expanded && (
<>
{showRealtimeAnnotation && <SimpleRealtimeAnnotation />}
<ItineraryBody
accessibilityScoreGradationMap={localizedGradationMapWithIcons}
itinerary={itinerary}
LegIcon={LegIcon}
RouteDescriptionOverride={RouteBlock}
setActiveLeg={setActiveLeg}
/>
</>
)}
</div>
)
}
}
const mapStateToProps = (state, ownProps) => {
return {
accessibilityScoreGradationMap:
state.otp.config.accessibilityScore?.gradationMap,
alwaysShowBothTimes: state.otp.config.itinerary?.alwaysShowBothTimes,
co2Config: state.otp.config.co2,
configCosts: state.otp.config.itinerary?.costs,
defaultFareType: state.otp.config.itinerary?.defaultFareType || {
mediumId: null,
riderCategoryId: null
},
showResultHeaders:
state.otp.config.itinerary?.showBatchUiItineraryHeaders !== false
}
}
DefaultItinerary.ModesAndRoutes = ItinerarySummary
export default injectIntl(connect(mapStateToProps)(DefaultItinerary))