diff --git a/lib/components/form/error-message.js b/lib/components/form/error-message.js index 9a9cb8cf1..31f3122b8 100644 --- a/lib/components/form/error-message.js +++ b/lib/components/form/error-message.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types' import { connect } from 'react-redux' import TripTools from '../narrative/trip-tools' -import { getActiveErrors } from '../../util/state' +import { getActiveError } from '../../util/state' class ErrorMessage extends Component { static propTypes = { @@ -46,7 +46,7 @@ class ErrorMessage extends Component { const mapStateToProps = (state, ownProps) => { return { - error: getActiveErrors(state.otp)[0], + error: getActiveError(state.otp), currentQuery: state.otp.currentQuery, errorMessages: state.otp.config.errorMessages } diff --git a/lib/components/mobile/results-screen.js b/lib/components/mobile/results-screen.js index c5b8448d5..1da52d089 100644 --- a/lib/components/mobile/results-screen.js +++ b/lib/components/mobile/results-screen.js @@ -17,7 +17,7 @@ import { MobileScreens, setMobileScreen } from '../../actions/ui' import { setUseRealtimeResponse } from '../../actions/narrative' import { clearActiveSearch } from '../../actions/form' import { - getActiveErrors, + getActiveError, getActiveItineraries, getActiveSearch, getRealtimeEffects @@ -244,7 +244,7 @@ const mapStateToProps = (state, ownProps) => { return { query: state.otp.currentQuery, realtimeEffects, - error: getActiveErrors(state.otp)[0], + error: getActiveError(state.otp), resultCount: response ? activeSearch.query.routingType === 'ITINERARY' diff --git a/lib/components/narrative/narrative-itineraries.js b/lib/components/narrative/narrative-itineraries.js index 76a450dfe..93b245fa6 100644 --- a/lib/components/narrative/narrative-itineraries.js +++ b/lib/components/narrative/narrative-itineraries.js @@ -16,7 +16,7 @@ import DefaultItinerary from './default/default-itinerary' import Icon from '../narrative/icon' import LinkButton from '../user/link-button' import { - getActiveErrors, + getResponsesWithErrors, getActiveItineraries, getActiveSearch, getRealtimeEffects @@ -254,7 +254,7 @@ const mapStateToProps = (state, ownProps) => { const useRealtime = state.otp.useRealtime return { activeSearch, - errors: getActiveErrors(state.otp), + errors: getResponsesWithErrors(state.otp), // swap out realtime itineraries with non-realtime depending on boolean itineraries, pending, diff --git a/lib/components/narrative/narrative-routing-results.js b/lib/components/narrative/narrative-routing-results.js index 294080dfd..12f9f5fb3 100644 --- a/lib/components/narrative/narrative-routing-results.js +++ b/lib/components/narrative/narrative-routing-results.js @@ -7,7 +7,7 @@ import TabbedItineraries from './tabbed-itineraries' import ErrorMessage from '../form/error-message' import { - getActiveErrors, + getActiveError, getActiveItineraries, getActiveSearch } from '../../util/state' @@ -51,7 +51,7 @@ const mapStateToProps = (state, ownProps) => { const pending = activeSearch ? Boolean(activeSearch.pending) : false return { mainPanelContent: state.otp.ui.mainPanelContent, - error: getActiveErrors(state.otp)[0], + error: getActiveError(state.otp), itineraries: getActiveItineraries(state.otp), pending, routingType: activeSearch && activeSearch.query.routingType diff --git a/lib/util/state.js b/lib/util/state.js index 72dfa3114..a3459b805 100644 --- a/lib/util/state.js +++ b/lib/util/state.js @@ -25,20 +25,32 @@ export function getTimestamp (time = moment()) { } /** - * Gets the active errors returned for the OTP responses. + * Gets the OTP responses that contain errors. For batch routing this could be + * an array of multiple responses, but for standard itinerary routing it will be + * at most an array with one response. * @param {Object} otpState the OTP state object - * @return {Array} array of OTP plan responses with errors + * @return {Array} array of OTP plan responses that contain errors */ -export function getActiveErrors (otpState) { +export function getResponsesWithErrors (otpState) { const search = getActiveSearch(otpState) - const errors = [] + const errorResponses = [] const response = !search ? null : search.response if (response) { response.forEach(res => { - if (res && res.error) errors.push(res) + if (res && res.error) errorResponses.push(res) }) } - return errors + return errorResponses +} + +/** + * Gets the active error (first OTP plan response that contains an error). Only + * one OTP response is expected in itinerary routing, but if batch routing is + * enabled, multiple responses with errors should be handled. + */ +export function getActiveError (otpState) { + const errorResponse = getResponsesWithErrors(otpState)[0] + if (errorResponse && errorResponse.error) return errorResponse.error } /**