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

[TS migration] Migrate 'SettingsSecurityCloseAccount' page to TypeScript #34718

4 changes: 2 additions & 2 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ type OnyxValues = {
[ONYXKEYS.FORMS.WORKSPACE_SETTINGS_FORM_DRAFT]: OnyxTypes.Form;
[ONYXKEYS.FORMS.WORKSPACE_RATE_AND_UNIT_FORM]: OnyxTypes.Form;
[ONYXKEYS.FORMS.WORKSPACE_RATE_AND_UNIT_FORM_DRAFT]: OnyxTypes.Form;
[ONYXKEYS.FORMS.CLOSE_ACCOUNT_FORM]: OnyxTypes.Form;
[ONYXKEYS.FORMS.CLOSE_ACCOUNT_FORM_DRAFT]: OnyxTypes.Form;
[ONYXKEYS.FORMS.CLOSE_ACCOUNT_FORM]: OnyxTypes.CloseAccountForm;
[ONYXKEYS.FORMS.CLOSE_ACCOUNT_FORM_DRAFT]: OnyxTypes.CloseAccountForm;
[ONYXKEYS.FORMS.PROFILE_SETTINGS_FORM]: OnyxTypes.Form;
[ONYXKEYS.FORMS.PROFILE_SETTINGS_FORM_DRAFT]: OnyxTypes.Form;
[ONYXKEYS.FORMS.DISPLAY_NAME_FORM]: OnyxTypes.DisplayNameForm;
Expand Down
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('');

Expand All @@ -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 ?? '');
Copy link
Contributor

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?

Copy link
Contributor Author

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 has null 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 update formatPhoneNumber typing instead, let me try

Copy link
Contributor Author

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?

Copy link
Contributor

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?

Copy link
Contributor Author

@VickyStash VickyStash Jan 31, 2024

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

Copy link
Contributor

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 by withOnyx and therefore can be null as any other Onyx value, so we can either default the value to '' or change formatPhoneNumber to accept and handle undefined values too. Looks like @VickyStash already changed formatPhoneNumber to support it and I think it's the best approach, wdyt?

const errors = ValidationUtils.getFieldRequiredErrors(values, requiredFields);

if (values.phoneOrEmail && sanitizePhoneOrEmail(userEmailOrPhone) !== sanitizePhoneOrEmail(values.phoneOrEmail)) {
Expand All @@ -82,59 +78,59 @@ function CloseAccountPage(props) {
return errors;
};

const userEmailOrPhone = props.formatPhoneNumber(props.session.email);
const userEmailOrPhone = formatPhoneNumber(session?.email ?? '');
Copy link
Contributor

Choose a reason for hiding this comment

The 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
/>
Expand All @@ -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);
7 changes: 6 additions & 1 deletion src/types/onyx/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ type PrivateNotesForm = Form<{
privateNotes: string;
}>;

type CloseAccountForm = Form<{
reasonForLeaving: string;
phoneOrEmail: string;
}>;

export default Form;

export type {AddDebitCardForm, DateOfBirthForm, PrivateNotesForm, DisplayNameForm, FormValueType, NewRoomForm, BaseForm, IKnowATeacherForm, IntroSchoolPrincipalForm};
export type {AddDebitCardForm, DateOfBirthForm, PrivateNotesForm, DisplayNameForm, FormValueType, NewRoomForm, BaseForm, IKnowATeacherForm, IntroSchoolPrincipalForm, CloseAccountForm};
3 changes: 2 additions & 1 deletion src/types/onyx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type Credentials from './Credentials';
import type Currency from './Currency';
import type CustomStatusDraft from './CustomStatusDraft';
import type Download from './Download';
import type {AddDebitCardForm, DateOfBirthForm, DisplayNameForm, IKnowATeacherForm, IntroSchoolPrincipalForm, NewRoomForm, PrivateNotesForm} from './Form';
import type {AddDebitCardForm, CloseAccountForm, DateOfBirthForm, DisplayNameForm, IKnowATeacherForm, IntroSchoolPrincipalForm, NewRoomForm, PrivateNotesForm} from './Form';
import type Form from './Form';
import type FrequentlyUsedEmoji from './FrequentlyUsedEmoji';
import type {FundList} from './Fund';
Expand Down Expand Up @@ -82,6 +82,7 @@ export type {
Credentials,
Currency,
CustomStatusDraft,
CloseAccountForm,
DateOfBirthForm,
Download,
Form,
Expand Down
Loading