Skip to content

Commit

Permalink
Merge pull request Expensify#35359 from GandalfGwaihir/issue-25219
Browse files Browse the repository at this point in the history
[TS migration] Migrate 'SettingsSecurity2FA' page to TypeScript
  • Loading branch information
puneetlath authored Mar 6, 2024
2 parents 042bdcc + 3cdc3ce commit a04481f
Show file tree
Hide file tree
Showing 24 changed files with 173 additions and 261 deletions.
2 changes: 1 addition & 1 deletion src/components/AnimatedStep/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type AnimatedStepProps = ChildrenProps & {
direction: AnimationDirection;

/** Callback to fire when the animation ends */
onAnimationEnd: () => void;
onAnimationEnd?: () => void;
};

function AnimatedStep({onAnimationEnd, direction = CONST.ANIMATION_DIRECTION.IN, style, children}: AnimatedStepProps) {
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useWindowDimensions/index.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function (): WindowDimensions {
const isSmallScreenWidth = true;
const isMediumScreenWidth = false;
const isLargeScreenWidth = false;
const isExtraSmallScreenWidth = windowWidth <= variables.extraSmallMobileResponsiveWidthBreakpoint;
const isSmallScreen = true;

return {
Expand All @@ -21,6 +22,7 @@ export default function (): WindowDimensions {
isSmallScreenWidth,
isMediumScreenWidth,
isLargeScreenWidth,
isExtraSmallScreenWidth,
isSmallScreen,
};
}
2 changes: 2 additions & 0 deletions src/hooks/useWindowDimensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default function (useCachedViewportHeight = false): WindowDimensions {
const isSmallScreenWidth = windowWidth <= variables.mobileResponsiveWidthBreakpoint;
const isMediumScreenWidth = windowWidth > variables.mobileResponsiveWidthBreakpoint && windowWidth <= variables.tabletResponsiveWidthBreakpoint;
const isLargeScreenWidth = windowWidth > variables.tabletResponsiveWidthBreakpoint;
const isExtraSmallScreenWidth = windowWidth <= variables.extraSmallMobileResponsiveWidthBreakpoint;

const lowerScreenDimmension = Math.min(windowWidth, windowHeight);
const isSmallScreen = lowerScreenDimmension <= variables.mobileResponsiveWidthBreakpoint;
Expand Down Expand Up @@ -88,6 +89,7 @@ export default function (useCachedViewportHeight = false): WindowDimensions {
isSmallScreenWidth,
isMediumScreenWidth,
isLargeScreenWidth,
isExtraSmallScreenWidth,
isSmallScreen,
};
}
1 change: 1 addition & 0 deletions src/hooks/useWindowDimensions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type WindowDimensions = {
isSmallScreenWidth: boolean;
isMediumScreenWidth: boolean;
isLargeScreenWidth: boolean;
isExtraSmallScreenWidth: boolean;
isSmallScreen: boolean;
};

Expand Down
7 changes: 6 additions & 1 deletion src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ type WorkspaceSwitcherNavigatorParamList = {
[SCREENS.WORKSPACE_SWITCHER.ROOT]: undefined;
};

type BackToParams = {
backTo?: Routes;
};

type SettingsNavigatorParamList = {
[SCREENS.SETTINGS.ROOT]: undefined;
[SCREENS.SETTINGS.SHARE_CODE]: undefined;
Expand Down Expand Up @@ -203,7 +207,7 @@ type SettingsNavigatorParamList = {
[SCREENS.GET_ASSISTANCE]: {
backTo: Routes;
};
[SCREENS.SETTINGS.TWO_FACTOR_AUTH]: undefined;
[SCREENS.SETTINGS.TWO_FACTOR_AUTH]: BackToParams;
[SCREENS.SETTINGS.REPORT_CARD_LOST_OR_DAMAGED]: undefined;
[SCREENS.KEYBOARD_SHORTCUTS]: undefined;
[SCREENS.SETTINGS.EXIT_SURVEY.REASON]: undefined;
Expand Down Expand Up @@ -632,4 +636,5 @@ export type {
WorkspaceSwitcherNavigatorParamList,
OnboardEngagementNavigatorParamList,
SwitchPolicyIDParams,
BackToParams,
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,35 @@ import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import useThemeStyles from '@hooks/useThemeStyles';
import * as TwoFactorAuthActions from '@userActions/TwoFactorAuthActions';
import StepWrapperPropTypes from './StepWrapperPropTypes';
import type {StepCounterParams} from '@src/languages/types';
import type ChildrenProps from '@src/types/utils/ChildrenProps';

type StepWrapperProps = ChildrenProps & {
/** Title of the Header */
title?: string;

/** Data to display a step counter in the header */
stepCounter?: StepCounterParams;

/** Method to trigger when pressing back button of the header */
onBackButtonPress?: () => void;

/** Called when navigated Screen's transition is finished. It does not fire when user exits the page. */
onEntryTransitionEnd?: () => void;

/** Flag to indicate if the keyboard avoiding view should be enabled */
shouldEnableKeyboardAvoidingView?: boolean;
};

function StepWrapper({
title = '',
stepCounter = null,
stepCounter,
onBackButtonPress = () => TwoFactorAuthActions.quitAndNavigateBack(),
children = null,
shouldEnableKeyboardAvoidingView = true,
onEntryTransitionEnd,
}) {
}: StepWrapperProps) {
const styles = useThemeStyles();
const shouldShowStepCounter = Boolean(stepCounter);

const {animationDirection} = useAnimatedStepContext();

return (
Expand All @@ -34,7 +50,6 @@ function StepWrapper({
>
<HeaderWithBackButton
title={title}
shouldShowStepCounter={shouldShowStepCounter}
stepCounter={stepCounter}
onBackButtonPress={onBackButtonPress}
/>
Expand All @@ -44,7 +59,6 @@ function StepWrapper({
);
}

StepWrapper.propTypes = StepWrapperPropTypes;
StepWrapper.displayName = 'StepWrapper';

export default StepWrapper;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, {useEffect, useState} from 'react';
import {ActivityIndicator, ScrollView, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import Button from '@components/Button';
import FixedFooter from '@components/FixedFooter';
import FormHelpMessage from '@components/FormHelpMessage';
Expand All @@ -16,15 +15,18 @@ import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import Clipboard from '@libs/Clipboard';
import localFileDownload from '@libs/localFileDownload';
import type {BackToParams} from '@libs/Navigation/types';
import StepWrapper from '@pages/settings/Security/TwoFactorAuth/StepWrapper/StepWrapper';
import useTwoFactorAuthContext from '@pages/settings/Security/TwoFactorAuth/TwoFactorAuthContext/useTwoFactorAuth';
import {defaultAccount, TwoFactorAuthPropTypes} from '@pages/settings/Security/TwoFactorAuth/TwoFactorAuthPropTypes';
import type {BaseTwoFactorAuthFormOnyxProps} from '@pages/settings/Security/TwoFactorAuth/TwoFactorAuthForm/types';
import * as Session from '@userActions/Session';
import * as TwoFactorAuthActions from '@userActions/TwoFactorAuthActions';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';

function CodesStep({account = defaultAccount, backTo}) {
type CodesStepProps = BaseTwoFactorAuthFormOnyxProps & BackToParams;

function CodesStep({account, backTo}: CodesStepProps) {
const theme = useTheme();
const styles = useThemeStyles();
const {translate} = useLocalize();
Expand All @@ -34,7 +36,8 @@ function CodesStep({account = defaultAccount, backTo}) {
const {setStep} = useTwoFactorAuthContext();

useEffect(() => {
if (account.requiresTwoFactorAuth || account.recoveryCodes) {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (account?.requiresTwoFactorAuth || account?.recoveryCodes) {
return;
}
Session.toggleTwoFactorAuth(true);
Expand Down Expand Up @@ -63,15 +66,15 @@ function CodesStep({account = defaultAccount, backTo}) {
<Text>{translate('twoFactorAuth.codesLoseAccess')}</Text>
</View>
<View style={styles.twoFactorAuthCodesBox({isExtraSmallScreenWidth, isSmallScreenWidth})}>
{account.isLoading ? (
{account?.isLoading ? (
<View style={styles.twoFactorLoadingContainer}>
<ActivityIndicator color={theme.spinner} />
</View>
) : (
<>
<View style={styles.twoFactorAuthCodesContainer}>
{Boolean(account.recoveryCodes) &&
_.map(account.recoveryCodes.split(', '), (code) => (
{Boolean(account?.recoveryCodes) &&
account?.recoveryCodes?.split(', ').map((code) => (
<Text
style={styles.twoFactorAuthCode}
key={code}
Expand All @@ -87,32 +90,38 @@ function CodesStep({account = defaultAccount, backTo}) {
icon={Expensicons.Copy}
inline={false}
onPress={() => {
Clipboard.setString(account.recoveryCodes);
Clipboard.setString(account?.recoveryCodes ?? '');
setError('');
TwoFactorAuthActions.setCodesAreCopied();
}}
styles={[styles.button, styles.buttonMedium, styles.twoFactorAuthCodesButton]}
textStyles={[styles.buttonMediumText]}
accessible={false}
tooltipText=""
tooltipTextChecked=""
/>
<PressableWithDelayToggle
text={translate('common.download')}
icon={Expensicons.Download}
onPress={() => {
localFileDownload('two-factor-auth-codes', account.recoveryCodes);
localFileDownload('two-factor-auth-codes', account?.recoveryCodes ?? '');
setError('');
TwoFactorAuthActions.setCodesAreCopied();
}}
inline={false}
styles={[styles.button, styles.buttonMedium, styles.twoFactorAuthCodesButton]}
textStyles={[styles.buttonMediumText]}
accessible={false}
tooltipText=""
tooltipTextChecked=""
/>
</View>
</>
)}
</View>
</Section>
<FixedFooter style={[styles.mtAuto, styles.pt5]}>
{!_.isEmpty(error) && (
{Boolean(error) && (
<FormHelpMessage
isError
message={error}
Expand All @@ -123,7 +132,7 @@ function CodesStep({account = defaultAccount, backTo}) {
success
text={translate('common.next')}
onPress={() => {
if (!account.codesAreCopied) {
if (!account?.codesAreCopied) {
setError('twoFactorAuth.errorStepCodes');
return;
}
Expand All @@ -136,10 +145,8 @@ function CodesStep({account = defaultAccount, backTo}) {
);
}

CodesStep.propTypes = TwoFactorAuthPropTypes;
CodesStep.displayName = 'CodesStep';

// eslint-disable-next-line rulesdir/onyx-props-must-have-default
export default withOnyx({
export default withOnyx<CodesStepProps, BaseTwoFactorAuthFormOnyxProps>({
account: {key: ONYXKEYS.ACCOUNT},
})(CodesStep);
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import PropTypes from 'prop-types';
import React from 'react';
import ConfirmationPage from '@components/ConfirmationPage';
import LottieAnimations from '@components/LottieAnimations';
import useLocalize from '@hooks/useLocalize';
import type {BackToParams} from '@libs/Navigation/types';
import Navigation from '@navigation/Navigation';
import StepWrapper from '@pages/settings/Security/TwoFactorAuth/StepWrapper/StepWrapper';
import useTwoFactorAuthContext from '@pages/settings/Security/TwoFactorAuth/TwoFactorAuthContext/useTwoFactorAuth';
import * as TwoFactorAuthActions from '@userActions/TwoFactorAuthActions';
import CONST from '@src/CONST';

const propTypes = {
/** The route where user needs to be redirected after setting up 2FA */
backTo: PropTypes.string,
};

const defaultProps = {
backTo: '',
};

function SuccessStep({backTo}) {
function SuccessStep({backTo}: BackToParams) {
const {setStep} = useTwoFactorAuthContext();

const {translate} = useLocalize();
Expand Down Expand Up @@ -49,7 +40,4 @@ function SuccessStep({backTo}) {
);
}

SuccessStep.propTypes = propTypes;
SuccessStep.defaultProps = defaultProps;

export default SuccessStep;
Loading

0 comments on commit a04481f

Please sign in to comment.