From 1ea1f7a0863ddd3efed8d69abb979f462f82d500 Mon Sep 17 00:00:00 2001 From: David Bondy Date: Fri, 14 Apr 2023 16:50:50 -0600 Subject: [PATCH 01/16] import hook methods and create new state/ref variables --- .../Profile/Contacts/NewContactMethodPage.js | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/pages/settings/Profile/Contacts/NewContactMethodPage.js b/src/pages/settings/Profile/Contacts/NewContactMethodPage.js index 70e0860feb95..e32380f08015 100644 --- a/src/pages/settings/Profile/Contacts/NewContactMethodPage.js +++ b/src/pages/settings/Profile/Contacts/NewContactMethodPage.js @@ -1,4 +1,4 @@ -import React, {Component} from 'react'; +import React, {useCallback, useRef, useState} from 'react'; import PropTypes from 'prop-types'; import {View} from 'react-native'; import {ScrollView} from 'react-native-gesture-handler'; @@ -52,22 +52,10 @@ const defaultProps = { loginList: {}, }; -class NewContactMethodPage extends Component { - constructor(props) { - super(props); - - this.state = { - login: '', - password: '', - }; - this.onLoginChange = this.onLoginChange.bind(this); - this.validateForm = this.validateForm.bind(this); - this.submitForm = this.submitForm.bind(this); - } - - onLoginChange(login) { - this.setState({login}); - } +function NewContactMethodPage(props) { + const [login, setLogin] = useState(''); + const [password, setPassword] = useState(''); + const loginInputRef = useRef(null); /** * Determine whether the form is valid From 2cd76d101abbd2ab191c198d5d6f1dba72d3dc9a Mon Sep 17 00:00:00 2001 From: David Bondy Date: Fri, 14 Apr 2023 16:52:13 -0600 Subject: [PATCH 02/16] port methods to new format --- .../Profile/Contacts/NewContactMethodPage.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/pages/settings/Profile/Contacts/NewContactMethodPage.js b/src/pages/settings/Profile/Contacts/NewContactMethodPage.js index e32380f08015..dd3d6e377801 100644 --- a/src/pages/settings/Profile/Contacts/NewContactMethodPage.js +++ b/src/pages/settings/Profile/Contacts/NewContactMethodPage.js @@ -62,22 +62,21 @@ function NewContactMethodPage(props) { * * @returns {Boolean} */ - validateForm() { - const login = this.state.login.trim(); - const phoneLogin = LoginUtils.getPhoneNumberWithoutSpecialChars(login); + const validateForm = useCallback(() => { + const phoneLogin = LoginUtils.getPhoneNumberWithoutSpecialChars(login.trim()); - return (Permissions.canUsePasswordlessLogins(this.props.betas) || this.state.password) + return (Permissions.canUsePasswordlessLogins(props.betas) || this.state.password) && (Str.isValidEmail(login) || Str.isValidPhone(phoneLogin)); - } + }, [login, password, props.betas]); - submitForm() { + const submitForm = useCallback(() => { // If this login already exists, just go back. - if (lodashGet(this.props.loginList, this.state.login)) { + if (lodashGet(props.loginList, login)) { Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS); return; } - User.addNewContactMethodAndNavigate(this.state.login, this.state.password); - } + User.addNewContactMethodAndNavigate(login, password); + }, [login, props.loginList, password]); render() { return ( From a7768f0b478f3215727da871f34068c410a394c8 Mon Sep 17 00:00:00 2001 From: David Bondy Date: Fri, 14 Apr 2023 16:52:50 -0600 Subject: [PATCH 03/16] update remaining component body to work with functional component --- .../Profile/Contacts/NewContactMethodPage.js | 104 +++++++++--------- 1 file changed, 51 insertions(+), 53 deletions(-) diff --git a/src/pages/settings/Profile/Contacts/NewContactMethodPage.js b/src/pages/settings/Profile/Contacts/NewContactMethodPage.js index dd3d6e377801..582ff07cbbb8 100644 --- a/src/pages/settings/Profile/Contacts/NewContactMethodPage.js +++ b/src/pages/settings/Profile/Contacts/NewContactMethodPage.js @@ -78,60 +78,58 @@ function NewContactMethodPage(props) { User.addNewContactMethodAndNavigate(login, password); }, [login, props.loginList, password]); - render() { - return ( - { - if (!this.loginInputRef) { - return; - } - this.loginInputRef.focus(); - }} - > - Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS)} - onCloseButtonPress={() => Navigation.dismissModal(true)} - /> - - - {this.props.translate('common.pleaseEnterEmailOrPhoneNumber')} - - - this.loginInputRef = el} - value={this.state.login} - onChangeText={this.onLoginChange} - autoCapitalize="none" - returnKeyType={Permissions.canUsePasswordlessLogins(this.props.betas) ? 'done' : 'next'} - /> - - {!Permissions.canUsePasswordlessLogins(this.props.betas) - && ( - - this.setState({password})} - returnKeyType="done" - /> - - )} - - -