Skip to content

Conversation

@binh-dam-ibigroup
Copy link
Collaborator

This PR shows the 'Save trip' button in the NarrativeItineraries if the selected itinerary has a transit leg and no rental legs, hides the button otherwise. The PR also removes the 'Save this option' trip button from LineItinerary.

Copy link
Contributor

@evansiroky evansiroky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment.

Comment on lines 19 to 59
// TODO: move to OTP-UI + add tests?
/**
* @returns true if at least one of the legs of the specified itinerary is a transit leg.
*/
export function itineraryHasTransit (itinerary) {
if (itinerary && itinerary.legs) {
for (const leg of itinerary.legs) {
if (coreUtils.itinerary.isTransit(leg.mode)) {
return true
}
}
}

return false
}

// TODO: move to OTP-UI + add tests?
/**
* @returns true if at least one of the legs of the specified itinerary is a rental leg
* (e.g. CAR_RENT, BICYCLE_RENT, MICROMOBILITY_RENT).
*/
export function itineraryHasRental (itinerary) {
if (itinerary && itinerary.legs) {
for (const leg of itinerary.legs) {
if (leg.mode.indexOf('_RENT') > -1) {
return true
}
}
}

return false
}

/**
* Determines whether an itinerary can be monitored.
* @returns true if at least one of the legs of the specified itinerary is
* a transit leg, and none of the legs is a rental leg (e.g. CAR_RENT, BICYCLE_RENT, etc.).
*/
export function itineraryCanBeMonitored (itinerary) {
return itineraryHasTransit(itinerary) && !itineraryHasRental(itinerary)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 3 functions could probably be combined into one so that a single loop occurs which checks each leg as needed. Furthermore, the coreUtils.itinerary.hasRental could be used. Also, this must check if a ridehail leg exists (coreUtils.itinerary.hasHail can be used). Also, I noticed that lib/util/state.js has a duplicate function of itineraryHasTransit. This redundancy should be avoided. Regarding testing, I think making tests for the itineraryCanBeMonitored would be good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I totally missed on the duplicate in state.js but that function is no longer used for the purpose of this PR. The changes are in df6bf8d, and the tests in aa19444.

{/* FIXME: only save if meets requirements (e.g., is transit + non-realtime mode) */}
{persistence && persistence.enabled
{/* Only save if itinerary has transit and no rental modes that cannot be guaranteed. */}
{persistence && persistence.enabled && itineraryCanBeMonitored(itineraries[activeItinerary])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I think we should maybe take this opportunity to encapsulate this button into its own component. There are a few states that could be represented here, that we haven't quite addressed yet:

  1. User is not logged in - in this instance, I think we should have the SaveTripButton render something like: "Please sign in to save trip"
  2. Persistence disabled - just return null
  3. itin cannot be monitored - we should probably disable the button and for now show a tooltip saying: "To save a trip it must contain a transit leg and no rental vehicles legs." Not sure if that's a great message long-term though...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I agree it is probably the simpler solution for now.
  2. Agree
  3. How about for the disabled button text: 🚫 Cannot save? And for the tooltip, taken from the backend: "Only trips that include transit and no rentals or ride hailing can be saved/monitored."

Copy link
Member

@landonreed landonreed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save trip button probably warrants a component at this stage. Also, what Evan said.

@landonreed landonreed removed their assignment Nov 2, 2020

testCases.forEach(({ expected, itinerary, title }) => {
it(title, () => {
expect(itineraryCanBeMonitored(itinerary))[expected ? 'toBeTruthy' : 'toBeFalsy']()
Copy link
Contributor

@evansiroky evansiroky Nov 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since itineraryCanBeMonitored always returns exactly true or false, the readability of this assertion could be improved to be expect(...).toBe(expected).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, it was otherwise copy/paste from another test... Updated in. 4ccc522.

Comment on lines 85 to 87
const mapDispatchToProps = (dispatch, ownProps) => {}

export default connect(mapStateToProps, mapDispatchToProps)(SaveTripButton)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mapDispatchToProps is not a required argument, so this can be replaced with connect(mapStateToProps)(SaveTripButton)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 4ccc522.

Copy link
Contributor

@evansiroky evansiroky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 2 comments.

@evansiroky evansiroky removed their assignment Nov 5, 2020
const { persistence } = state.otp.config
const itineraries = getActiveItineraries(state.otp)
return {
itinerary: activeSearch && itineraries && itineraries[activeSearch.activeItinerary],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opportunity to use getActiveItinerary(state.otp) which handles all of this other getActiveSearch/getActiveItineraries logic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Updated in 80b8a91.

{user &&
<span className='pull-right'><LinkButton to='/savetrip'>Save this option</LinkButton></span>
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I know this goes against what I previously stated, but we should keep the new SaveTrip button component here <SaveTripButton />. This was decided in conversation with Ritesh earlier last week.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reinstated in 80b8a91.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dec353a uses <SaveTripButton /> in LineItinerary.

Copy link
Member

@landonreed landonreed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Conditional approval on a few final items.

@binh-dam-ibigroup binh-dam-ibigroup merged commit 761e817 into dev Nov 13, 2020
@binh-dam-ibigroup binh-dam-ibigroup deleted the only-save-itins-with-transit branch November 13, 2020 13:59
@evansiroky evansiroky mentioned this pull request Nov 20, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Jan 8, 2021

🎉 This PR is included in version 2.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants