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

Remove ui_activeItinerary from URL params when value is -1 #1234

Merged
merged 3 commits into from
Jul 1, 2024
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
4 changes: 2 additions & 2 deletions lib/actions/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ export function updateOtpUrlParams(state, searchId) {
})

params.ui_activeSearch = searchId
// Assumes this is a new search and the active itinerary should be reset.
params.ui_activeItinerary = -1
// Assumes this is a new search and the active itinerary should be reset (i.e. removed).
params.ui_activeItinerary = undefined
// At the same time, reset/delete the ui_itineraryView param.
params.ui_itineraryView = undefined
// Merge in the provided OTP params and update the URL.
Expand Down
2 changes: 2 additions & 0 deletions lib/actions/narrative.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export function setActiveItinerary(payload) {
// Remove the ui_itineraryView param if changing to another itinerary.
// Note: set to undefined instead of deleting so that it merges with the other search params.
urlParams.ui_itineraryView = undefined
// Also remove the ui_activeItinerary from URL because that's the default value.
urlParams.ui_activeItinerary = undefined
}

dispatch(setUrlSearch(urlParams))
Expand Down
5 changes: 4 additions & 1 deletion lib/actions/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getItineraryView,
getMapToggleNewItineraryView,
getPathFromParts,
isDefined,
isDefinedAndNotEqual,
ItineraryView
} from '../util/ui'
Expand Down Expand Up @@ -334,7 +335,9 @@ export function handleBackButtonPress(e) {
// console.log('back button pressed', e)
const urlParams = coreUtils.query.getUrlParams()
const previousSearchId = urlParams.ui_activeSearch
const previousItinIndex = +urlParams.ui_activeItinerary || 0
const previousItinIndex = isDefined(urlParams.ui_activeItinerary)
? urlParams.ui_activeItinerary
: -1
const previousSearch = state.otp.searches[previousSearchId]
if (previousSearch) {
// If back button pressed and active search has changed, set search to
Expand Down
3 changes: 2 additions & 1 deletion lib/components/map/itinerary-summary-overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getActiveSearch,
getVisibleItineraryIndex
} from '../../util/state'
import { isDefined } from '../../util/ui'
import MetroItineraryRoutes from '../narrative/metro/metro-itinerary-routes'

type ItinWithGeometry = Itinerary & {
Expand Down Expand Up @@ -156,7 +157,7 @@ const ItinerarySummaryOverlay = ({
(mp) =>
// If no itinerary is hovered, show all of them. If one is selected, show only that one
// TODO: clean up conditionals, move these to a more appropriate place without breaking indexing
(visibleItinerary !== null && visibleItinerary !== undefined
(isDefined(visibleItinerary)
? visibleItinerary === mp.itin.index
: true) &&
mp.uniquePoint && (
Expand Down
7 changes: 3 additions & 4 deletions lib/components/narrative/narrative-itineraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import {
itinerariesAreEqual,
sortStartTimes
} from '../../util/itinerary'
import { getItineraryView, ItineraryView } from '../../util/ui'
import { getItineraryView, isDefined, ItineraryView } from '../../util/ui'
import { grey } from '../util/colors'
import {
setActiveItinerary,
setActiveLeg,
Expand All @@ -41,7 +42,6 @@ import PageTitle from '../util/page-title'

import * as S from './styled'
import { getItineraryDescription } from './default/itinerary-description'
import { grey } from '../util/colors'
import ErrorRenderer from './metro/metro-error-renderer'
import Loading from './loading'
import NarrativeItinerariesErrors from './narrative-itineraries-errors'
Expand Down Expand Up @@ -586,8 +586,7 @@ const mapStateToProps = (state) => {
} = config.itinerary || false
// Default to true for backwards compatibility
const renderSkeletons = !config.itinerary?.hideSkeletons
const itineraryIsExpanded =
activeItinerary !== undefined && activeItinerary !== null && showDetails
const itineraryIsExpanded = isDefined(activeItinerary) && showDetails
const { localUser, loggedInUser } = state.user
const user = loggedInUser || localUser

Expand Down
5 changes: 4 additions & 1 deletion lib/util/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
addSortingCosts,
collectItinerariesWithoutDuplicates
} from './itinerary'
import { isDefined } from './ui'

// For lowercase context
const LowerCase = styled.span`
Expand Down Expand Up @@ -660,7 +661,9 @@ export function getShowUserSettings(state) {
export function getUiUrlParams(state) {
const activeSearch = getActiveSearch(state)
const uiParams = {
ui_activeItinerary: activeSearch ? activeSearch.activeItinerary : 0,
ui_activeItinerary: isDefined(activeSearch?.activeItinerary)
? activeSearch?.activeItinerary
: -1,
ui_activeSearch: state.otp.activeSearchId
}
return uiParams
Expand Down
12 changes: 8 additions & 4 deletions lib/util/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,17 @@ interface UrlParams {
ui_itineraryView: ItineraryView
}

export function isDefined(
subject: number | string | null | undefined
): boolean {
return subject !== null && subject !== undefined
}

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

/**
Expand Down
Loading