-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6651 from Expensify/marcaaron-payWithExpensifyButton
Refactor Settlement Button for reuse in both Details and Confirm screens
- Loading branch information
Showing
4 changed files
with
199 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import {withOnyx} from 'react-native-onyx'; | ||
import ButtonWithMenu from './ButtonWithMenu'; | ||
import * as Expensicons from './Icon/Expensicons'; | ||
import Permissions from '../libs/Permissions'; | ||
import isAppInstalled from '../libs/isAppInstalled'; | ||
import * as ValidationUtils from '../libs/ValidationUtils'; | ||
import makeCancellablePromise from '../libs/MakeCancellablePromise'; | ||
import ONYXKEYS from '../ONYXKEYS'; | ||
import CONST from '../CONST'; | ||
import compose from '../libs/compose'; | ||
import withLocalize, {withLocalizePropTypes} from './withLocalize'; | ||
|
||
const propTypes = { | ||
/** Settlement currency type */ | ||
currency: PropTypes.string, | ||
|
||
/** Should we show paypal option */ | ||
shouldShowPaypal: PropTypes.bool, | ||
|
||
/** Associated phone login for the person we are sending money to */ | ||
recipientPhoneNumber: PropTypes.string, | ||
|
||
...withLocalizePropTypes, | ||
}; | ||
|
||
const defaultProps = { | ||
currency: CONST.CURRENCY.USD, | ||
recipientPhoneNumber: '', | ||
shouldShowPaypal: false, | ||
}; | ||
|
||
class SettlementButton extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
const buttonOptions = []; | ||
|
||
if (props.currency === CONST.CURRENCY.USD && Permissions.canUsePayWithExpensify(props.betas) && Permissions.canUseWallet(props.betas)) { | ||
buttonOptions.push({ | ||
text: props.translate('iou.settleExpensify'), | ||
icon: Expensicons.Wallet, | ||
value: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, | ||
}); | ||
} | ||
|
||
if (props.shouldShowPaypal) { | ||
buttonOptions.push({ | ||
text: props.translate('iou.settlePaypalMe'), | ||
icon: Expensicons.PayPal, | ||
value: CONST.IOU.PAYMENT_TYPE.PAYPAL_ME, | ||
}); | ||
} | ||
|
||
buttonOptions.push({ | ||
text: props.translate('iou.settleElsewhere'), | ||
icon: Expensicons.Cash, | ||
value: CONST.IOU.PAYMENT_TYPE.ELSEWHERE, | ||
}); | ||
|
||
// Venmo requires an async call to the native layer to determine availability and will be added as an option if available. | ||
this.checkVenmoAvailabilityPromise = null; | ||
|
||
this.state = { | ||
buttonOptions, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
this.addVenmoPaymentOptionToMenu(); | ||
} | ||
|
||
componentWillUnmount() { | ||
if (!this.checkVenmoAvailabilityPromise) { | ||
return; | ||
} | ||
|
||
this.checkVenmoAvailabilityPromise.cancel(); | ||
this.checkVenmoAvailabilityPromise = null; | ||
} | ||
|
||
/** | ||
* @returns {Boolean} | ||
*/ | ||
doesRecipientHaveValidPhoneLogin() { | ||
return this.props.recipientPhoneNumber && ValidationUtils.isValidUSPhone(this.props.recipientPhoneNumber); | ||
} | ||
|
||
/** | ||
* Adds Venmo, if available, as the second option in the menu of payment options | ||
*/ | ||
addVenmoPaymentOptionToMenu() { | ||
if (this.props.currency !== CONST.CURRENCY.USD || !this.doesRecipientHaveValidPhoneLogin()) { | ||
return; | ||
} | ||
|
||
this.checkVenmoAvailabilityPromise = makeCancellablePromise(isAppInstalled('venmo')); | ||
this.checkVenmoAvailabilityPromise | ||
.promise | ||
.then((isVenmoInstalled) => { | ||
if (!isVenmoInstalled) { | ||
return; | ||
} | ||
|
||
this.setState(prevState => ({ | ||
buttonOptions: [...prevState.buttonOptions.slice(0, 1), | ||
{ | ||
text: this.props.translate('iou.settleVenmo'), | ||
icon: Expensicons.Venmo, | ||
value: CONST.IOU.PAYMENT_TYPE.VENMO, | ||
}, | ||
...prevState.buttonOptions.slice(1), | ||
], | ||
})); | ||
}); | ||
} | ||
|
||
render() { | ||
return ( | ||
<ButtonWithMenu | ||
isDisabled={this.props.isDisabled} | ||
isLoading={this.props.isLoading} | ||
onPress={this.props.onPress} | ||
options={this.state.buttonOptions} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
SettlementButton.propTypes = propTypes; | ||
SettlementButton.defaultProps = defaultProps; | ||
|
||
export default compose( | ||
withLocalize, | ||
withOnyx({ | ||
betas: { | ||
key: ONYXKEYS.BETAS, | ||
}, | ||
}), | ||
)(SettlementButton); |
Oops, something went wrong.