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

Convert SearchPage to functional component #23076

Merged
merged 11 commits into from
Sep 14, 2023

Conversation

Piotrfj
Copy link
Contributor

@Piotrfj Piotrfj commented Jul 18, 2023

Details

Fixed Issues

$ #16251
PROPOSAL: proposal not yet created

Tests

  1. Click on search icon in chat list to enter search page.
  2. Verify if the results are the same as on production version of the application.
  • Verify that no errors appear in the JS console

Offline tests

  1. Click on search icon in chat list to enter search page.
  2. Verify if the results are the same as on production version of the application.

QA Steps

  1. Click on search icon in chat list to enter search page.
  2. Verify if the results are the same as on production version of the application.
  • 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 / Chrome
    • iOS / native
    • iOS / 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 the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • 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 verified the translation was requested/reviewed 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 approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • 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.js 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 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
    • 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)
    • 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(themeColors.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 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.
  • 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 author checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
Kapture.2023-08-23.at.14.57.44.mp4
Mobile Web - Chrome
Mobile Web - Safari
Desktop
Kapture.2023-08-23.at.14.57.44.mp4
iOS
Kapture.2023-08-24.at.13.12.01.mp4
Android
Kapture.2023-08-23.at.15.14.34.mp4

@github-actions
Copy link
Contributor

github-actions bot commented Jul 18, 2023

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@Piotrfj
Copy link
Contributor Author

Piotrfj commented Jul 18, 2023

I have read the CLA Document and I hereby sign the CLA

src/pages/SearchPage.js Outdated Show resolved Hide resolved
userToInvite,
};
}
const debouncedUpdateOptions =_.debounce(updateOptions, 75);
Copy link
Contributor

@gedu gedu Jul 19, 2023

Choose a reason for hiding this comment

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

_.debounce returns always a new function, probably would be good to wrap it into an useCallback for the useEffect, so you can add it into the dependency array safely

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, the app should have the same reference across the re-renders. useCallback is necessary here.

this.setState({searchValue}, this.debouncedUpdateOptions);
}
useEffect(() => {
updateOptions();
Copy link
Contributor

@gedu gedu Jul 19, 2023

Choose a reason for hiding this comment

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

if updateOptions isn't part of the dependency array, please add a comment explaining why

Copy link
Contributor

@rezkiy37 rezkiy37 Jul 19, 2023

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I just realized that updateOptions useCallback, also is recreated when the searchValue change, I think could be some useEffects and function duplications, would be good to update the logic, and merge where is possible

personalDetails,
});
useEffect(() => {
debouncedUpdateOptions()
Copy link
Contributor

Choose a reason for hiding this comment

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

why are you updating the options after every new word? You are listening to reports and personalDetails to update the options

}

SearchPage.propTypes = propTypes;
SearchPage.defaultProps = defaultProps;

SearchPage.displayName = 'SearchPage';
Copy link
Contributor

Choose a reason for hiding this comment

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

use hooks useLocalize and useWindowDimensions

}
useEffect(() => {
updateOptions();
},[props.reports, props.personalDetails])
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this useEffect should have only this function as a dependency. It is logically. Finally, it uses the same deps.

Suggested change
},[props.reports, props.personalDetails])
},[updateOptions])

