diff --git a/__tests__/util/ui.ts b/__tests__/util/ui.ts index 813f90d2e..314e604e4 100644 --- a/__tests__/util/ui.ts +++ b/__tests__/util/ui.ts @@ -1,4 +1,8 @@ -import { getItineraryView, ItineraryView } from '../../lib/util/ui' +import { + getItineraryView, + getMapToggleNewItineraryView, + ItineraryView +} from '../../lib/util/ui' describe('util > ui', () => { describe('getItineraryView', () => { @@ -17,18 +21,21 @@ describe('util > ui', () => { ) }) it('returns an itinerary list view if URL contains ui_activeItinerary=-1 regardless of ui_itineraryView', () => { - expect( - getItineraryView({ - ui_activeItinerary: -1, - ui_itineraryView: ItineraryView.FULL - }) - ).toBe(ItineraryView.LIST) - expect( - getItineraryView({ - ui_activeItinerary: -1, - ui_itineraryView: ItineraryView.LEG - }) - ).toBe(ItineraryView.LIST) + const expectedValues = { + [ItineraryView.FULL]: ItineraryView.LIST, + [ItineraryView.LEG]: ItineraryView.LIST, + [ItineraryView.LEG_HIDDEN]: ItineraryView.LIST, + [ItineraryView.LIST]: ItineraryView.LIST, + [ItineraryView.LIST_HIDDEN]: ItineraryView.LIST_HIDDEN + } + Object.entries(expectedValues).forEach(([k, v]) => { + expect( + getItineraryView({ + ui_activeItinerary: -1, + ui_itineraryView: k + }) + ).toBe(v) + }) }) it('returns the specified view mode when set in URL', () => { expect( @@ -39,4 +46,17 @@ describe('util > ui', () => { ).toBe(ItineraryView.LEG) }) }) + describe('getMapToggleNewItineraryView', () => { + it('should obtain the new itinerary view value', () => { + const expectedValues = { + [ItineraryView.LEG]: ItineraryView.LEG_HIDDEN, + [ItineraryView.LEG_HIDDEN]: ItineraryView.LEG, + [ItineraryView.LIST]: ItineraryView.LIST_HIDDEN, + [ItineraryView.LIST_HIDDEN]: ItineraryView.LIST + } + Object.entries(expectedValues).forEach(([k, v]) => { + expect(getMapToggleNewItineraryView(k)).toBe(v) + }) + }) + }) }) diff --git a/lib/actions/apiV2.js b/lib/actions/apiV2.js index a1577ee7a..3290f9afd 100644 --- a/lib/actions/apiV2.js +++ b/lib/actions/apiV2.js @@ -879,8 +879,6 @@ export function routingQuery(searchId = null, updateSearchInReducer) { }) ) - dispatch(setItineraryView(ItineraryView.LIST)) - combinations.forEach((combo, index) => { const query = generateOtp2Query(combo) dispatch( diff --git a/lib/actions/ui.js b/lib/actions/ui.js index 3b254fa61..21856fd9b 100644 --- a/lib/actions/ui.js +++ b/lib/actions/ui.js @@ -9,12 +9,14 @@ import { getMatchingLocaleString, loadLocaleData } from '../util/i18n' -import { getModesForActiveAgencyFilter, getUiUrlParams } from '../util/state' import { + getItineraryView, + getMapToggleNewItineraryView, getPathFromParts, isDefinedAndNotEqual, ItineraryView } from '../util/ui' +import { getModesForActiveAgencyFilter, getUiUrlParams } from '../util/state' import { clearActiveSearch, @@ -39,7 +41,7 @@ export const setHoveredStop = createAction('SET_HOVERED_STOP') const viewTrip = createAction('SET_VIEWED_TRIP') const viewRoute = createAction('SET_VIEWED_ROUTE') export const toggleAutoRefresh = createAction('TOGGLE_AUTO_REFRESH') -const setPreviousItineraryView = createAction('SET_PREVIOUS_ITINERARY_VIEW') +const settingItineraryView = createAction('SET_ITINERARY_VIEW') export const setPopupContent = createAction('SET_POPUP_CONTENT') // This code-less action calls the reducer code @@ -314,21 +316,15 @@ export function handleBackButtonPress(e) { export function setItineraryView(value) { return function (dispatch, getState) { const urlParams = coreUtils.query.getUrlParams() - const prevItineraryView = urlParams.ui_itineraryView || ItineraryView.LIST // If the itinerary value is changed, - // set the desired ui query param + // set the desired ui query param (even if LIST, so it replaces the current value) // and store the current view as previousItineraryView. - if (value !== urlParams.ui_itineraryView) { - if (value !== ItineraryView.LIST) { - urlParams.ui_itineraryView = value - } else if (urlParams.ui_itineraryView) { - // Remove the ui_itineraryView param if it is set to LIST (default). - delete urlParams.ui_itineraryView - } + if (value !== getItineraryView(urlParams)) { + urlParams.ui_itineraryView = value dispatch(setUrlSearch(urlParams)) - dispatch(setPreviousItineraryView(prevItineraryView)) + dispatch(settingItineraryView(value)) } } } @@ -340,16 +336,10 @@ export function setItineraryView(value) { export function toggleBatchResultsMap() { return function (dispatch, getState) { const urlParams = coreUtils.query.getUrlParams() - const itineraryView = urlParams.ui_itineraryView || ItineraryView.LIST + const itineraryView = getItineraryView(urlParams) - if (itineraryView === ItineraryView.LEG) { - dispatch(setItineraryView(ItineraryView.LEG_HIDDEN)) - } else if (itineraryView === ItineraryView.LIST) { - dispatch(setItineraryView(ItineraryView.LIST_HIDDEN)) - } else { - const { previousItineraryView } = getState().otp.ui - dispatch(setItineraryView(previousItineraryView)) - } + const newView = getMapToggleNewItineraryView(itineraryView) + dispatch(setItineraryView(newView)) } } diff --git a/lib/components/mobile/batch-results-screen.js b/lib/components/mobile/batch-results-screen.js index 2e88d01e9..69119f1c3 100644 --- a/lib/components/mobile/batch-results-screen.js +++ b/lib/components/mobile/batch-results-screen.js @@ -143,7 +143,7 @@ const mapStateToProps = (state) => { activeLeg: activeSearch ? activeSearch.activeLeg : null, errors: getResponsesWithErrors(state), itineraries: getActiveItineraries(state), - itineraryView: getItineraryView(urlParams) + itineraryView: getItineraryView(urlParams) || state.otp.ui.itineraryView } } diff --git a/lib/reducers/create-otp-reducer.js b/lib/reducers/create-otp-reducer.js index 6fc02bb58..472598ab7 100644 --- a/lib/reducers/create-otp-reducer.js +++ b/lib/reducers/create-otp-reducer.js @@ -1053,9 +1053,9 @@ function createOtpReducer(config) { }) case 'UPDATE_ITINERARY_FILTER': return update(state, { filter: { $set: action.payload } }) - case 'SET_PREVIOUS_ITINERARY_VIEW': + case 'SET_ITINERARY_VIEW': return update(state, { - ui: { previousItineraryView: { $set: action.payload } } + ui: { itineraryView: { $set: action.payload } } }) case 'UPDATE_LOCALE': return update(state, { diff --git a/lib/util/ui.ts b/lib/util/ui.ts index 54dba6d99..ed992e860 100644 --- a/lib/util/ui.ts +++ b/lib/util/ui.ts @@ -113,9 +113,29 @@ export function getItineraryView({ ((ui_activeItinerary === null || ui_activeItinerary === undefined || `${ui_activeItinerary}` === '-1') && - ItineraryView.LIST) || + (ui_itineraryView === ItineraryView.LIST_HIDDEN + ? ItineraryView.LIST_HIDDEN + : ItineraryView.LIST)) || ui_itineraryView || (isDefinedAndNotEqual(ui_activeItinerary, -1) && ItineraryView.FULL) || ItineraryView.LIST ) } + +/** + * Gets the new itinerary view to display based on current view. + */ +export function getMapToggleNewItineraryView( + currentView: ItineraryView +): ItineraryView { + switch (currentView) { + case ItineraryView.LEG: + return ItineraryView.LEG_HIDDEN + case ItineraryView.LIST: + return ItineraryView.LIST_HIDDEN + case ItineraryView.LEG_HIDDEN: + return ItineraryView.LEG + default: + return ItineraryView.LIST + } +}