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/16232/migration enable payments page to function component #28236

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 57 additions & 65 deletions src/pages/EnablePayments/EnablePaymentsPage.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import _ from 'underscore';
import React from 'react';
import React, {useEffect} from 'react';
import {withOnyx} from 'react-native-onyx';
import ScreenWrapper from '../../components/ScreenWrapper';
import * as Wallet from '../../libs/actions/Wallet';
import ONYXKEYS from '../../ONYXKEYS';
import FullScreenLoadingIndicator from '../../components/FullscreenLoadingIndicator';
import CONST from '../../CONST';
import userWalletPropTypes from './userWalletPropTypes';
import {withNetwork} from '../../components/OnyxProvider';
import networkPropTypes from '../../components/networkPropTypes';

// Steps
import OnfidoStep from './OnfidoStep';
Expand All @@ -17,94 +15,88 @@ import TermsStep from './TermsStep';
import ActivateStep from './ActivateStep';
import HeaderWithBackButton from '../../components/HeaderWithBackButton';
import FailedKYC from './FailedKYC';
import compose from '../../libs/compose';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import Navigation from '../../libs/Navigation/Navigation';
import ROUTES from '../../ROUTES';
import useLocalize from '../../hooks/useLocalize';
import useNetwork from '../../hooks/useNetwork';

const propTypes = {
/** Information about the network from Onyx */
network: networkPropTypes.isRequired,

/** The user's wallet */
userWallet: userWalletPropTypes,

...withLocalizePropTypes,
};

const defaultProps = {
userWallet: {},
};

class EnablePaymentsPage extends React.Component {
componentDidMount() {
Wallet.openEnablePaymentsPage();
}
function EnablePaymentsPage({userWallet}) {
const {translate} = useLocalize();
const {isOffline} = useNetwork();

componentDidUpdate(prevProps) {
if (!prevProps.network.isOffline || this.props.network.isOffline) {
useEffect(() => {
if (isOffline) {
return;
}

Wallet.openEnablePaymentsPage();
}
}, [isOffline]);

render() {
if (_.isEmpty(this.props.userWallet)) {
return <FullScreenLoadingIndicator />;
}

return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
testID={EnablePaymentsPage.displayName}
>
{() => {
if (this.props.userWallet.errorCode === CONST.WALLET.ERROR.KYC) {
return (
<>
<HeaderWithBackButton
title={this.props.translate('additionalDetailsStep.headerTitle')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WALLET)}
/>
<FailedKYC />
</>
);
}

if (this.props.userWallet.shouldShowWalletActivationSuccess) {
return <ActivateStep userWallet={this.props.userWallet} />;
}

const currentStep = this.props.userWallet.currentStep || CONST.WALLET.STEP.ADDITIONAL_DETAILS;
if (_.isEmpty(userWallet)) {
return <FullScreenLoadingIndicator />;
}

return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
testID={EnablePaymentsPage.displayName}
>
{() => {
if (userWallet.errorCode === CONST.WALLET.ERROR.KYC) {
return (
<>
{(currentStep === CONST.WALLET.STEP.ADDITIONAL_DETAILS || currentStep === CONST.WALLET.STEP.ADDITIONAL_DETAILS_KBA) && <AdditionalDetailsStep />}
{currentStep === CONST.WALLET.STEP.ONFIDO && <OnfidoStep />}
{currentStep === CONST.WALLET.STEP.TERMS && <TermsStep />}
{currentStep === CONST.WALLET.STEP.ACTIVATE && <ActivateStep userWallet={this.props.userWallet} />}
<HeaderWithBackButton
title={translate('additionalDetailsStep.headerTitle')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WALLET)}
/>
<FailedKYC />
</>
);
}}
</ScreenWrapper>
);
}
}

if (userWallet.shouldShowWalletActivationSuccess) {
return <ActivateStep userWallet={userWallet} />;
}

const currentStep = userWallet.currentStep || CONST.WALLET.STEP.ADDITIONAL_DETAILS;

switch (currentStep) {
case CONST.WALLET.STEP.ADDITIONAL_DETAILS:
case CONST.WALLET.STEP.ADDITIONAL_DETAILS_KBA:
return <AdditionalDetailsStep />;
case CONST.WALLET.STEP.ONFIDO:
return <OnfidoStep />;
case CONST.WALLET.STEP.TERMS:
return <TermsStep />;
case CONST.WALLET.STEP.ACTIVATE:
return <ActivateStep userWallet={userWallet} />;
default:
return null;
}
}}
</ScreenWrapper>
);
}

EnablePaymentsPage.displayName = 'EnablePaymentsPage';
EnablePaymentsPage.propTypes = propTypes;
EnablePaymentsPage.defaultProps = defaultProps;

export default compose(
withLocalize,
withOnyx({
userWallet: {
key: ONYXKEYS.USER_WALLET,
export default withOnyx({
userWallet: {
key: ONYXKEYS.USER_WALLET,

// We want to refresh the wallet each time the user attempts to activate the wallet so we won't use the
// stored values here.
initWithStoredValues: false,
},
}),
withNetwork(),
)(EnablePaymentsPage);
// We want to refresh the wallet each time the user attempts to activate the wallet so we won't use the
// stored values here.
initWithStoredValues: false,
},
})(EnablePaymentsPage);
Loading