From 0ea80dd5988e6488a93e868eac16407692af4f0a Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Mon, 5 Aug 2024 12:43:42 -0400 Subject: [PATCH] fix(actions/user): Correctly parse trip request data with GraphQL parameters --- lib/actions/user.js | 60 ++++++++++++++++------- lib/components/form/user-settings-i18n.js | 10 +++- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/lib/actions/user.js b/lib/actions/user.js index 9ec2d9965..149d7cf9a 100644 --- a/lib/actions/user.js +++ b/lib/actions/user.js @@ -156,35 +156,57 @@ export function fetchAuth0Token(auth0, intl) { */ function convertRequestToSearch(config) { return function (tripRequest) { - const { dateCreated, id, requestParameters = {} } = tripRequest + const { + dateCreated, + id, + otp2QueryParams = {}, + requestParameters = {} // Deprecated OTP1 + } = tripRequest const { host, path } = config.api return { canDelete: false, id, - query: planParamsToQuery(requestParameters || {}), + query: planParamsToQuery(otp2QueryParams || requestParameters || {}), timestamp: dateCreated, url: `${host}${path}/plan?${qs.stringify(requestParameters)}` } } } +// TODO move to a utils file +function areRequestsSame(qp1, qp2) { + // Everything except modes. + // Exclude banned/preferred/unpreferred at this time. + return ( + qp1.bikeReluctance === qp2.bikeReluctance && + qp1.carReluctance === qp2.carReluctance && + qp1.date === qp2.date && + qp1.departArrive === qp2.departArrive && + qp1.from?.name === qp2.from?.name && + qp1.departArrive === qp2.departArrive && + qp1.time === qp2.time && + qp1.to?.name === qp2.to?.name && + qp1.walkReluctance === qp2.walkReluctance && + qp1.walkSpeed === qp2.walkSpeed && + qp1.wheelchair === qp2.wheelchair + ) +} + /** - * Removes duplicate requests saved from batch queries, - * so that only one request is displayed per batch. - * (Except for the mode, all query params in the same batch are the same.) + * Removes duplicate requests so that only one request is displayed per "batch". */ -function removeDuplicateRequestsFromBatch() { - const batches = {} - return function ({ query }) { - if (!batches[query.batchId]) { - batches[query.batchId] = true - - // Remove the mode for display purposes - query.mode = '' - return true - } - return false +function removeDuplicateRequests(filtered, tripRequest) { + // Compare one trip request to the next one. + if (filtered.length === 0) { + filtered.push(tripRequest) + } else if ( + !areRequestsSame(filtered[filtered.length - 1].query, tripRequest.query) + ) { + filtered.push(tripRequest) + } else { + filtered[filtered.length - 1].query.modes.push(...tripRequest.query.modes) } + return filtered } /** @@ -221,8 +243,10 @@ export function fetchTripRequests() { const { config } = getState().otp const convertedTrips = trips.data .map(convertRequestToSearch(config)) - .filter((tripReq) => !isEmpty(tripReq.query)) - .filter(removeDuplicateRequestsFromBatch()) + .filter( + (tripReq) => !isEmpty(tripReq.query) || !!tripReq.otp2QueryParams + ) + .reduce(removeDuplicateRequests, []) dispatch(setCurrentUserTripRequests(convertedTrips)) } diff --git a/lib/components/form/user-settings-i18n.js b/lib/components/form/user-settings-i18n.js index c1a639a00..246baea68 100644 --- a/lib/components/form/user-settings-i18n.js +++ b/lib/components/form/user-settings-i18n.js @@ -50,15 +50,21 @@ export function summarizeQuery(query, intl, locations = []) { ) } + const modes = + query.modes + ?.map( + ({ mode, qualifier }) => `${mode}${qualifier ? `_${qualifier}` : ''}` + ) + .join(',') || '' const from = findLocationType(intl, query.from, locations) || stripAllButNameOfAddress(query.from.name) const to = findLocationType(intl, query.to, locations) || stripAllButNameOfAddress(query.to.name) - const mode = hasTransit(query.mode) + const mode = hasTransit(modes) ? intl.formatMessage({ id: 'common.modes.transit' }) - : toSentenceCase(query.mode) + : toSentenceCase(modes) return intl.formatMessage( { id: 'components.UserSettings.recentSearchSummary' }, { from, mode, to }