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

Removing payment method actions model when payment method is removed from another device #14869

Merged
81 changes: 56 additions & 25 deletions src/pages/settings/Payments/PaymentsPage/BasePaymentsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class BasePaymentsPage extends React.Component {
formattedSelectedPaymentMethod: {
title: '',
},
selectedPaymentMethodType: null,
anchorPositionTop: 0,
anchorPositionBottom: 0,
anchorPositionRight: 0,
Expand Down Expand Up @@ -87,6 +88,27 @@ class BasePaymentsPage extends React.Component {
this.debounceSetShouldShowLoadingSpinner();
}

if (this.state.shouldShowDefaultDeleteMenu || this.state.shouldShowPasswordPrompt) {
// We should reset selected payment method state values and close corresponding modals if the selected payment method is deleted
let shouldResetPaymentMethodData = false;

if (this.state.selectedPaymentMethodType === CONST.PAYMENT_METHODS.BANK_ACCOUNT && _.isEmpty(this.props.bankAccountList[this.state.methodID])) {
abdulrahuman5196 marked this conversation as resolved.
Show resolved Hide resolved
shouldResetPaymentMethodData = true;
} else if (this.state.selectedPaymentMethodType === CONST.PAYMENT_METHODS.DEBIT_CARD && _.isEmpty(this.props.cardList[this.state.methodID])) {
shouldResetPaymentMethodData = true;
} else if (this.state.selectedPaymentMethodType === CONST.PAYMENT_METHODS.PAYPAL && this.props.payPalMeData !== prevProps.payPalMeData && _.isEmpty(this.props.payPalMeData)) {
shouldResetPaymentMethodData = true;
}
if (shouldResetPaymentMethodData) {
// Close corresponding selected payment method modals which are open
if (this.state.shouldShowDefaultDeleteMenu) {
this.hideDefaultDeleteMenu();
} else if (this.state.shouldShowPasswordPrompt) {
this.hidePasswordPrompt();
}
}
}

// previously online OR currently offline, skip fetch
if (!prevProps.network.isOffline || this.props.network.isOffline) {
return;
Expand Down Expand Up @@ -138,6 +160,23 @@ class BasePaymentsPage extends React.Component {
});
}

resetSelectedPaymentMethodData() {
// The below state values are used by payment method modals and we reset them while closing the modals.
// We should only reset the values when the modal animation is completed and so using InteractionManager.runAfterInteractions which fires after all animaitons are complete
InteractionManager.runAfterInteractions(() => {
abdulrahuman5196 marked this conversation as resolved.
Show resolved Hide resolved
// Reset to same values as in the constructor
this.setState({
isSelectedPaymentMethodDefault: false,
selectedPaymentMethod: {},
formattedSelectedPaymentMethod: {
title: '',
},
methodID: null,
selectedPaymentMethodType: null,
});
});
}

/**
* Display the delete/default menu, or the add payment method menu
*
Expand Down Expand Up @@ -235,18 +274,25 @@ class BasePaymentsPage extends React.Component {

/**
* Hide the default / delete modal
* @param {boolean} shouldClearSelectedData - Clear selected payment method data if true
*/
hideDefaultDeleteMenu() {
hideDefaultDeleteMenu(shouldClearSelectedData = true) {
this.setState({shouldShowDefaultDeleteMenu: false});
InteractionManager.runAfterInteractions(() => {
this.setState({
showConfirmDeleteContent: false,
});
if (shouldClearSelectedData) {
this.resetSelectedPaymentMethodData();
}
abdulrahuman5196 marked this conversation as resolved.
Show resolved Hide resolved
});
}

hidePasswordPrompt() {
hidePasswordPrompt(shouldClearSelectedData = true) {
this.setState({shouldShowPasswordPrompt: false});
if (shouldClearSelectedData) {
this.resetSelectedPaymentMethodData();
}

// Due to iOS modal freeze issue, password modal freezes the app when closed.
// LayoutAnimation undoes the running animation.
Expand All @@ -267,6 +313,7 @@ class BasePaymentsPage extends React.Component {
} else if (this.state.selectedPaymentMethodType === CONST.PAYMENT_METHODS.DEBIT_CARD) {
PaymentMethods.makeDefaultPaymentMethod(password, null, this.state.selectedPaymentMethod.fundID, previousPaymentMethod, currentPaymentMethod);
}
this.resetSelectedPaymentMethodData();
}

deletePaymentMethod() {
Expand All @@ -277,6 +324,7 @@ class BasePaymentsPage extends React.Component {
} else if (this.state.selectedPaymentMethodType === CONST.PAYMENT_METHODS.DEBIT_CARD) {
PaymentMethods.deletePaymentCard(this.state.selectedPaymentMethod.fundID);
}
this.resetSelectedPaymentMethodData();
}

navigateToTransferBalancePage() {
Expand Down Expand Up @@ -439,30 +487,10 @@ class BasePaymentsPage extends React.Component {
) : (
<ConfirmContent
onConfirm={() => {
this.setState({
shouldShowDefaultDeleteMenu: false,
});
InteractionManager.runAfterInteractions(() => {
this.setState({
showConfirmDeleteContent: false,
});
});
this.hideDefaultDeleteMenu(false);
this.deletePaymentMethod();
}}
onCancel={() => {
this.setState({
shouldShowDefaultDeleteMenu: false,
});
InteractionManager.runAfterInteractions(
() => {
this.setState(
{
showConfirmDeleteContent: false,
},
);
},
);
}}
onCancel={this.hideDefaultDeleteMenu}
contentStyles={!this.props.isSmallScreenWidth ? [styles.sidebarPopover] : undefined}
title={this.props.translate('paymentsPage.deleteAccount')}
prompt={this.props.translate('paymentsPage.deleteConfirmation')}
Expand All @@ -485,7 +513,7 @@ class BasePaymentsPage extends React.Component {
right: this.state.anchorPositionRight,
}}
onSubmit={(password) => {
this.hidePasswordPrompt();
this.hidePasswordPrompt(false);
this.makeDefaultPaymentMethod(password);
}}
submitButtonText={this.state.passwordButtonText}
Expand Down Expand Up @@ -521,6 +549,9 @@ export default compose(
walletTerms: {
key: ONYXKEYS.WALLET_TERMS,
},
payPalMeData: {
key: ONYXKEYS.PAYPAL,
},
isLoadingPaymentMethods: {
key: ONYXKEYS.IS_LOADING_PAYMENT_METHODS,
},
Expand Down