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 NewContactMethodPage to use hooks #17472

Merged
merged 21 commits into from
Apr 25, 2023

Conversation

bondydaa
Copy link
Contributor

Details

Refactoring the NewContactMethodPage component to a functional component and to use hooks.

Fixed Issues

$ #16291

Tests And QA Steps

  1. Sign in and go to Profile -> Contact Method
  2. Add new contact method which isn't claimed by another user
  3. Verify that you are redirected to Contact Methods and the newly added contact method is in the list
  4. Validate it and verify that the green indicator goes away
  5. Add a new contact method which is added already to another account
  6. Verify that you are redirected to the contact methods list and the newly added one has a red indicator with an error
  7. Verify that you can successfully remove the contact methods added in the steps above
  • Verify that no errors appear in the JS console

Offline tests

  1. Go to Contact Methods on deviceA
  2. Sign in with the same account and go to contact methods on deviceB
  3. Turn off the internet on deviceA
  4. Add a new contact method
  5. Verify that you are redirected back to Contact Methods and the newly added contact method is in a pending action
  6. Turn on the internet on deviceA
  7. Verify that the newly added contact method is not greyed out anymore and you can click and validate it
  8. Verify on deviceB that the new contact method is listed in Contact Methods page
  • 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 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
    • 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
16291_web.mp4
Mobile Web - Chrome
Mobile Web - Safari
Desktop
2023-04-14_17-10-11.mp4
iOS
Android

@bondydaa bondydaa self-assigned this Apr 14, 2023
@bondydaa bondydaa requested a review from a team as a code owner April 14, 2023 23:23
@melvin-bot melvin-bot bot requested review from Beamanator and eVoloshchak and removed request for a team April 14, 2023 23:24
@MelvinBot
Copy link

@eVoloshchak @Beamanator One of you needs to 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]

@bondydaa
Copy link
Contributor Author

I couldn't get ios or android to build locally, probably b/c i was doing old dot dev recently so if the c+ could get those screenshots for me that'd be awesome. otherwise I'll take a look next week.

Another thing is that I am seeing this console error
Screenshot 2023-04-14 at 4 48 34 PM

Which I tried to fix with this: 64c0f0f

but that led to a bunch of other errors in the app, like on the login form so I think we might need to ignore it until we refactor that in a single go. But let me know if there's something else I should try/do.

@eVoloshchak
Copy link
Contributor

Attaching the screen recordings for iOS and Android

iOS
Screen.Recording.2023-04-17.at.11.44.57.mov
Android
Screen_Recording_20230417-114306_New.Expensify.mp4

As for the Proptypes error with innerRef, we could change this line to

    innerRef: PropTypes.oneOfType([
        PropTypes.func,
        PropTypes.shape({current: PropTypes.instanceOf(Element)}),
    ]),


