Skip to content

Commit

Permalink
fix: review fixes, refactored into single exported function
Browse files Browse the repository at this point in the history
  • Loading branch information
partisaani committed Nov 13, 2024
1 parent 1141026 commit b8b5f1c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 39 deletions.
3 changes: 2 additions & 1 deletion app/component/itinerary/ItineraryPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,8 @@ export default function ItineraryPage(props, context) {
updateLocalStorage(true);
addFeedbackly(context);

if (isStoredItineraryRelevant(match)) {
const storedItinerary = getLatestNavigatorItinerary();
if (isStoredItineraryRelevant(storedItinerary, match)) {
setNavigation(true);
} else {
clearLatestNavigatorItinerary();
Expand Down
50 changes: 13 additions & 37 deletions app/component/itinerary/ItineraryPageUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
stopRealTimeClient,
} from '../../action/realTimeClientAction';
import { PlannerMessageType } from '../../constants';
import { getLatestNavigatorItinerary } from '../../store/localStorage';
import { addAnalyticsEvent } from '../../util/analyticsUtils';
import { boundWithMinimumArea } from '../../util/geo-utils';
import { compressLegs, getTotalBikingDistance } from '../../util/legUtils';
Expand Down Expand Up @@ -537,51 +536,28 @@ export function quitIteration(plan, newPlan, planParams, startTime) {
return false;
}

/**
* Checks if itinerary in local storage was set with identical URL parameters.
*
* Note: storedParams.hash is not compared
*
* @param {{from: string, to: string, time: number, arriveBy: boolean}} storedParams
* @param {matchShape} match matchContext with URL params
* @returns true if stored itinerary was set with identical parameters
*/
const storedParamsMatchURL = (storedParams, match) => {
if (!storedParams || !match) {
return false;
}

return (
storedParams.from === match.params?.from &&
storedParams.to === match.params?.to &&
storedParams.time === match.location?.query?.time &&
storedParams.arriveBy === match.location?.query?.arriveBy
);
};

/**
* Enables Navigator assisted journey on itinerary selection if all of the following resolve as true:
* - a stored itinerary exists
* - the stored itinerary ends in future
* - the params stored along itinerary match those of the current query
*
* Note: Itinerary related hash param is also compared
* - the params stored along itinerary are identical to current URL parameters
*
* @param {{itinerary: itineraryShape, params: {from: string, to: string, time: number, arriveBy: boolean, index: string}}} itinerary matchContext with URL params
* @param {matchShape} match matchContext with URL params
* @returns true if Navigator can be initialized with stored itinerary
*/
export const isStoredItineraryRelevant = match => {
/* eslint-disable eqeqeq */
const { itinerary, params } = getLatestNavigatorItinerary();
const {
params: { hash, secondHash },
} = match;
export const isStoredItineraryRelevant = ({ itinerary, params }, match) => {
if (!itinerary || !params) {
return false;
}

return (
(itinerary &&
Date.parse(itinerary.end) > Date.now() &&
storedParamsMatchURL(params, match) &&
params?.index == secondHash) ||
params?.index == hash
Date.parse(itinerary.end) > Date.now() &&
params.from === match.params.from &&
params.to === match.params.to &&
params.time === match.location?.query?.time &&
params.arriveBy === match.location?.query?.arriveBy &&
params.hash === match.params.hash &&
params.secondHash === match.params.secondHash
);
};
3 changes: 2 additions & 1 deletion app/component/itinerary/StartNavi.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const StartNavi = ({ setNavigation, itinerary }, context) => {
to: match.params.to,
arriveBy: match.location.query.arriveBy,
time: match.location.query.time,
index: match.params.secondHash ?? match.params.hash,
hash: match.params.hash,
secondHash: match.params.secondHash,
},
});
};
Expand Down

0 comments on commit b8b5f1c

Please sign in to comment.