recentReports,
personalDetails,
});
useEffect(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please group all useEffects in one place instead of having them in random places across the component.

src/pages/SearchPage.js Outdated Show resolved Hide resolved
class SearchPage extends Component {
constructor(props) {
super(props);
function SearchPage(props) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's use prop destructuring now in order to be aligned with the style guidelines -> https://github.com/Expensify/App/blob/main/contributingGuides/STYLE.md#destructuring

Copy link
Contributor

@fabioh8010 fabioh8010 Jul 24, 2023

Choose a reason for hiding this comment

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

Also, you have to remove usage of defaultProps and assign default values during the prop destructuring.

You don't need to do this one now, we should still use defaultProps in JS files.

Copy link
Contributor

@fabioh8010 fabioh8010 left a comment

Choose a reason for hiding this comment

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

Just one nitpick, LGTM!

reports: {},
};
function SearchPage({betas = [], personalDetails = {}, reports= {}}) {
//Data for initialization (runs only on the first render)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
//Data for initialization (runs only on the first render)
// Data for initialization (runs only on the first render)

personalDetails: {},
reports: {},
};
function SearchPage({betas = [], personalDetails = {}, reports= {}}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
function SearchPage({betas = [], personalDetails = {}, reports= {}}) {
function SearchPage({betas = [], personalDetails = {}, reports = {}}) {

Super nitpick sorry XD

@thiagobrez
Copy link
Contributor

@Piotrfj The whole searching flow looks more complex than it needs to be, in my opinion. I know it was already like this before, but I propose the following to make it simpler and prevent a few re-renders:

  • Merge activeRecentReports, activePersonalDetails and activeUserToInvite into one state object: searchOptions, so you don't have to set 3 states (and therefore 3 re-renders), but only 1
  • Currently, the onChangeText function is only setting state for searchValue. Then, when searchValue is changed, in the useEffect you're calling debouncedUpdateOptions which calls updateOptions. My suggestion here is to move the updateOptions logic inside onChangeText, so you can delete updateOptions, debouncedUpdateOptions and consequently the useEffect

This is how it would look like in the end:

function SearchPage({betas = [], personalDetails = {}, reports= {}}) {

...

    const [searchValue, setSearchValue] = useState('')
    
    // Only 1 state for the 3 objects here
    const [searchOptions, setSearchOptions] = useState({
      activeRecentReports: initialRecentReports,
      activePersonalDetails: initialPersonalDetails,
      activeUserToInvite: initialUserToInvite
    });

    const {translate} = useLocalize();

    // delete updateOptions

    // delete debouncedUpdateOptions
  
    ...
    
    // delete the debouncedUpdateOptions useEffect

    ...
    
    const onChangeText = (value = '') => {
        setSearchValue(value);
        
        // `updateOptions` logic is here, with the debounce
        _.debounce(() => {
            const {recentReports: localRecentReports, personalDetails: localPersonalDetails, userToInvite: localUserToInvite} = OptionsListUtils.getSearchOptions(
                reports,
                personalDetails,
                searchValue.trim(),
                betas,
            );

            // only 1 state change here prevents rerenders
            setSearchOptions({
                activeUserToInvite: localUserToInvite,
                activeRecentReports: localRecentReports,
                activePersonalDetails: localPersonalDetails,
            });
        }, 75);
    }

}

This way when you look at onChangeText, you know exactly what's gonna happen all in one place, and also prevents re-renders from setting multiple states, and calling unnecessary effects

}
useEffect(() => {
debouncedUpdateOptions()
}, [searchValue, debouncedUpdateOptions])
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, searchValue not necessary here.
useEffect has debouncedUpdateOptions has updateOptions has searchValue. BTW, if you want to simplify this chain, you can pass searchValue as an argument to updateOptions.

Suggested change
}, [searchValue, debouncedUpdateOptions])
}, [debouncedUpdateOptions])

};
function SearchPage({betas = [], personalDetails = {}, reports = {}}) {
// Data for initialization (runs only on the first render)
const {recentReports: initialRecentReports, personalDetails: initialPersonalDetails, userToInvite: initialUserToInvite} = useMemo(() => OptionsListUtils.getSearchOptions(reports, personalDetails, '', betas), []);
Copy link
Contributor

Choose a reason for hiding this comment

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

Linter will complain on this, so just disable this rule

Suggested change
const {recentReports: initialRecentReports, personalDetails: initialPersonalDetails, userToInvite: initialUserToInvite} = useMemo(() => OptionsListUtils.getSearchOptions(reports, personalDetails, '', betas), []);
// eslint-disable-next-line react-hooks/exhaustive-deps
const {recentReports: initialRecentReports, personalDetails: initialPersonalDetails, userToInvite: initialUserToInvite} = useMemo(() => OptionsListUtils.getSearchOptions(reports, personalDetails, '', betas), []);

@Piotrfj Piotrfj marked this pull request as ready for review July 27, 2023 08:47
@Piotrfj Piotrfj requested a review from a team as a code owner July 27, 2023 08:47
@melvin-bot melvin-bot bot removed the request for review from a team July 27, 2023 08:47
@melvin-bot
Copy link

melvin-bot bot commented Jul 27, 2023

@thesahindia 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]

