Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix View Switcher #977

Merged
merged 6 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions __tests__/util/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ describe('util > ui', () => {
describe('getItineraryView', () => {
it('returns a list mode by default or ui_activeItinerary is -1', () => {
expect(getItineraryView({})).toBe(ItineraryView.LIST)
expect(getItineraryView({ ui_activeItinerary: null })).toBe(
ItineraryView.LIST
)
expect(getItineraryView({ ui_activeItinerary: -1 })).toBe(
ItineraryView.LIST
)
Expand Down
20 changes: 15 additions & 5 deletions lib/actions/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
loadLocaleData
} from '../util/i18n'
import { getModesForActiveAgencyFilter, getUiUrlParams } from '../util/state'
import { getPathFromParts, ItineraryView } from '../util/ui'
import {
getPathFromParts,
isDefinedAndNotEqual,
ItineraryView
} from '../util/ui'

import {
clearActiveSearch,
Expand Down Expand Up @@ -114,12 +118,16 @@ export function setViewedTrip(payload) {
export function setMainPanelContent(payload) {
return function (dispatch, getState) {
const { otp, router } = getState()
if (otp.ui.mainPanelContent === payload) {
if (
otp.ui.mainPanelContent === payload &&
(payload || router.location.pathname === '/')
) {
// Do nothing if the panel is already set (but do something if changing to main panel
// and the router path is not at the root (/#/?ui_activeSearch=...).
// This will guard against over-enthusiastic routing and overwriting current/nested states.
console.warn(
`Attempt to route from ${otp.ui.mainPanelContent} to ${payload}. Doing nothing`
)
// Do nothing if the panel is already set. This will guard against over
// enthusiastic routing and overwriting current/nested states.
return
}
dispatch(setPanel(payload))
Expand Down Expand Up @@ -269,7 +277,9 @@ export function handleBackButtonPress(e) {
// previous search ID.
if (activeSearchId !== previousSearchId) {
dispatch(setActiveSearch(previousSearchId))
} else if (uiUrlParams.ui_activeItinerary !== previousItinIndex) {
} else if (
isDefinedAndNotEqual(uiUrlParams.ui_activeItinerary, previousItinIndex)
) {
// Active itinerary index has changed.
dispatch(setActiveItinerary({ index: previousItinIndex }))
}
Expand Down
28 changes: 6 additions & 22 deletions lib/components/app/view-switcher.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Button } from 'react-bootstrap'
import { connect } from 'react-redux'
import { FormattedMessage, useIntl } from 'react-intl'
import { useHistory } from 'react-router'
import React from 'react'

import * as uiActions from '../../actions/ui'
import { MainPanelContent } from '../../actions/ui-constants'
import { setMainPanelContent } from '../../actions/ui'

type Props = {
accountsActive: boolean
Expand All @@ -23,23 +22,18 @@ const ViewSwitcher = ({
setMainPanelContent,
sticky
}: Props) => {
const history = useHistory()
const intl = useIntl()

const _showRouteViewer = () => {
setMainPanelContent(MainPanelContent.ROUTE_VIEWER)
}

const _showTripPlanner = () => {
if (accountsActive) {
// Go up to root while preserving query parameters
history.push('..' + history.location.search)
} else {
setMainPanelContent(null)
}
// setMainPanelContent(null) already includes navigation to '/'.
setMainPanelContent(null)
}

const tripPlannerActive = activePanel === null
const tripPlannerActive = activePanel === null && !accountsActive
const routeViewerActive = activePanel === MainPanelContent.ROUTE_VIEWER

return (
Expand Down Expand Up @@ -84,25 +78,15 @@ const ViewSwitcher = ({
// connect to the redux store

const mapStateToProps = (state: any) => {
const { mainPanelContent } = state.otp.ui

// Reverse the ID to string mapping
const activePanelPair = Object.entries(MainPanelContent).find(
(keyValuePair) => keyValuePair[1] === mainPanelContent
)
// activePanel is array of form [string, ID]
// The trip planner has id null
const activePanel = (activePanelPair && activePanelPair[1]) || null

return {
// TODO: more reliable way of detecting these things, such as terms of storage page
accountsActive: state.router.location.pathname.includes('/account'),
activePanel
activePanel: state.otp.ui.mainPanelContent
}
}

const mapDispatchToProps = {
setMainPanelContent
setMainPanelContent: uiActions.setMainPanelContent
}

export default connect(mapStateToProps, mapDispatchToProps)(ViewSwitcher)
13 changes: 10 additions & 3 deletions lib/util/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ interface UrlParams {
ui_itineraryView: ItineraryView
}

export function isDefinedAndNotEqual(
subject: number | string,
value: number | string
): boolean {
return (
subject !== null && subject !== undefined && `${subject}` !== `${value}`
)
}

/**
* Gets the itinerary view to display based on URL params.
*/
Expand All @@ -102,9 +111,7 @@ export function getItineraryView({
}: UrlParams): ItineraryView {
return (
ui_itineraryView ||
(ui_activeItinerary !== undefined &&
`${ui_activeItinerary}` !== '-1' &&
ItineraryView.FULL) ||
(isDefinedAndNotEqual(ui_activeItinerary, -1) && ItineraryView.FULL) ||
ItineraryView.LIST
)
}