submitForm() {
const submitForm = useCallback(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need a useCallback here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It probably isn't strictly required here per the react docs https://react.dev/reference/react/useCallback#usage it suggests you probably only need it for a performance optimization.

But I chatted with @marcaaron a bit 1:1 about it, sounds like there was a discussion somewhere (not sure where probably open-source but I haven't looked for it yet) where we landed on preferring to just always use useCallback

the resolution was that we should just always use it since the performance downsides of missing when we should use it vs not can be pretty bad.
⁦Or said another way - we'll probably mess stuff up more by not using it then we will by using it

Copy link
Contributor

Choose a reason for hiding this comment

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

That makes sense, thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm interesting thread that seems to be saying the opposite of what we decided here https://expensify.slack.com/archives/C01GTK53T8Q/p1680096510744619

just wanted to share so others didn't keep thinking we should prefer useCallback by default.

bondydaa and others added 2 commits April 17, 2023 09:23
Co-authored-by: Eugene Voloshchak <copyreading@gmail.com>
Co-authored-by: Eugene Voloshchak <copyreading@gmail.com>
@eVoloshchak
Copy link
Contributor

eVoloshchak commented Apr 17, 2023

@bondydaa , is this ready for testing? What are your thoughts on this?

marcaaron
marcaaron previously approved these changes Apr 17, 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.

Looks super great - nailed pretty much everything 👍👍

validateForm() {
const login = this.state.login.trim();
const phoneLogin = LoginUtils.getPhoneNumberWithoutSpecialChars(login);
const validateForm = useCallback(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

NAB, since we are calling this validateForm() method in the render... I'd maybe update this to be a useMemo() and return the value instead of a function...

const isFormValid = useMemo(() => {...}, dependencies);

It's kind of minor, but I think slightly better in that we don't need to call the function on render and can just use the value directly instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah i was really confused looking at this and was thinking it'd be better if it was a single value not a function but then didn't want to accidentally break something. can update though.

@bondydaa
Copy link
Contributor Author

bondydaa commented Apr 17, 2023

I def don't know enough to feel comfortable signing off on this change #17472 (comment)

@marcaaron what are your thoughts? Would that allow us to use both the current code that exists everywhere and then use the useRef hook here? then once we've converted everything we can change it to only be compatible with useRef?

@marcaaron
Copy link
Contributor

Would that allow us to use both the current code that exists everywhere and then use the useRef hook here? then once we've converted everything we can change it to only be compatible with useRef?

I think the suggestion is fine. Ultimately, it's not guaranteed that all refs would have the {current: any} type. Some could still be callbacks (function) as callback refs are still a thing (just maybe not as common as we use them today).

label={`${props.translate('common.email')}/${props.translate('common.phoneNumber')}`}
ref={loginInputRef}
value={login}
onChangeText={value => setLogin(value)}
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
onChangeText={value => setLogin(value)}
onChangeText={setLogin}

Instead of defining a new arrow function, I believe we can simply pass setLogin here. This would result in a minor performance boost, as we would not need to recreate the function each time the component is updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh cool thanks, makes sense. I don't think I realized the value would be passed by default to the function here.

<TextInput
label={props.translate('common.password')}
value={password}
onChangeText={value => setPassword(value)}
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
onChangeText={value => setPassword(value)}
onChangeText={setPassword}

Same here.

eVoloshchak
eVoloshchak previously approved these changes Apr 21, 2023
Copy link
Contributor

@eVoloshchak eVoloshchak left a comment

Choose a reason for hiding this comment

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

Tests well

@bondydaa
Copy link
Contributor Author

@marcaaron @Beamanator @cristipaval can you give it another look please before more conflicts pop up 🙏

marcaaron
marcaaron previously approved these changes Apr 24, 2023
Beamanator
Beamanator previously approved these changes Apr 25, 2023
Copy link
Contributor

@Beamanator Beamanator left a comment

Choose a reason for hiding this comment

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

Looks great! 1 tiny question, approving since it's very small

@@ -47,7 +47,10 @@ const propTypes = {
hideFocusedState: PropTypes.bool,

/** Forward the inner ref */
innerRef: PropTypes.func,
innerRef: PropTypes.oneOfType([
Copy link
Contributor

Choose a reason for hiding this comment

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

Has this been resolved?

class NewContactMethodPage extends Component {
constructor(props) {
super(props);
function NewContactMethodPage(props) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suuuper just wondering, why aren't we doing this? I see some components doing this & some doing what you have, I feel like it would be nice to standardize but maybe it doesn't matter for now

Suggested change
function NewContactMethodPage(props) {
const NewContactMethodPage = (props) => {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The difference between these 2 things is that defining it as a function means that it will be hoisted into the global scope. Defining it as a variable prevents this hoisting. See for the nitty gritty on why is may or may not matter https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch5.md#hoisting-declaration-vs-expression

But I agree, right now we are fine with either and we should probably standardize on one or the other. I don't have a strong preference for either format.

@bondydaa
Copy link
Contributor Author

:marshmellow-duet: conflicts again, fixing them.

Comment on lines 62 to 68
const handleLoginChange = useCallback((value) => {
setLogin(value.trim());
}, [login]);

const handlePasswordChange = useCallback((value) => {
setPassword(value.trim());
}, [password]);
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 did this to fix the merge conflict caused by #17817 and think it's a better solution so that the values in state never have leading or trailing whitespace.

It does add one small UX quirk that I'm not sure if we care about: https://user-images.githubusercontent.com/4073354/234353895-d83bf242-44b4-48fc-8032-f85a0323c50b.mp4

basically if you go back to the start of the text and hit a space b/c we .trim the value the cursor jumps to the end of the text.

Personally I don't really think this is a problem and probably only something QA will do while testing - real users most likely are not attempting this and will require a bit more code to prevent it but open to other opinions.

@bondydaa
Copy link
Contributor Author

updated

Comment on lines 62 to 63
const handleLoginChange = value => setLogin(value.trim());
const handlePasswordChange = value => setPassword(value.trim());
Copy link
Contributor

Choose a reason for hiding this comment

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

@bondydaa We can use useCallback and we pass empty dependency array

const handlePasswordChange = useCallback((value) => {
    setPassword(value.trim());
}, []);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah that is probably what I was missing! thanks for the tip.

@bondydaa
Copy link
Contributor Author

(dj khaled voice) and another 1.

@marcaaron marcaaron merged commit b047ec7 into main Apr 25, 2023
@marcaaron marcaaron deleted the bondy-login-component-migration branch April 25, 2023 21:12
@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.6-0 🚀

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

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/jasperhuangg in version: 1.3.6-0 🚀

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

8 participants