@marcaaron
Copy link
Contributor

@thesahindia let's get this tested and reviewed 🙏 🙇

@allroundexperts
Copy link
Contributor

@allroundexperts lint issues are fixed, PR is ready

@Piotrfj Can you also please resolve the outstanding comments on your PR?

@Piotrfj
Copy link
Contributor Author

Piotrfj commented Aug 21, 2023

@marcaaron @allroundexperts please accept a pr

@allroundexperts
Copy link
Contributor

@Piotrfj I'm on it today!

@allroundexperts
Copy link
Contributor

@Piotrfj I still see some un-resolved comments.

Screenshot 2023-08-21 at 3 16 29 PM

Can you please resolve these?

@fabioh8010
Copy link
Contributor

@allroundexperts @Piotrfj You can ignore this comment, is not valid:

Also, you have to remove usage of defaultProps and assign default values during the prop destructuring.

But @Piotrfj let's do this one:

Let's use prop destructuring now in order to be aligned with the style guidelines -> https://github.com/Expensify/App/blob/main/contributingGuides/STYLE.md#destructuring

@allroundexperts
Copy link
Contributor

@Piotrfj Can you please upload screen recordings for all the platforms?

@Piotrfj
Copy link
Contributor Author

Piotrfj commented Aug 22, 2023

@allroundexperts sure, gonna provide them today

@allroundexperts
Copy link
Contributor

Bump @Piotrfj

@Piotrfj
Copy link
Contributor Author

Piotrfj commented Aug 31, 2023

@allroundexperts examples provided, sorry for the delay, I was actually providing them earlier but forgot to save them...

@marcaaron marcaaron changed the title Convert SearchPage to functional component [HOLD merge freeze] Convert SearchPage to functional component Sep 6, 2023
Copy link
Contributor

@marcaaron marcaaron left a comment

Choose a reason for hiding this comment

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

Nice changes overall. We are on a merge freeze I think for the next couple of days. So, we will wait a bit before merging. If it goes for too long please tag me back in here!

@marcaaron
Copy link
Contributor

The checklist also looks like it went out of date somewhere if we can fix that in the meantime @allroundexperts

@marcaaron marcaaron changed the title [HOLD merge freeze] Convert SearchPage to functional component Convert SearchPage to functional component Sep 14, 2023
@marcaaron
Copy link
Contributor

Merge freeze is over so taking this off hold. @allroundexperts can you check my comment above?

@allroundexperts
Copy link
Contributor

Merge freeze is over so taking this off hold. @allroundexperts can you check my comment above?

Hi @marcaaron!
I'd be updating it along with testing it again.

Copy link
Contributor

@allroundexperts allroundexperts left a comment

Choose a reason for hiding this comment

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

Still testing well!

@melvin-bot melvin-bot bot requested a review from marcaaron September 14, 2023 01:03
@allroundexperts
Copy link
Contributor

This is ready @marcaaron!

Copy link
Contributor

@marcaaron marcaaron left a comment

Choose a reason for hiding this comment

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

LGTM. Let's 🪨 🤘

@marcaaron marcaaron self-requested a review September 14, 2023 01:57
@marcaaron marcaaron merged commit e18ec42 into Expensify:main Sep 14, 2023
16 of 17 checks passed
@OSBotify
Copy link
Contributor

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

@OSBotify
Copy link
Contributor

🚀 Deployed to staging by https://github.com/marcaaron in version: 1.3.70-0 🚀

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

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/thienlnam in version: 1.3.70-8 🚀

platform result
🤖 android 🤖 failure ❌
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 failure ❌
🕸 web 🕸 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.

9 participants