From a8bec1a58111dedf93b9bda6457d40b3977d00b3 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:19:29 -0400 Subject: [PATCH 1/2] refactor(api,narrative): Remove ui_activeItinerary param if default (-1). --- lib/actions/api.js | 4 ++-- lib/actions/narrative.js | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/actions/api.js b/lib/actions/api.js index f0a466da0..2a1f64b7d 100644 --- a/lib/actions/api.js +++ b/lib/actions/api.js @@ -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. diff --git a/lib/actions/narrative.js b/lib/actions/narrative.js index 88418392c..ac18279a5 100644 --- a/lib/actions/narrative.js +++ b/lib/actions/narrative.js @@ -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)) From 565e47b8d65163f9e4875c4930ec8e81717feb89 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Tue, 11 Jun 2024 18:21:55 -0400 Subject: [PATCH 2/2] fix(state): Make -1 the default value for ui_activeItinerary. --- lib/actions/ui.js | 5 ++++- lib/components/map/itinerary-summary-overlay.tsx | 3 ++- lib/components/narrative/narrative-itineraries.js | 7 +++---- lib/util/state.js | 5 ++++- lib/util/ui.ts | 12 ++++++++---- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/actions/ui.js b/lib/actions/ui.js index 9591853dc..f7308f4cc 100644 --- a/lib/actions/ui.js +++ b/lib/actions/ui.js @@ -13,6 +13,7 @@ import { getItineraryView, getMapToggleNewItineraryView, getPathFromParts, + isDefined, isDefinedAndNotEqual, ItineraryView } from '../util/ui' @@ -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 diff --git a/lib/components/map/itinerary-summary-overlay.tsx b/lib/components/map/itinerary-summary-overlay.tsx index 632ffd7ee..8baf875ac 100644 --- a/lib/components/map/itinerary-summary-overlay.tsx +++ b/lib/components/map/itinerary-summary-overlay.tsx @@ -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 & { @@ -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 && ( diff --git a/lib/components/narrative/narrative-itineraries.js b/lib/components/narrative/narrative-itineraries.js index 06e5adaf9..7d9faded6 100644 --- a/lib/components/narrative/narrative-itineraries.js +++ b/lib/components/narrative/narrative-itineraries.js @@ -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, @@ -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' @@ -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 diff --git a/lib/util/state.js b/lib/util/state.js index 9ceb2b350..08f1d2baa 100644 --- a/lib/util/state.js +++ b/lib/util/state.js @@ -17,6 +17,7 @@ import { addSortingCosts, collectItinerariesWithoutDuplicates } from './itinerary' +import { isDefined } from './ui' // For lowercase context const LowerCase = styled.span` @@ -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 diff --git a/lib/util/ui.ts b/lib/util/ui.ts index ed992e860..e68f28a0d 100644 --- a/lib/util/ui.ts +++ b/lib/util/ui.ts @@ -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}` } /**