-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathIOUModal.js
executable file
·374 lines (336 loc) · 13.8 KB
/
IOUModal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import React, {Component} from 'react';
import {View, TouchableOpacity} from 'react-native';
import PropTypes from 'prop-types';
import lodashGet from 'lodash/get';
import {withOnyx} from 'react-native-onyx';
import IOUAmountPage from './steps/IOUAmountPage';
import IOUParticipantsPage from './steps/IOUParticipantsPage';
import IOUConfirmPage from './steps/IOUConfirmPage';
import Header from '../../components/Header';
import styles from '../../styles/styles';
import Icon from '../../components/Icon';
import {createIOUSplit, createIOUTransaction, getPreferredCurrency} from '../../libs/actions/IOU';
import {Close, BackArrow} from '../../components/Icon/Expensicons';
import Navigation from '../../libs/Navigation/Navigation';
import ONYXKEYS from '../../ONYXKEYS';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import compose from '../../libs/compose';
import {getPersonalDetailsForLogins} from '../../libs/OptionsListUtils';
import FullScreenLoadingIndicator from '../../components/FullscreenLoadingIndicator';
import ScreenWrapper from '../../components/ScreenWrapper';
import CONST from '../../CONST';
import KeyboardAvoidingView from '../../libs/KeyboardAvoidingView';
/**
* IOU modal for requesting money and splitting bills.
*/
const propTypes = {
/** Whether the IOU is for a single request or a group bill split */
hasMultipleParticipants: PropTypes.bool,
/** The report passed via the route */
report: PropTypes.shape({
/** Participants associated with current report */
participants: PropTypes.arrayOf(PropTypes.string),
}),
// The personal details of the person who is logged in
myPersonalDetails: PropTypes.shape({
// Preferred Currency Code of the current user
preferredCurrencyCode: PropTypes.string,
// Currency Symbol of the Preferred Currency
preferredCurrencySymbol: PropTypes.string,
}),
// Holds data related to IOU view state, rather than the underlying IOU data.
iou: PropTypes.shape({
/** Whether or not transaction creation has started */
creatingIOUTransaction: PropTypes.bool,
/** Whether or not transaction creation has resulted to error */
error: PropTypes.bool,
// is loading
loading: PropTypes.bool,
}).isRequired,
/** Personal details of all the users */
personalDetails: PropTypes.shape({
/** Primary login of participant */
login: PropTypes.string,
/** Display Name of participant */
displayName: PropTypes.string,
/** Avatar url of participant */
avatar: PropTypes.string,
}).isRequired,
...withLocalizePropTypes,
};
const defaultProps = {
hasMultipleParticipants: false,
report: {
participants: [],
},
myPersonalDetails: {
preferredCurrencyCode: CONST.CURRENCY.USD,
preferredCurrencySymbol: '$',
},
};
// Determines type of step to display within Modal, value provides the title for that page.
const Steps = {
IOUAmount: 'iou.amount',
IOUParticipants: 'iou.participants',
IOUConfirm: 'iou.confirm',
};
class IOUModal extends Component {
constructor(props) {
super(props);
this.navigateToPreviousStep = this.navigateToPreviousStep.bind(this);
this.navigateToNextStep = this.navigateToNextStep.bind(this);
this.currencySelected = this.currencySelected.bind(this);
this.addParticipants = this.addParticipants.bind(this);
this.createTransaction = this.createTransaction.bind(this);
this.updateComment = this.updateComment.bind(this);
this.addParticipants = this.addParticipants.bind(this);
this.getReady = this.getReady.bind(this);
const participants = lodashGet(props, 'report.participants', []);
const participantsWithDetails = getPersonalDetailsForLogins(participants, props.personalDetails)
.map(personalDetails => ({
login: personalDetails.login,
text: personalDetails.displayName,
alternateText: personalDetails.login,
icons: [personalDetails.avatar],
keyForList: personalDetails.login,
}));
this.state = {
currentStepIndex: 0,
participants: participantsWithDetails,
// amount is currency in decimal format
amount: '',
selectedCurrency: {
currencyCode: props.myPersonalDetails.preferredCurrencyCode,
currencySymbol: props.myPersonalDetails.preferredCurrencySymbol,
},
comment: '',
};
// Skip IOUParticipants step if participants are passed in
if (participants.length) {
// The steps to be shown within the create IOU flow.
this.steps = [Steps.IOUAmount, Steps.IOUConfirm];
} else {
this.steps = [Steps.IOUAmount, Steps.IOUParticipants, Steps.IOUConfirm];
}
}
componentDidUpdate(prevProps) {
// Successfully close the modal if transaction creation has ended and there is no error
if (prevProps.iou.creatingIOUTransaction && !this.props.iou.creatingIOUTransaction && !this.props.iou.error) {
Navigation.dismissModal();
}
if (prevProps.myPersonalDetails.preferredCurrencyCode
!== this.props.myPersonalDetails.preferredCurrencyCode) {
this.updateSelectedCurrency({
currencyCode: this.props.myPersonalDetails.preferredCurrencyCode,
currencySymbol: this.props.myPersonalDetails.preferredCurrencySymbol,
});
}
}
getReady() {
getPreferredCurrency();
}
/**
* Retrieve title for current step, based upon current step and type of IOU
*
* @returns {String}
*/
getTitleForStep() {
const currentStepIndex = this.state.currentStepIndex;
if (currentStepIndex === 1 || currentStepIndex === 2) {
return this.props.translate(
this.props.hasMultipleParticipants ? 'iou.split' : 'iou.request', {
amount: this.props.numberFormat(
this.state.amount, {
style: 'currency',
currency: this.state.selectedCurrency.currencyCode,
},
),
},
);
}
if (currentStepIndex === 0) {
return this.props.translate(this.props.hasMultipleParticipants ? 'iou.splitBill' : 'iou.requestMoney');
}
return this.props.translate(this.steps[currentStepIndex]) || '';
}
addParticipants(participants) {
this.setState({
participants,
});
}
/**
* Update the selected currency
* @param {Object} selectedCurrency
*/
updateSelectedCurrency(selectedCurrency) {
this.setState({
selectedCurrency,
});
}
/**
* Navigate to the next IOU step if possible
*/
navigateToPreviousStep() {
if (this.state.currentStepIndex <= 0) {
return;
}
this.setState(prevState => ({
currentStepIndex: prevState.currentStepIndex - 1,
}));
}
/**
* Navigate to the previous IOU step if possible
*/
navigateToNextStep() {
if (this.state.currentStepIndex >= this.steps.length - 1) {
return;
}
this.setState(prevState => ({
currentStepIndex: prevState.currentStepIndex + 1,
}));
}
/**
* Update comment whenever user enters any new text
*
* @param {String} comment
*/
updateComment(comment) {
this.setState({
comment,
});
}
/**
* Update the currency state
*
* @param {String} selectedCurrency
*/
currencySelected(selectedCurrency) {
this.setState({selectedCurrency});
}
/**
* @param {Array} [splits]
*/
createTransaction(splits) {
if (splits) {
createIOUSplit({
comment: this.state.comment,
// should send in cents to API
amount: this.state.amount * 100,
currency: this.state.selectedCurrency.currencyCode,
splits,
});
return;
}
createIOUTransaction({
comment: this.state.comment,
// should send in cents to API
amount: this.state.amount * 100,
currency: this.state.selectedCurrency.currencyCode,
debtorEmail: this.state.participants[0].login,
});
}
render() {
const currentStep = this.steps[this.state.currentStepIndex];
return (
<ScreenWrapper onTransitionEnd={this.getReady}>
{({didScreenTransitionEnd}) => (
<KeyboardAvoidingView>
<View style={[styles.headerBar]}>
<View style={[
styles.dFlex,
styles.flexRow,
styles.alignItemsCenter,
styles.flexGrow1,
styles.justifyContentBetween,
styles.overflowHidden,
]}
>
{this.state.currentStepIndex > 0
&& (
<TouchableOpacity
onPress={this.navigateToPreviousStep}
style={[styles.touchableButtonImage]}
>
<Icon src={BackArrow} />
</TouchableOpacity>
)}
<Header title={this.getTitleForStep()} />
<View style={[styles.reportOptions, styles.flexRow]}>
<TouchableOpacity
onPress={() => Navigation.dismissModal()}
style={[styles.touchableButtonImage]}
>
<Icon src={Close} />
</TouchableOpacity>
</View>
</View>
</View>
<View style={[styles.pRelative, styles.flex1]}>
<FullScreenLoadingIndicator visible={!didScreenTransitionEnd} />
{didScreenTransitionEnd && (
<>
{currentStep === Steps.IOUAmount && (
<IOUAmountPage
onStepComplete={(amount) => {
this.setState({amount});
this.navigateToNextStep();
}}
currencySelected={this.currencySelected}
reportID={this.props.route.params.reportID}
selectedCurrency={this.state.selectedCurrency}
hasMultipleParticipants={this.props.hasMultipleParticipants}
selectedAmount={this.state.amount}
navigation={this.props.navigation}
/>
)}
{currentStep === Steps.IOUParticipants && (
<IOUParticipantsPage
participants={this.state.participants}
hasMultipleParticipants={this.props.hasMultipleParticipants}
onAddParticipants={this.addParticipants}
onStepComplete={this.navigateToNextStep}
/>
)}
{currentStep === Steps.IOUConfirm && (
<IOUConfirmPage
onConfirm={this.createTransaction}
hasMultipleParticipants={this.props.hasMultipleParticipants}
participants={this.state.participants}
iouAmount={this.state.amount}
comment={this.state.comment}
selectedCurrency={this.state.selectedCurrency}
onUpdateComment={this.updateComment}
/>
)}
</>
)}
</View>
</KeyboardAvoidingView>
)}
</ScreenWrapper>
);
}
}
IOUModal.propTypes = propTypes;
IOUModal.defaultProps = defaultProps;
IOUModal.displayName = 'IOUModal';
export default compose(
withLocalize,
withOnyx({
report: {
key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${route.params.reportID}`,
},
iousReport: {
key: ONYXKEYS.COLLECTION.REPORT_IOUS,
},
iou: {
key: ONYXKEYS.IOU,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS,
},
myPersonalDetails: {
key: ONYXKEYS.MY_PERSONAL_DETAILS,
},
}),
)(IOUModal);