Skip to content

Commit 8bb40a8

Browse files
committed
fix(alert): fix time formatter for alert effective time
fix ibi-group/trimet-mod-otp#219
1 parent 2d2db00 commit 8bb40a8

File tree

5 files changed

+59
-29
lines changed

5 files changed

+59
-29
lines changed

lib/components/narrative/line-itin/line-itinerary.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,19 @@ export default class LineItinerary extends NarrativeItinerary {
6060

6161
return (
6262
<div className='line-itin'>
63-
<ItinerarySummary companies={companies} itinerary={itinerary} timeOptions={timeOptions} onClick={onClick} customIcons={customIcons} />
63+
<ItinerarySummary
64+
companies={companies}
65+
itinerary={itinerary}
66+
timeOptions={timeOptions}
67+
onClick={onClick}
68+
customIcons={customIcons} />
6469
{showRealtimeAnnotation && <SimpleRealtimeAnnotation />}
65-
{active || expanded ? <ItineraryBody {...this.props} itinerary={itinerary} timeOptions={timeOptions} /> : null}
70+
{active || expanded
71+
? <ItineraryBody
72+
{...this.props}
73+
itinerary={itinerary}
74+
timeOptions={timeOptions} />
75+
: null}
6676
{itineraryFooter}
6777
</div>
6878
)

lib/components/narrative/line-itin/transit-leg-body.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import moment from 'moment'
66

77
import ViewTripButton from '../../viewers/view-trip-button'
88
import { getIcon } from '../../../util/itinerary'
9-
import { formatDuration } from '../../../util/time'
9+
import { formatDuration, getTimeFormat } from '../../../util/time'
1010

1111
// TODO: support multi-route legs for profile routing
1212

@@ -38,7 +38,7 @@ class TransitLegBody extends Component {
3838
}
3939

4040
render () {
41-
const { customIcons, leg, operator } = this.props
41+
const { customIcons, leg, operator, timeFormat } = this.props
4242
const {
4343
agencyBrandingUrl,
4444
agencyName,
@@ -108,7 +108,7 @@ class TransitLegBody extends Component {
108108

109109
{/* The Alerts body, if visible */}
110110
<VelocityTransitionGroup enter={{animation: 'slideDown'}} leave={{animation: 'slideUp'}}>
111-
{alertsExpanded && <AlertsBody alerts={leg.alerts} />}
111+
{alertsExpanded && <AlertsBody alerts={leg.alerts} timeFormat={timeFormat} />}
112112
</VelocityTransitionGroup>
113113
{/* The "Ride X Min / X Stops" Row, including IntermediateStops body */}
114114
{leg.intermediateStops && leg.intermediateStops.length > 0 && (
@@ -151,7 +151,8 @@ class TransitLegBody extends Component {
151151

152152
const mapStateToProps = (state, ownProps) => {
153153
return {
154-
operator: state.otp.config.operators.find(operator => operator.id === ownProps.leg.agencyId)
154+
operator: state.otp.config.operators.find(operator => operator.id === ownProps.leg.agencyId),
155+
timeFormat: getTimeFormat(state.otp.config)
155156
}
156157
}
157158

@@ -184,22 +185,29 @@ class AlertsBody extends Component {
184185
}
185186

186187
render () {
188+
const { timeFormat } = this.props
187189
return (
188190
<div className='transit-alerts'>
189-
{this.props.alerts.sort((a, b) => (a.effectiveStartDate < b.effectiveStartDate) ? 1 : -1).map((alert, k) => {
190-
const effectiveStartDate = moment(alert.effectiveStartDate)
191-
let effectiveDateString = 'Effective as of '
192-
const daysAway = moment().diff(effectiveStartDate, 'days')
193-
if (Math.abs(daysAway) <= 1) effectiveDateString += moment(effectiveStartDate).format('h:MMa, ')
194-
effectiveDateString += effectiveStartDate.calendar(null, { sameElse: 'MMMM D, YYYY' }).split(' at')[0]
195-
return (
196-
<div key={k} className='transit-alert'>
197-
<div className='alert-icon'><i className='fa fa-exclamation-triangle' /></div>
198-
<div className='alert-body'>{alert.alertDescriptionText}</div>
199-
<div className='effective-date'>{effectiveDateString}</div>
200-
</div>
201-
)
202-
})}
191+
{this.props.alerts
192+
.sort((a, b) => b.effectiveStartDate - a.effectiveStartDate)
193+
.map((alert, i) => {
194+
const effectiveStartDate = moment(alert.effectiveStartDate)
195+
let effectiveDateString = 'Effective as of '
196+
const daysAway = moment().diff(effectiveStartDate, 'days')
197+
// Add time if alert is effective within one day.
198+
if (Math.abs(daysAway) <= 1) {
199+
effectiveDateString += `${effectiveStartDate.format(timeFormat)}, `
200+
}
201+
effectiveDateString += effectiveStartDate.calendar(null, { sameElse: 'MMMM D, YYYY' }).split(' at')[0]
202+
return (
203+
<div key={i} className='transit-alert'>
204+
<div className='alert-icon'><i className='fa fa-exclamation-triangle' /></div>
205+
<div className='alert-body'>{alert.alertDescriptionText}</div>
206+
<div className='effective-date'>{effectiveDateString}</div>
207+
</div>
208+
)
209+
})
210+
}
203211
</div>
204212
)
205213
}

lib/components/viewers/stop-viewer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ function getFormattedStopTime (stopTime, homeTimezone, soonText = 'Due', timeFor
371371

372372
const userTimeZone = moment.tz.guess()
373373
const inHomeTimezone = homeTimezone && homeTimezone === userTimeZone
374-
// Determine whether to show departure as countdown (e.g. "5 min") or as HH:MM time
374+
// Determine whether to show departure as countdown (e.g. "5 min") or as HH:mm time
375375
const secondsUntilDeparture = stopTime.realtimeDeparture - currentHomeTime
376376
const departsInFuture = stopTime.realtimeDeparture > currentHomeTime
377377
// Show the exact time if the departure occurs after midnight and if the

lib/util/query-params.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const queryParams = [
8282
default: getCurrentDate()
8383
},
8484

85-
{ /* time - the arrival/departure time for an itinerary trip, in HH:MM format */
85+
{ /* time - the arrival/departure time for an itinerary trip, in HH:mm format */
8686
name: 'time',
8787
routingTypes: [ 'ITINERARY' ],
8888
default: getCurrentTime()
@@ -95,13 +95,13 @@ const queryParams = [
9595
itineraryRewrite: value => ({ arriveBy: (value === 'ARRIVE') })
9696
},
9797

98-
{ /* startTime - the start time for a profile trip, in HH:MM format */
98+
{ /* startTime - the start time for a profile trip, in HH:mm format */
9999
name: 'startTime',
100100
routingTypes: [ 'PROFILE' ],
101101
default: '07:00'
102102
},
103103

104-
{ /* endTime - the end time for a profile trip, in HH:MM format */
104+
{ /* endTime - the end time for a profile trip, in HH:mm format */
105105
name: 'endTime',
106106
routingTypes: [ 'PROFILE' ],
107107
default: '09:00'

lib/util/time.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
import moment from 'moment'
22
import 'moment-timezone'
33

4+
const TIME_FORMAT_24_HR = 'HH:mm'
5+
6+
/**
7+
* @param {[type]} config the OTP config object found in store
8+
* @return {string} the config-defined time formatter or HH:mm (24-hr time)
9+
*/
410
export function getTimeFormat (config) {
5-
return (config.dateTime && config.dateTime.timeFormat) ? config.dateTime.timeFormat : 'HH:mm'
11+
return (config.dateTime && config.dateTime.timeFormat)
12+
? config.dateTime.timeFormat
13+
: TIME_FORMAT_24_HR
614
}
715

816
export function getDateFormat (config) {
9-
return (config.dateTime && config.dateTime.dateFormat) ? config.dateTime.dateFormat : 'YYYY-MM-DD'
17+
return (config.dateTime && config.dateTime.dateFormat)
18+
? config.dateTime.dateFormat
19+
: 'YYYY-MM-DD'
1020
}
1121

1222
export function getLongDateFormat (config) {
13-
return (config.dateTime && config.dateTime.longDateFormat) ? config.dateTime.longDateFormat : 'D MMMM YYYY'
23+
return (config.dateTime && config.dateTime.longDateFormat)
24+
? config.dateTime.longDateFormat
25+
: 'D MMMM YYYY'
1426
}
1527

1628
/**
@@ -35,7 +47,7 @@ export function formatDuration (seconds) {
3547
*/
3648
export function formatTime (ms, options) {
3749
return moment(ms + (options && options.offset ? options.offset : 0))
38-
.format(options && options.format ? options.format : 'HH:mm')
50+
.format(options && options.format ? options.format : TIME_FORMAT_24_HR)
3951
}
4052

4153
/**
@@ -56,7 +68,7 @@ export function formatStopTime (seconds, timezone = null, timeFormat = 'h:mm a')
5668
* @returns {string} formatted text representation
5769
*/
5870
export function getCurrentTime () {
59-
return moment().format('HH:mm')
71+
return moment().format(TIME_FORMAT_24_HR)
6072
}
6173

6274
/**

0 commit comments

Comments
 (0)