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

Refactor bookATrip function #55852

Merged
merged 29 commits into from
Feb 5, 2025
Merged

Refactor bookATrip function #55852

merged 29 commits into from
Feb 5, 2025

Conversation

cristipaval
Copy link
Contributor

@cristipaval cristipaval commented Jan 28, 2025

This PR refactors the Travel.ts file to not Navigate from the action functions anymore.

Explanation of Change

This PR moves the Travel.bookATrip function to a reusable BookTravelButton component because we want to navigate only from the views and because we also want the code to be DRY.

Fixed Issues

$ #55829
$ #55755
PROPOSAL:

Tests

  1. Click Book Travel from Global Create
  2. On the Travel page, click Book travel
  3. Verify that the button works
Screenshot 2025-01-28 at 13 55 45
  1. Go to the Reports tab and select Trips in the LHN
  2. Verify that the Book Trip button works
Screenshot 2025-01-28 at 13 56 10
  • Verify that no errors appear in the JS console

Offline tests

Same as tests.

QA Steps

// TODO: These must be filled out, or the issue title must include "[No QA]."

Same as tests.

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
    • MacOS: Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I used JaimeGPT to get English > Spanish translation. I then posted it in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.ts or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I verified that similar component doesn't exist in the codebase
  • I verified that all props are defined accurately and each prop has a /** comment above it */
  • I verified that each file is named correctly
  • I verified that each component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
  • I verified that the only data being stored in component state is data necessary for rendering and nothing else
  • In component if we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
  • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
  • I verified that component internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
  • I verified that all JSX used for rendering exists in the render method
  • I verified that each component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
Screen.Recording.2025-02-04.at.11.40.35.mov
MacOS: Chrome / Safari
Screen.Recording.2025-02-04.at.11.19.23.mov
MacOS: Desktop
Screen.Recording.2025-02-04.at.11.22.45.mov

@cristipaval cristipaval self-assigned this Jan 28, 2025
@cristipaval cristipaval requested a review from a team as a code owner January 28, 2025 11:50
@melvin-bot melvin-bot bot requested a review from DylanDylann January 28, 2025 11:50
Copy link

melvin-bot bot commented Jan 28, 2025

@DylanDylann Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot melvin-bot bot removed the request for review from a team January 28, 2025 11:50
@cristipaval cristipaval requested review from ZhenjaHorbach and removed request for DylanDylann January 28, 2025 11:51
if (!activePolicyID) {
return;
}
function bookATrip(policy: Policy, translate: LocaleContextProps['translate'], setCtaErrorMessage: Dispatch<SetStateAction<string>>, ctaErrorMessage = ''): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need full policy object if we use only activePolicyID ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, we check the policy address on it as well as the travelSettings node

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this function will be refactored anyway, as part of this issue

@ZhenjaHorbach
Copy link
Contributor

ZhenjaHorbach commented Jan 28, 2025

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
    • MacOS: Desktop
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.ts or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: Native
android.mov
Android: mWeb Chrome
android-web.mov
iOS: Native
ios.mov
ios.mov
iOS: mWeb Safari
ios-web.mov
Снимок экрана 2025-02-05 в 09 44 56 Снимок экрана 2025-02-05 в 09 45 00 Снимок экрана 2025-02-05 в 09 45 05
MacOS: Chrome / Safari
web.mov
2025-02-05.09.33.51.mov
MacOS: Desktop
desktop.mov

