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

FIx payment method popover toggle #26377

Merged
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
59 changes: 25 additions & 34 deletions src/pages/settings/Wallet/WalletPage/BaseWalletPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ function BaseWalletPage(props) {
selectedPaymentMethodType: null,
});
const addPaymentMethodAnchorRef = useRef(null);
const deletePaymentMethodAnchorRef = useRef(null);
const paymentMethodButtonRef = useRef(null);
const [anchorPosition, setAnchorPosition] = useState({
anchorPositionHorizontal: 0,
Expand Down Expand Up @@ -102,19 +101,15 @@ function BaseWalletPage(props) {
}, [paymentMethod.selectedPaymentMethod.bankAccountID, paymentMethod.selectedPaymentMethod.fundID, paymentMethod.selectedPaymentMethodType]);

const resetSelectedPaymentMethodData = useCallback(() => {
// 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
Julesssss marked this conversation as resolved.
Show resolved Hide resolved
InteractionManager.runAfterInteractions(() => {
// Reset to same values as in the constructor
setPaymentMethod({
isSelectedPaymentMethodDefault: false,
selectedPaymentMethod: {},
formattedSelectedPaymentMethod: {
title: '',
},
methodID: null,
selectedPaymentMethodType: null,
});
// Reset to same values as in the constructor
setPaymentMethod({
isSelectedPaymentMethodDefault: false,
selectedPaymentMethod: {},
formattedSelectedPaymentMethod: {
title: '',
},
methodID: null,
selectedPaymentMethodType: null,
});
}, [setPaymentMethod]);

Expand All @@ -133,6 +128,11 @@ function BaseWalletPage(props) {
return;
}

if (shouldShowDefaultDeleteMenu) {
setShouldShowDefaultDeleteMenu(false);
return;
}

paymentMethodButtonRef.current = nativeEvent.currentTarget;

// The delete/default menu
Expand Down Expand Up @@ -212,18 +212,12 @@ function BaseWalletPage(props) {
* Hide the default / delete modal
* @param {boolean} shouldClearSelectedData - Clear selected payment method data if true
*/
const hideDefaultDeleteMenu = useCallback(
(shouldClearSelectedData = true) => {
setShouldShowDefaultDeleteMenu(false);
InteractionManager.runAfterInteractions(() => {
setShowConfirmDeleteContent(false);
if (shouldClearSelectedData) {
resetSelectedPaymentMethodData();
}
});
},
[setShouldShowDefaultDeleteMenu, setShowConfirmDeleteContent, resetSelectedPaymentMethodData],
);
const hideDefaultDeleteMenu = useCallback(() => {
setShouldShowDefaultDeleteMenu(false);
InteractionManager.runAfterInteractions(() => {
setShowConfirmDeleteContent(false);
});
}, [setShouldShowDefaultDeleteMenu, setShowConfirmDeleteContent]);

const makeDefaultPaymentMethod = useCallback(() => {
const paymentCardList = props.fundList || {};
Expand All @@ -237,15 +231,13 @@ function BaseWalletPage(props) {
} else if (paymentMethod.selectedPaymentMethodType === CONST.PAYMENT_METHODS.DEBIT_CARD) {
PaymentMethods.makeDefaultPaymentMethod(null, paymentMethod.selectedPaymentMethod.fundID, previousPaymentMethod, currentPaymentMethod);
}
resetSelectedPaymentMethodData();
}, [
paymentMethod.methodID,
paymentMethod.selectedPaymentMethod.bankAccountID,
paymentMethod.selectedPaymentMethod.fundID,
paymentMethod.selectedPaymentMethodType,
props.bankAccountList,
props.fundList,
resetSelectedPaymentMethodData,
]);

const deletePaymentMethod = useCallback(() => {
Expand All @@ -256,8 +248,7 @@ function BaseWalletPage(props) {
} else if (paymentMethod.selectedPaymentMethodType === CONST.PAYMENT_METHODS.DEBIT_CARD) {
PaymentMethods.deletePaymentCard(paymentMethod.selectedPaymentMethod.fundID);
}
resetSelectedPaymentMethodData();
}, [paymentMethod.selectedPaymentMethod.bankAccountID, paymentMethod.selectedPaymentMethod.fundID, paymentMethod.selectedPaymentMethodType, resetSelectedPaymentMethodData]);
}, [paymentMethod.selectedPaymentMethod.bankAccountID, paymentMethod.selectedPaymentMethod.fundID, paymentMethod.selectedPaymentMethodType]);

const navigateToTransferBalancePage = () => {
Navigation.navigate(ROUTES.SETTINGS_WALLET_TRANSFER_BALANCE);
Expand Down Expand Up @@ -424,7 +415,8 @@ function BaseWalletPage(props) {
right: anchorPosition.anchorPositionRight,
}}
withoutOverlay
anchorRef={deletePaymentMethodAnchorRef}
anchorRef={paymentMethodButtonRef}
onModalHide={resetSelectedPaymentMethodData}
>
{!showConfirmDeleteContent ? (
<View style={[styles.m5, !isSmallScreenWidth ? styles.sidebarPopover : '']}>
Expand All @@ -440,8 +432,8 @@ function BaseWalletPage(props) {
{shouldShowMakeDefaultButton && (
<Button
onPress={() => {
setShouldShowDefaultDeleteMenu(false);
makeDefaultPaymentMethod();
setShouldShowDefaultDeleteMenu(false);
}}
Copy link
Contributor Author

@bernhardoj bernhardoj Aug 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as below (or above)

text={translate('walletPage.setDefaultConfirmation')}
/>
Expand All @@ -461,14 +453,13 @@ function BaseWalletPage(props) {
style={[shouldShowMakeDefaultButton ? styles.mt4 : {}]}
text={translate('common.delete')}
danger
ref={deletePaymentMethodAnchorRef}
/>
</View>
) : (
<ConfirmContent
onConfirm={() => {
hideDefaultDeleteMenu(false);
deletePaymentMethod();
hideDefaultDeleteMenu();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is actually no problem so far calling hideDefaultDeleteMenu before deletePaymentMethod, but I think it makes more sense to call deletePaymentMethod first because it uses the paymentMethod which will be cleared once the popover is hidden.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree

}}
onCancel={hideDefaultDeleteMenu}
contentStyles={!isSmallScreenWidth ? [styles.sidebarPopover, styles.willChangeTransform] : undefined}
Expand Down