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 add payment dynamic #7847

Merged
merged 5 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
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
37 changes: 24 additions & 13 deletions src/components/KYCWall/BaseKYCWall.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import {withOnyx} from 'react-native-onyx';
import {ActivityIndicator} from 'react-native';
import {ActivityIndicator, Dimensions} from 'react-native';
import themeColors from '../../styles/themes/default';
import CONST from '../../CONST';
import Navigation from '../../libs/Navigation/Navigation';
Expand All @@ -20,26 +20,40 @@ class KYCWall extends React.Component {
super(props);

this.continue = this.continue.bind(this);
this.setMenuPosition = this.setMenuPosition.bind(this);

this.state = {
shouldShowAddPaymentMenu: false,
anchorPositionTop: 0,
anchorPositionLeft: 0,
transferBalanceButton: null,
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure we need to put this in state? wouldn't a class variable suffice?

Copy link
Contributor Author

@phivh phivh Mar 1, 2022

Choose a reason for hiding this comment

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

@rushatgabhane The position of the menu belongs to position of the button that user clicking on. Thus, when use listener we need to re-assign this button when any click event happens, the possible way to re-assign any variables to the listener in this case needs to use state.

Copy link
Contributor

Choose a reason for hiding this comment

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

I know this is old, but I don't understand this explanation and it seems wrong. @rushatgabhane please try to address all comments before approving 🙇‍♂️

Copy link
Member

@rushatgabhane rushatgabhane Jul 16, 2022

Choose a reason for hiding this comment

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

@marcaaron im sorry about it (and i don't understand it either)

please try to address all comments before approving

yess!

};
}

componentDidMount() {
PaymentMethods.getPaymentMethods();
PaymentMethods.kycWallRef.current = this;
if (this.props.shouldListenForResize) {
this.dimensionsSubscription = Dimensions.addEventListener('change', this.setMenuPosition);
}
}

componentWillUnmount() {
if (this.props.shouldListenForResize) {
window.removeEventListener('resize', null);
if (this.props.shouldListenForResize && this.dimensionsSubscription) {
this.dimensionsSubscription.remove();
}
PaymentMethods.kycWallRef.current = null;
}

setMenuPosition() {
if (!this.state.transferBalanceButton) {
return;
}
const buttonPosition = getClickedElementLocation(this.state.transferBalanceButton);
const position = this.getAnchorPosition(buttonPosition);
this.setPositionAddPaymentMenu(position);
}

/**
* @param {DOMRect} domRect
* @returns {Object}
Expand Down Expand Up @@ -78,22 +92,19 @@ class KYCWall extends React.Component {
* @param {Event} event
*/
continue(event) {
this.setState({
transferBalanceButton: event.nativeEvent,
});

// Check to see if user has a valid payment method on file and display the add payment popover if they don't
if (!PaymentUtils.hasExpensifyPaymentMethod(this.props.cardList, this.props.bankAccountList)) {
Log.info('[KYC Wallet] User does not have valid payment method');
let clickedElementLocation = getClickedElementLocation(event.nativeEvent);
let position = this.getAnchorPosition(clickedElementLocation);
if (this.props.shouldListenForResize) {
window.addEventListener('resize', () => {
clickedElementLocation = getClickedElementLocation(event.nativeEvent);
position = this.getAnchorPosition(clickedElementLocation);
this.setPositionAddPaymentMenu(position);
});
}
const clickedElementLocation = getClickedElementLocation(event.nativeEvent);
const position = this.getAnchorPosition(clickedElementLocation);
this.setPositionAddPaymentMenu(position);
this.setState({
shouldShowAddPaymentMenu: true,
});
this.setPositionAddPaymentMenu(position);
return;
}

Expand Down
39 changes: 25 additions & 14 deletions src/pages/settings/Payments/PaymentsPage/BasePaymentsPage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import {View, TouchableOpacity} from 'react-native';
import {View, TouchableOpacity, Dimensions} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import PaymentMethodList from '../PaymentMethodList';
import ROUTES from '../../../../ROUTES';
Expand Down Expand Up @@ -42,6 +42,7 @@ class BasePaymentsPage extends React.Component {
formattedSelectedPaymentMethod: {},
anchorPositionTop: 0,
anchorPositionLeft: 0,
addPaymentMethodButton: null,
Copy link
Member

Choose a reason for hiding this comment

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

Same thing in this file. This isn't needed, and can be removed

Copy link
Contributor Author

@phivh phivh Mar 1, 2022

Choose a reason for hiding this comment

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

I've explained the reason for the comment below, it is the same.

};

this.paymentMethodPressed = this.paymentMethodPressed.bind(this);
Expand All @@ -52,10 +53,29 @@ class BasePaymentsPage extends React.Component {
this.deletePaymentMethod = this.deletePaymentMethod.bind(this);
this.hidePasswordPrompt = this.hidePasswordPrompt.bind(this);
this.navigateToTransferBalancePage = this.navigateToTransferBalancePage.bind(this);
this.setMenuPosition = this.setMenuPosition.bind(this);
}

componentDidMount() {
PaymentMethods.getPaymentMethods();
if (this.props.shouldListenForResize) {
this.dimensionsSubscription = Dimensions.addEventListener('change', this.setMenuPosition);
}
}

componentWillUnmount() {
if (!this.props.shouldListenForResize || !this.dimensionsSubscription) {
return;
}
this.dimensionsSubscription.remove();
}

setMenuPosition() {
if (!this.state.addPaymentMethodButton) {
return;
}
const buttonPosition = getClickedElementLocation(this.state.addPaymentMethodButton);
this.setPositionAddPaymentMenu(buttonPosition);
}

getSelectedPaymentMethodID() {
Expand Down Expand Up @@ -92,13 +112,10 @@ class BasePaymentsPage extends React.Component {
* @param {String} account
*/
paymentMethodPressed(nativeEvent, accountType, account) {
let position = getClickedElementLocation(nativeEvent);
if (this.props.shouldListenForResize) {
window.addEventListener('resize', () => {
position = getClickedElementLocation(nativeEvent);
this.setPositionAddPaymentMenu(position);
});
}
const position = getClickedElementLocation(nativeEvent);
this.setState({
addPaymentMethodButton: nativeEvent,
});
if (accountType) {
let formattedSelectedPaymentMethod;
if (accountType === CONST.PAYMENT_METHODS.PAYPAL) {
Expand Down Expand Up @@ -168,19 +185,13 @@ class BasePaymentsPage extends React.Component {
* Hide the add payment modal
*/
hideAddPaymentMenu() {
if (this.props.shouldListenForResize) {
window.removeEventListener('resize', null);
}
this.setState({shouldShowAddPaymentMenu: false});
}

/**
* Hide the default / delete modal
*/
hideDefaultDeleteMenu() {
if (this.props.shouldListenForResize) {
window.removeEventListener('resize', null);
}
this.setState({shouldShowDefaultDeleteMenu: false});
}

Expand Down