-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Adjusting IOUModal and adding createAndPayIOU #4329
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
Changes from all commits
2d770ad
2b3c4f7
ffbbddf
ff11fb3
84a8d0c
61c76ef
4bd4f9a
8b17843
8dda6b4
cc50eda
7a3d074
a20bdef
409b848
bef057a
26f87d6
9126504
b4a2c30
307440e
4457af8
341901e
eda967b
61b0a14
5dee09f
9669dd5
eadda9b
421fd84
bab792b
c55e984
6bbe303
761d4ad
f9728e3
75e221d
bbe044c
8c710a3
0642a21
2a21ec4
383cb6b
a91d03e
e18a4bf
58cc279
77f5ca2
634d46a
f1da312
bbbc077
6cda8c1
d3e64d6
1517e50
86e8b4f
ad644f3
9c70be0
fc9125e
58da027
84ed8cc
92bbf3a
5ee20f7
2054d30
7cfa7ab
f43b988
821c90c
3cc4fa6
40b1e55
2db4946
c4ca01e
73937b2
ca34f19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -111,6 +111,7 @@ class IOUModal extends Component { | |||
| this.addParticipants = this.addParticipants.bind(this); | ||||
| this.createTransaction = this.createTransaction.bind(this); | ||||
| this.updateComment = this.updateComment.bind(this); | ||||
| this.sendMoney = this.sendMoney.bind(this); | ||||
| const participants = lodashGet(props, 'report.participants', []); | ||||
| const participantsWithDetails = _.map(OptionsListUtils.getPersonalDetailsForLogins(participants, props.personalDetails), personalDetails => ({ | ||||
| login: personalDetails.login, | ||||
|
|
@@ -121,8 +122,6 @@ class IOUModal extends Component { | |||
| payPalMeAddress: lodashGet(personalDetails, 'payPalMeAddress', ''), | ||||
| phoneNumber: lodashGet(personalDetails, 'phoneNumber', ''), | ||||
| })); | ||||
| this.isSendRequest = props.iouType === CONST.IOU.IOU_TYPE.SEND; | ||||
| this.hasGoldWallet = props.userWallet.tierName && props.userWallet.tiername === CONST.WALLET.TIER_NAME.GOLD; | ||||
|
|
||||
| this.state = { | ||||
| previousStepIndex: 0, | ||||
|
|
@@ -193,14 +192,15 @@ class IOUModal extends Component { | |||
| */ | ||||
| getTitleForStep() { | ||||
| const currentStepIndex = this.state.currentStepIndex; | ||||
| const isSendingMoney = this.props.iouType === CONST.IOU.IOU_TYPE.SEND; | ||||
| if (currentStepIndex === 1 || currentStepIndex === 2) { | ||||
| const formattedAmount = this.props.numberFormat( | ||||
| this.state.amount, { | ||||
| style: 'currency', | ||||
| currency: this.props.iou.selectedCurrencyCode, | ||||
| }, | ||||
| ); | ||||
| if (this.isSendRequest) { | ||||
| if (isSendingMoney) { | ||||
| return this.props.translate('iou.send', { | ||||
| amount: formattedAmount, | ||||
| }); | ||||
|
|
@@ -212,7 +212,7 @@ class IOUModal extends Component { | |||
| ); | ||||
| } | ||||
| if (currentStepIndex === 0) { | ||||
| if (this.isSendRequest) { | ||||
| if (isSendingMoney) { | ||||
| return this.props.translate('iou.sendMoney'); | ||||
| } | ||||
| return this.props.translate(this.props.hasMultipleParticipants ? 'iou.splitBill' : 'iou.requestMoney'); | ||||
|
|
@@ -221,6 +221,22 @@ class IOUModal extends Component { | |||
| return this.props.translate(this.steps[currentStepIndex]) || ''; | ||||
| } | ||||
|
|
||||
| /** | ||||
| * Update comment whenever user enters any new text | ||||
| * | ||||
| * @param {String} comment | ||||
| */ | ||||
| updateComment(comment) { | ||||
| this.setState({ | ||||
| comment, | ||||
| }); | ||||
| } | ||||
|
|
||||
| /** | ||||
| * Update participants whenever user selects the payment recipient | ||||
| * | ||||
| * @param {Array} participants | ||||
| */ | ||||
| addParticipants(participants) { | ||||
| this.setState({ | ||||
| participants, | ||||
|
|
@@ -247,35 +263,61 @@ class IOUModal extends Component { | |||
| if (this.state.currentStepIndex >= this.steps.length - 1) { | ||||
| return; | ||||
| } | ||||
|
|
||||
| this.setState(prevState => ({ | ||||
| previousStepIndex: prevState.currentStepIndex, | ||||
| currentStepIndex: prevState.currentStepIndex + 1, | ||||
| })); | ||||
| } | ||||
|
|
||||
| /** | ||||
| * Update comment whenever user enters any new text | ||||
| * Checks if user has a GOLD wallet then creates a paid IOU report on the fly | ||||
| * | ||||
| * @param {String} comment | ||||
| * @param {String} paymentMethodType | ||||
| */ | ||||
| updateComment(comment) { | ||||
| this.setState({ | ||||
| sendMoney(paymentMethodType) { | ||||
| const hasGoldWallet = this.props.userWallet.tierName && this.props.userWallet.tierName === CONST.WALLET.TIER_NAME.GOLD; | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The section |
||||
|
|
||||
| // If the user is trying to send money, then they need to upgrade to a GOLD wallet | ||||
| if (!hasGoldWallet) { | ||||
| Navigation.navigate(ROUTES.IOU_ENABLE_PAYMENTS); | ||||
| return; | ||||
| } | ||||
|
|
||||
| const amount = Math.round(this.state.amount * 100); | ||||
| const currency = this.props.iou.selectedCurrencyCode; | ||||
| const comment = this.state.comment; | ||||
|
|
||||
| const newIOUReportDetails = JSON.stringify({ | ||||
| amount, | ||||
| currency, | ||||
| requestorEmail: this.state.participants[0].login, | ||||
| comment, | ||||
| idempotencyKey: Str.guid(), | ||||
| }); | ||||
|
|
||||
| IOU.payIOUReport({ | ||||
| chatReportID: lodashGet(this.props, 'route.params.reportID', ''), | ||||
| reportID: 0, | ||||
| paymentMethodType, | ||||
| amount, | ||||
| currency, | ||||
| requestorPayPalMeAddress: this.state.participants[0].payPalMeAddress, | ||||
| requestorPhoneNumber: this.state.participants[0].phoneNumber, | ||||
| comment, | ||||
| newIOUReportDetails, | ||||
| shouldRedirectToChatReport: true, | ||||
| }); | ||||
|
Comment on lines
+278
to
310
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically moved the minimal
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great summary of the thought process here. Thanks! Happy to leave things as they are for now, but just wanted to give this one last exploration as problems like these will pop up in the future and how we handle them sets the precedent for others to follow.
It might be reasonable to extract this into an "action" regardless of code re-use benefits. It will help separate the "action" logic from the view, keep things organized, and we get the benefit of replacing
Could we still follow this philosophy if we create a new action? Why does this rule exist?
What if we returned the promise here instead of wrapping it with a promise? Line 281 in ca34f19
Calling function sendMoneyAndNavigateToReport(params) {
payIOUReport(params)
.finally(() => {
Navigation.navigate(ROUTES.REPORT);
});
}
An advantage of navigating from outside this method (instead of within it) is that we pass the responsibility to the caller of the action and reduce the overall responsibility of the action itself. When will
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I agree with this.
Yes we could, and I believe this rule exists so we don't introduce any side effects within views (views should be responsible for displaying data is my understanding).
I guess I mostly struggled with how to make this work nicely with
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry bout that, didn't notice
export default function asyncOpenURL(promise, url) {
if (!url) {
return promise;
}
return promise.then((params) => {
Linking.openURL(typeof url === 'string' ? url : url(params));
});
}
payIOUPromise.then((response) => {...});
asyncOpenURL(payIOUPromise, url);
return payIOUPromise; |
||||
| } | ||||
|
|
||||
| /** | ||||
| * Create the IOU transaction | ||||
| * | ||||
| * @param {Array} [splits] | ||||
| */ | ||||
| createTransaction(splits) { | ||||
| const reportID = lodashGet(this.props, 'route.params.reportID', ''); | ||||
|
|
||||
| // If the user is trying to send money, then they need to upgrade to a GOLD wallet | ||||
| if (this.isSendRequest && !this.hasGoldWallet) { | ||||
| Navigation.navigate(ROUTES.IOU_ENABLE_PAYMENTS); | ||||
| return; | ||||
| } | ||||
|
|
||||
| // Only splits from a group DM has a reportID | ||||
| // Check if reportID is a number | ||||
| if (splits && CONST.REGEX.NUMBER.test(reportID)) { | ||||
|
|
@@ -290,11 +332,12 @@ class IOUModal extends Component { | |||
| }); | ||||
| return; | ||||
| } | ||||
|
|
||||
| if (splits) { | ||||
| IOU.createIOUSplit({ | ||||
| comment: this.state.comment, | ||||
|
|
||||
| // should send in cents to API | ||||
| // Send in cents to API. | ||||
| amount: Math.round(this.state.amount * 100), | ||||
| currency: this.props.iou.selectedCurrencyCode, | ||||
| splits, | ||||
|
|
@@ -305,7 +348,7 @@ class IOUModal extends Component { | |||
| IOU.createIOUTransaction({ | ||||
| comment: this.state.comment, | ||||
|
|
||||
| // should send in cents to API | ||||
| // Send in cents to API. | ||||
| amount: Math.round(this.state.amount * 100), | ||||
| currency: this.props.iou.selectedCurrencyCode, | ||||
| debtorEmail: OptionsListUtils.addSMSDomainIfPhoneNumber(this.state.participants[0].login), | ||||
|
|
@@ -398,13 +441,13 @@ class IOUModal extends Component { | |||
| > | ||||
| <IOUConfirmPage | ||||
| onConfirm={this.createTransaction} | ||||
| onSendMoney={this.sendMoney} | ||||
| hasMultipleParticipants={this.props.hasMultipleParticipants} | ||||
| participants={this.state.participants} | ||||
| iouAmount={this.state.amount} | ||||
| comment={this.state.comment} | ||||
| onUpdateComment={this.updateComment} | ||||
| iouType={this.props.iouType} | ||||
| localCurrencyCode={this.props.myPersonalDetails.localCurrencyCode} | ||||
| isGroupSplit={this.steps.length === 2} | ||||
| /> | ||||
| </AnimatedStep> | ||||
|
|
||||
Uh oh!
There was an error while loading. Please reload this page.