-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
[TS migration] Migrate 'SettingsSecurityCloseAccount' page to TypeScript #34718
Merged
flodnv
merged 11 commits into
Expensify:main
from
VickyStash:ts-migration/settingsSecurityCloseAccount-page
Feb 2, 2024
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4c04f9b
[TS migration] Migrate 'SettingsSecurityCloseAccount' page
VickyStash 1a23531
Merge branch 'main' into ts-migration/settingsSecurityCloseAccount-page
VickyStash d788f79
Remove extra TODOs, add CloseAccountForm type
VickyStash fc2ab2b
Update formatPhoneNumber typing
VickyStash 9b4b547
Merge branch 'main' into ts-migration/settingsSecurityCloseAccount-page
VickyStash 8c4b3d0
Fix Onyx types import
VickyStash 04b269a
Merge branch 'main' into ts-migration/settingsSecurityCloseAccount-page
VickyStash 026d292
Merge branch 'main' into ts-migration/settingsSecurityCloseAccount-page
VickyStash 39ef206
Lint fix
VickyStash 66e38c6
Merge branch 'main' into ts-migration/settingsSecurityCloseAccount-page
VickyStash d2f71a8
Fix lint error
VickyStash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,42 @@ | ||
import type {StackScreenProps} from '@react-navigation/stack'; | ||
import Str from 'expensify-common/lib/str'; | ||
import PropTypes from 'prop-types'; | ||
import React, {useEffect, useState} from 'react'; | ||
import {View} from 'react-native'; | ||
import {withOnyx} from 'react-native-onyx'; | ||
import type {OnyxEntry} from 'react-native-onyx/lib/types'; | ||
import ConfirmModal from '@components/ConfirmModal'; | ||
import FormProvider from '@components/Form/FormProvider'; | ||
import InputWrapper from '@components/Form/InputWrapper'; | ||
import type {OnyxFormValuesFields} from '@components/Form/types'; | ||
import HeaderWithBackButton from '@components/HeaderWithBackButton'; | ||
import ScreenWrapper from '@components/ScreenWrapper'; | ||
import Text from '@components/Text'; | ||
import TextInput from '@components/TextInput'; | ||
import withLocalize, {withLocalizePropTypes} from '@components/withLocalize'; | ||
import withWindowDimensions, {windowDimensionsPropTypes} from '@components/withWindowDimensions'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import compose from '@libs/compose'; | ||
import Navigation from '@libs/Navigation/Navigation'; | ||
import * as ValidationUtils from '@libs/ValidationUtils'; | ||
import type {SettingsNavigatorParamList} from '@navigation/types'; | ||
import * as CloseAccount from '@userActions/CloseAccount'; | ||
import * as User from '@userActions/User'; | ||
import CONST from '@src/CONST'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import ROUTES from '@src/ROUTES'; | ||
import type SCREENS from '@src/SCREENS'; | ||
import type {Session} from '@src/types/onyx'; | ||
import type {Errors} from '@src/types/onyx/OnyxCommon'; | ||
|
||
const propTypes = { | ||
type CloseAccountPageOnyxProps = { | ||
/** Session of currently logged in user */ | ||
session: PropTypes.shape({ | ||
/** Email address */ | ||
email: PropTypes.string.isRequired, | ||
}), | ||
|
||
...windowDimensionsPropTypes, | ||
...withLocalizePropTypes, | ||
session: OnyxEntry<Session>; | ||
}; | ||
|
||
const defaultProps = { | ||
session: { | ||
email: null, | ||
}, | ||
}; | ||
type CloseAccountPageProps = CloseAccountPageOnyxProps & StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.CLOSE>; | ||
|
||
function CloseAccountPage(props) { | ||
function CloseAccountPage({session}: CloseAccountPageProps) { | ||
const styles = useThemeStyles(); | ||
const {translate, formatPhoneNumber} = useLocalize(); | ||
|
||
const [isConfirmModalVisible, setConfirmModalVisibility] = useState(false); | ||
const [reasonForLeaving, setReasonForLeaving] = useState(''); | ||
|
||
|
@@ -59,21 +55,21 @@ function CloseAccountPage(props) { | |
hideConfirmModal(); | ||
}; | ||
|
||
const showConfirmModal = (values) => { | ||
const showConfirmModal = (values: OnyxFormValuesFields<typeof ONYXKEYS.FORMS.CLOSE_ACCOUNT_FORM>) => { | ||
setConfirmModalVisibility(true); | ||
setReasonForLeaving(values.reasonForLeaving); | ||
}; | ||
|
||
/** | ||
* Removes spaces and transform the input string to lowercase. | ||
* @param {String} phoneOrEmail - The input string to be sanitized. | ||
* @returns {String} The sanitized string | ||
* @param phoneOrEmail - The input string to be sanitized. | ||
* @returns The sanitized string | ||
*/ | ||
const sanitizePhoneOrEmail = (phoneOrEmail) => phoneOrEmail.replace(/\s+/g, '').toLowerCase(); | ||
const sanitizePhoneOrEmail = (phoneOrEmail: string): string => phoneOrEmail.replace(/\s+/g, '').toLowerCase(); | ||
|
||
const validate = (values) => { | ||
const validate = (values: OnyxFormValuesFields<typeof ONYXKEYS.FORMS.CLOSE_ACCOUNT_FORM>): Errors => { | ||
const requiredFields = ['phoneOrEmail']; | ||
const userEmailOrPhone = props.formatPhoneNumber(props.session.email); | ||
const userEmailOrPhone = formatPhoneNumber(session?.email ?? ''); | ||
const errors = ValidationUtils.getFieldRequiredErrors(values, requiredFields); | ||
|
||
if (values.phoneOrEmail && sanitizePhoneOrEmail(userEmailOrPhone) !== sanitizePhoneOrEmail(values.phoneOrEmail)) { | ||
|
@@ -82,59 +78,59 @@ function CloseAccountPage(props) { | |
return errors; | ||
}; | ||
|
||
const userEmailOrPhone = props.formatPhoneNumber(props.session.email); | ||
const userEmailOrPhone = formatPhoneNumber(session?.email ?? ''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same question |
||
|
||
return ( | ||
<ScreenWrapper | ||
includeSafeAreaPaddingBottom={false} | ||
testID={CloseAccountPage.displayName} | ||
> | ||
<HeaderWithBackButton | ||
title={props.translate('closeAccountPage.closeAccount')} | ||
title={translate('closeAccountPage.closeAccount')} | ||
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_SECURITY)} | ||
/> | ||
<FormProvider | ||
formID={ONYXKEYS.FORMS.CLOSE_ACCOUNT_FORM} | ||
validate={validate} | ||
onSubmit={showConfirmModal} | ||
submitButtonText={props.translate('closeAccountPage.closeAccount')} | ||
submitButtonText={translate('closeAccountPage.closeAccount')} | ||
style={[styles.flexGrow1, styles.mh5]} | ||
isSubmitActionDangerous | ||
> | ||
<View style={[styles.flexGrow1]}> | ||
<Text>{props.translate('closeAccountPage.reasonForLeavingPrompt')}</Text> | ||
<Text>{translate('closeAccountPage.reasonForLeavingPrompt')}</Text> | ||
<InputWrapper | ||
InputComponent={TextInput} | ||
inputID="reasonForLeaving" | ||
autoGrowHeight | ||
label={props.translate('closeAccountPage.enterMessageHere')} | ||
aria-label={props.translate('closeAccountPage.enterMessageHere')} | ||
label={translate('closeAccountPage.enterMessageHere')} | ||
aria-label={translate('closeAccountPage.enterMessageHere')} | ||
role={CONST.ROLE.PRESENTATION} | ||
containerStyles={[styles.mt5, styles.autoGrowHeightMultilineInput]} | ||
/> | ||
<Text style={[styles.mt5]}> | ||
{props.translate('closeAccountPage.enterDefaultContactToConfirm')} <Text style={[styles.textStrong]}>{userEmailOrPhone}</Text> | ||
{translate('closeAccountPage.enterDefaultContactToConfirm')} <Text style={[styles.textStrong]}>{userEmailOrPhone}</Text> | ||
</Text> | ||
<InputWrapper | ||
InputComponent={TextInput} | ||
inputID="phoneOrEmail" | ||
autoCapitalize="none" | ||
label={props.translate('closeAccountPage.enterDefaultContact')} | ||
aria-label={props.translate('closeAccountPage.enterDefaultContact')} | ||
label={translate('closeAccountPage.enterDefaultContact')} | ||
aria-label={translate('closeAccountPage.enterDefaultContact')} | ||
role={CONST.ROLE.PRESENTATION} | ||
containerStyles={[styles.mt5]} | ||
autoCorrect={false} | ||
inputMode={Str.isValidEmail(userEmailOrPhone) ? CONST.INPUT_MODE.EMAIL : CONST.INPUT_MODE.TEXT} | ||
/> | ||
<ConfirmModal | ||
danger | ||
title={props.translate('closeAccountPage.closeAccountWarning')} | ||
title={translate('closeAccountPage.closeAccountWarning')} | ||
onConfirm={onConfirm} | ||
onCancel={hideConfirmModal} | ||
isVisible={isConfirmModalVisible} | ||
prompt={props.translate('closeAccountPage.closeAccountPermanentlyDeleteData')} | ||
confirmText={props.translate('common.yesContinue')} | ||
cancelText={props.translate('common.cancel')} | ||
prompt={translate('closeAccountPage.closeAccountPermanentlyDeleteData')} | ||
confirmText={translate('common.yesContinue')} | ||
cancelText={translate('common.cancel')} | ||
shouldDisableConfirmButtonWhenOffline | ||
shouldShowCancelButton | ||
/> | ||
|
@@ -144,16 +140,10 @@ function CloseAccountPage(props) { | |
); | ||
} | ||
|
||
CloseAccountPage.propTypes = propTypes; | ||
CloseAccountPage.defaultProps = defaultProps; | ||
CloseAccountPage.displayName = 'CloseAccountPage'; | ||
|
||
export default compose( | ||
withLocalize, | ||
withWindowDimensions, | ||
withOnyx({ | ||
session: { | ||
key: ONYXKEYS.SESSION, | ||
}, | ||
}), | ||
)(CloseAccountPage); | ||
export default withOnyx<CloseAccountPageProps, CloseAccountPageOnyxProps>({ | ||
session: { | ||
key: ONYXKEYS.SESSION, | ||
}, | ||
})(CloseAccountPage); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why wouldn't we have an email here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
session
goes from Onyx it hasnull
in the typing. Email is also option field in the session, so to prevent TS error we need to add default value, but I can try to updateformatPhoneNumber
typing instead, let me tryThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@flodnv Done, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a noob, I want to try to understand more about this. I wanted to spin up a discussion in #expensify-open-source in Slack and tag you but I failed to find you on Slack, what is your handle?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@flodnv You can find me as
Viktoryia Kliushun
, I've also pinged you in Slack so you should see me there@fabioh8010 @blazejkustra It would be great to here your thoughts on this one since we have doubts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@flodnv In this component
session
is a Onyx prop supplied bywithOnyx
and therefore can benull
as any other Onyx value, so we can either default the value to''
or changeformatPhoneNumber
to accept and handle undefined values too. Looks like @VickyStash already changedformatPhoneNumber
to support it and I think it's the best approach, wdyt?