@@ -133,7 +134,13 @@ function EmptySearchView({type, hasResults}: EmptySearchViewProps) {
buttons: [
{
buttonText: translate('search.searchResults.emptyTripResults.buttonText'),
buttonAction: () => bookATrip(translate, setCtaErrorMessage, ctaErrorMessage),
buttonAction: () => {
const activePolicy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`];
Copy link
Contributor

@ZhenjaHorbach ZhenjaHorbach Jan 28, 2025

Choose a reason for hiding this comment

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

Maybe it's better to use getPolicy(activePolicyID, allPolicies) ?
Or usePolicy(activePolicyID) ?

Copy link
Contributor

Choose a reason for hiding this comment

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

But probably
We will refactor this later

Copy link
Contributor Author

@cristipaval cristipaval Jan 28, 2025

Choose a reason for hiding this comment

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

Maybe it's better to use getPolicy(activePolicyID, allPolicies)

getPolicy still could return undefined and we need to check the activePolicy before calling bookATrip. So it won't reduce the lines of code here

Or usePolicy(activePolicyID)

I think it would be redundant since we already have the data in allPolicies

@ZhenjaHorbach
Copy link
Contributor

ZhenjaHorbach commented Jan 28, 2025

Actually I have this issue with new account with new changes
But it doesn't always reproduce

I think we need to get primaryLogin from view as well

Снимок экрана 2025-01-28 в 13 18 50

@cristipaval
Copy link
Contributor Author

cristipaval commented Jan 28, 2025

Actually I have this issue with new account with new changes But it doesn't always reproduce

I think we need to get primaryLogin from view as well

ah yes, same thing, the primary login isn't loaded from Onyx when the function is called.. I'm sure this is reproducible in prod as well

@ZhenjaHorbach
Copy link
Contributor

ZhenjaHorbach commented Jan 28, 2025

Actually I have this issue with new account with new changes But it doesn't always reproduce
I think we need to get primaryLogin from view as well

ah yes, same thing, the primary login isn't loaded from Onyx when the function is called.. I'm sure this is reproducible in prod as well

In that case, I think it's better to fix it in this PR !
And probably to prevent potential issues I think we should refuse Onyx.connect in Travel

@cristipaval
Copy link
Contributor Author

And probably to prevent potential issues I think we should refuse Onyx.connect in Travel

I agree with this!

@cristipaval cristipaval requested a review from tgolen January 28, 2025 23:23
@cristipaval cristipaval changed the title Fix click on the Book Travel button Refactor bookATrip function Jan 28, 2025
@cristipaval cristipaval changed the title Refactor bookATrip function [WIP] Refactor bookATrip function Jan 28, 2025
@cristipaval
Copy link
Contributor Author

Added WIP to this PR and asked @tgolen for an early review from him. If he approves the refactoring approach, I'll spend time on tests and recordings.

headerStyles={[styles.emptyStateCardIllustrationContainer, {backgroundColor: colors.ice800}]}
headerContentStyles={styles.pendingStateCardIllustration}
title={translate('workspace.moreFeatures.companyCards.pendingFeedTitle')}
subtitle={subtitle}
body={body}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the actual change in this file, the others are ESLint fixes.

Copy link
Contributor

@tgolen tgolen left a comment

Choose a reason for hiding this comment

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

I really like this cleanup! Seeing this code for the first time was good as it allowed me to point out some of the areas where comments could really help to explain why things are coded the way they are. That'll really help with future maintenance.

src/components/BookTravelButton.tsx Outdated Show resolved Hide resolved
src/components/BookTravelButton.tsx Show resolved Hide resolved
src/components/BookTravelButton.tsx Outdated Show resolved Hide resolved
src/components/BookTravelButton.tsx Outdated Show resolved Hide resolved
src/components/BookTravelButton.tsx Outdated Show resolved Hide resolved
src/components/BookTravelButton.tsx Show resolved Hide resolved
src/pages/Search/EmptySearchView.tsx Outdated Show resolved Hide resolved
type="error"
/>
)}
<BookTravelButton text={translate('search.searchResults.emptyTripResults.buttonText')} />
Copy link
Contributor

Choose a reason for hiding this comment

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

I would also use children instead of a text prop as well. I think it's a shame that our normal <Button> component doesn't use children.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, BookTravelButton wraps our normal Button so in order to add children, I need to refactor the Button as well, no?

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, just a NAB then.

src/pages/Travel/DomainSelectorPage.tsx Outdated Show resolved Hide resolved
src/pages/Travel/TravelTerms.tsx Show resolved Hide resolved
@OSBotify
Copy link
Contributor

A preview of your ExpensifyHelp changes have been deployed to https://750e57b6.helpdot.pages.dev ⚡️

@ZhenjaHorbach
Copy link
Contributor

ZhenjaHorbach commented Jan 31, 2025

@cristipaval
We have a lot of non-PR related changes
I think we need to re-merge

Looks like it's related with this

src/components/BookTravelButton.tsx Outdated Show resolved Hide resolved
return;
}

// An address is required when the Spotnana entity is created for the policy
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is an address required? (answer that in the comment please)

const isPolicyProvisioned = policy?.travelSettings?.spotnanaCompanyID ?? policy?.travelSettings?.associatedTravelDomainAccountID;
if (policy?.travelSettings?.hasAcceptedTerms ?? (travelSettings?.hasAcceptedTerms && isPolicyProvisioned)) {
openTravelDotLink(policy?.id)
?.then(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

NAB: (and we talked in person about this) but I think it would be better if openTravelDotLink() always returned a promise, no matter what, then this optional chaining isn't necessary. That would be easy to implement by doing something like policy?.id && openTravelDotLink()... and removing the early return inside of openTavelDotLink()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll open a separate PR for this one because changing this will require me to update the calls in other files, requiring me to fix ESLint checks, and I wouldn't extend the scope of this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

In some cases we can ignore eslint issues related to imports due to the large volume of a PR
But new PR is also good !

src/components/EmptyStateComponent/index.tsx Outdated Show resolved Hide resolved
type="error"
/>
)}
<BookTravelButton text={translate('search.searchResults.emptyTripResults.buttonText')} />
Copy link
Contributor

Choose a reason for hiding this comment

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

OK, just a NAB then.

>
<Text>
{translate('workspace.moreFeatures.companyCards.pendingFeedDescription')}
<TextLink onPress={() => navigateToConciergeChat()}> {CONST?.CONCIERGE_CHAT_NAME}</TextLink>.
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

@cristipaval cristipaval changed the title [WIP] Refactor bookATrip function Refactor bookATrip function Feb 4, 2025
@cristipaval cristipaval requested a review from tgolen February 4, 2025 19:18
@cristipaval
Copy link
Contributor Author

Ready once again, @ZhenjaHorbach , @tgolen

success
large
/>
{!!footer && footer}
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually !!footer is also redundant here

@@ -1,4 +1,5 @@
import React from 'react';
import type {ReactNode} from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

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

Extra import
We can use ReactNode from React

src/components/BookTravelButton.tsx Outdated Show resolved Hide resolved
@ZhenjaHorbach
Copy link
Contributor

And for me changes looks good !
I will recomplete checklist today or tomorrow

Copy link
Contributor

@tgolen tgolen left a comment

Choose a reason for hiding this comment

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

APPROVED

@ZhenjaHorbach
Copy link
Contributor

LGTM !

@melvin-bot melvin-bot bot requested a review from MarioExpensify February 5, 2025 08:47
Copy link

melvin-bot bot commented Feb 5, 2025

@MarioExpensify Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

Copy link
Contributor

@MarioExpensify MarioExpensify left a comment

Choose a reason for hiding this comment

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

Code looks good! Is there any reason we're holding this or it can be merged already?

@cristipaval
Copy link
Contributor Author

Go ahead and merge it, @MarioExpensify

@MarioExpensify
Copy link
Contributor

Nice, good work everybody!

@MarioExpensify MarioExpensify merged commit c137167 into main Feb 5, 2025
20 checks passed
@MarioExpensify MarioExpensify deleted the cristi_fix-bookATrip-action branch February 5, 2025 15:02
@OSBotify
Copy link
Contributor

OSBotify commented Feb 5, 2025

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

Copy link
Contributor

github-actions bot commented Feb 6, 2025

🚀 Deployed to staging by https://github.com/MarioExpensify in version: 9.0.95-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅
🤖🔄 android HybridApp 🤖🔄 success ✅
🍎🔄 iOS HybridApp 🍎🔄 success ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants