Skip to content

Commit

Permalink
potential approach for restoring auto-highlight behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
miles-grant-ibigroup committed Nov 17, 2023
1 parent d5a2d8d commit 56f43aa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
4 changes: 0 additions & 4 deletions lib/actions/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { getSecureFetchOptions } from '../util/middleware'
import { getStopViewerConfig } from '../util/state'

import { MainPanelContent } from './ui-constants'
import { setVisibleItinerary } from './narrative'
import v1Actions from './apiV1'
import v2Actions from './apiV2'

Expand Down Expand Up @@ -139,9 +138,6 @@ export function updateOtpUrlParams(state, searchId) {
params.ui_activeItinerary = -1
// At the same time, reset/delete the ui_itineraryView param.
params.ui_itineraryView = undefined
if (config.itinerary?.showFirstResultByDefault) {
dispatch(setVisibleItinerary({ index: 0 }))
}
// Merge in the provided OTP params and update the URL.
dispatch(setUrlSearch(Object.assign(params, otpParams)))
}
Expand Down
32 changes: 29 additions & 3 deletions lib/components/narrative/narrative-itineraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,16 @@ class NarrativeItineraries extends Component {
)
}

componentDidUpdate() {
componentDidUpdate(prevProps) {
// If set in URL, set the active itinerary in the state, once.
const {
activeItinerary,
activeSearch,
itineraryConfig,
mergedItineraries,
setActiveItinerary,
setVisibleItinerary
setVisibleItinerary,
visibleItinerary
} = this.props
const { ui_activeItinerary: uiActiveItinerary } =
coreUtils.query.getUrlParams() || {}
Expand All @@ -350,6 +353,28 @@ class NarrativeItineraries extends Component {
setActiveItinerary({ index: +uiActiveItinerary })
setVisibleItinerary({ index: +uiActiveItinerary })
}

/**
* Show first result by default is a lot more complicated now that we have
* fixed indeces. We must update the visible itinerary here instead of in the redux state.

Check failure on line 359 in lib/components/narrative/narrative-itineraries.js

View workflow job for this annotation

GitHub Actions / Check for spelling errors

indeces ==> indices
* Also, we need to update whenever new items arrive, to make sure that the highlighted
* itinerary is indeed the first one.
*
* Finally, we need to make sure we only update if the data changes, not if the user actually
* highlighted something else on their own.
*/
if (itineraryConfig?.showFirstResultByDefault) {
const firstSortedItin =
mergedItineraries?.length > 0 && mergedItineraries?.[0].index

if (
activeItinerary === -1 &&
(visibleItinerary === null || visibleItinerary === false) &&
prevProps.mergedItineraries.length !== mergedItineraries.length
) {
setVisibleItinerary({ index: firstSortedItin })
}
}
}

// eslint-disable-next-line complexity
Expand Down Expand Up @@ -531,7 +556,7 @@ const reduceErrorsFromResponse = (acc, cur) => {

const mapStateToProps = (state) => {
const { config, filter } = state.otp
const { co2, errorMessages, modes } = config
const { co2, errorMessages, itinerary, modes } = config
const { sort } = filter

const activeSearch = getActiveSearch(state)
Expand Down Expand Up @@ -590,6 +615,7 @@ const mapStateToProps = (state) => {
groupItineraries,
groupTransitModes,
itineraries: allItineraries,
itineraryConfig: itinerary,
itineraryIsExpanded,
// use a key so that the NarrativeItineraries component and its state is
// reset each time a new search is shown
Expand Down

0 comments on commit 56f43aa

Please sign in to comment.