-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
MoneyRequestPreview.js
399 lines (350 loc) · 18 KB
/
MoneyRequestPreview.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import {truncate} from 'lodash';
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import MoneyRequestSkeletonView from '@components/MoneyRequestSkeletonView';
import MultipleAvatars from '@components/MultipleAvatars';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import PressableWithFeedback from '@components/Pressable/PressableWithoutFeedback';
import refPropTypes from '@components/refPropTypes';
import RenderHTML from '@components/RenderHTML';
import {showContextMenuForReport} from '@components/ShowContextMenuContext';
import Text from '@components/Text';
import transactionPropTypes from '@components/transactionPropTypes';
import useLocalize from '@hooks/useLocalize';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import ControlSelection from '@libs/ControlSelection';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import * as IOUUtils from '@libs/IOUUtils';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as ReceiptUtils from '@libs/ReceiptUtils';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import walletTermsPropTypes from '@pages/EnablePayments/walletTermsPropTypes';
import reportActionPropTypes from '@pages/home/report/reportActionPropTypes';
import iouReportPropTypes from '@pages/iouReportPropTypes';
import reportPropTypes from '@pages/reportPropTypes';
import * as PaymentMethods from '@userActions/PaymentMethods';
import * as Report from '@userActions/Report';
import CONST from '@src/CONST';
import * as Localize from '@src/libs/Localize';
import ONYXKEYS from '@src/ONYXKEYS';
import ReportActionItemImages from './ReportActionItemImages';
const propTypes = {
/** The active IOUReport, used for Onyx subscription */
// eslint-disable-next-line react/no-unused-prop-types
iouReportID: PropTypes.string.isRequired,
/** The associated chatReport */
chatReportID: PropTypes.string.isRequired,
/** Callback for the preview pressed */
onPreviewPressed: PropTypes.func,
/** All the data of the action, used for showing context menu */
action: PropTypes.shape(reportActionPropTypes),
/** Popover context menu anchor, used for showing context menu */
contextMenuAnchor: refPropTypes,
/** Callback for updating context menu active state, used for showing context menu */
checkIfContextMenuActive: PropTypes.func,
/** Extra styles to pass to View wrapper */
// eslint-disable-next-line react/forbid-prop-types
containerStyles: PropTypes.arrayOf(PropTypes.object),
/* Onyx Props */
/** chatReport associated with iouReport */
chatReport: reportPropTypes,
/** IOU report data object */
iouReport: iouReportPropTypes,
/** True if this is this IOU is a split instead of a 1:1 request */
isBillSplit: PropTypes.bool.isRequired,
/** True if the IOU Preview card is hovered */
isHovered: PropTypes.bool,
/** All of the personal details for everyone */
personalDetails: PropTypes.objectOf(
PropTypes.shape({
/** This is either the user's full name, or their login if full name is an empty string */
displayName: PropTypes.string,
}),
),
/** The transaction attached to the action.message.iouTransactionID */
transaction: transactionPropTypes,
/** Session info for the currently logged in user. */
session: PropTypes.shape({
/** Currently logged in user email */
email: PropTypes.string,
}),
/** Information about the user accepting the terms for payments */
walletTerms: walletTermsPropTypes,
/** Whether or not an IOU report contains money requests in a different currency
* that are either created or cancelled offline, and thus haven't been converted to the report's currency yet
*/
shouldShowPendingConversionMessage: PropTypes.bool,
/** Whether a message is a whisper */
isWhisper: PropTypes.bool,
};
const defaultProps = {
iouReport: {},
onPreviewPressed: null,
action: undefined,
contextMenuAnchor: undefined,
checkIfContextMenuActive: () => {},
containerStyles: [],
walletTerms: {},
chatReport: {},
isHovered: false,
personalDetails: {},
session: {
email: null,
},
transaction: {},
shouldShowPendingConversionMessage: false,
isWhisper: false,
};
function MoneyRequestPreview(props) {
const theme = useTheme();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();
const {isSmallScreenWidth, windowWidth} = useWindowDimensions();
const parser = new ExpensiMark();
if (_.isEmpty(props.iouReport) && !props.isBillSplit) {
return null;
}
const sessionAccountID = lodashGet(props.session, 'accountID', null);
const managerID = props.iouReport.managerID || '';
const ownerAccountID = props.iouReport.ownerAccountID || '';
const isPolicyExpenseChat = ReportUtils.isPolicyExpenseChat(props.chatReport);
const participantAccountIDs = props.isBillSplit ? lodashGet(props.action, 'originalMessage.participantAccountIDs', []) : [managerID, ownerAccountID];
const participantAvatars = OptionsListUtils.getAvatarsForAccountIDs(participantAccountIDs, props.personalDetails);
const sortedParticipantAvatars = _.sortBy(participantAvatars, (avatar) => avatar.id);
if (isPolicyExpenseChat && props.isBillSplit) {
sortedParticipantAvatars.push(ReportUtils.getWorkspaceIcon(props.chatReport));
}
// Pay button should only be visible to the manager of the report.
const isCurrentUserManager = managerID === sessionAccountID;
const {amount: requestAmount, currency: requestCurrency, comment: requestComment, merchant} = ReportUtils.getTransactionDetails(props.transaction);
const description = truncate(requestComment, {length: CONST.REQUEST_PREVIEW.MAX_LENGTH});
const requestMerchant = truncate(merchant, {length: CONST.REQUEST_PREVIEW.MAX_LENGTH});
const hasReceipt = TransactionUtils.hasReceipt(props.transaction);
const isScanning = hasReceipt && TransactionUtils.isReceiptBeingScanned(props.transaction);
const hasFieldErrors = TransactionUtils.hasMissingSmartscanFields(props.transaction);
const isDistanceRequest = TransactionUtils.isDistanceRequest(props.transaction);
const isExpensifyCardTransaction = TransactionUtils.isExpensifyCardTransaction(props.transaction);
const isSettled = ReportUtils.isSettled(props.iouReport.reportID);
const isDeleted = lodashGet(props.action, 'pendingAction', null) === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE;
// Show the merchant for IOUs and expenses only if they are custom or not related to scanning smartscan
const shouldShowMerchant = !_.isEmpty(requestMerchant) && requestMerchant !== CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT && requestMerchant !== CONST.TRANSACTION.DEFAULT_MERCHANT;
const shouldShowDescription = !_.isEmpty(description) && !shouldShowMerchant && !isScanning;
const hasPendingWaypoints = lodashGet(props.transaction, 'pendingFields.waypoints', null);
let merchantOrDescription = requestMerchant;
if (!shouldShowMerchant) {
merchantOrDescription = description || '';
} else if (hasPendingWaypoints) {
merchantOrDescription = requestMerchant.replace(CONST.REGEX.FIRST_SPACE, translate('common.tbd'));
}
const receiptImages = hasReceipt ? [ReceiptUtils.getThumbnailAndImageURIs(props.transaction)] : [];
const getSettledMessage = () => {
if (isExpensifyCardTransaction) {
return translate('common.done');
}
return translate('iou.settledExpensify');
};
const showContextMenu = (event) => {
showContextMenuForReport(event, props.contextMenuAnchor, props.chatReportID, props.action, props.checkIfContextMenuActive);
};
const getPreviewHeaderText = () => {
if (isDistanceRequest) {
return translate('common.distance');
}
if (isScanning) {
return translate('common.receipt');
}
if (props.isBillSplit) {
return translate('iou.split');
}
if (isExpensifyCardTransaction) {
let message = translate('iou.card');
if (TransactionUtils.isPending(props.transaction)) {
message += ` • ${translate('iou.pending')}`;
}
return message;
}
let message = translate('iou.cash');
if (ReportUtils.isPaidGroupPolicyExpenseReport(props.iouReport) && ReportUtils.isReportApproved(props.iouReport) && !ReportUtils.isSettled(props.iouReport)) {
message += ` • ${translate('iou.approved')}`;
} else if (props.iouReport.isWaitingOnBankAccount) {
message += ` • ${translate('iou.pending')}`;
} else if (props.iouReport.isCancelledIOU) {
message += ` • ${translate('iou.canceled')}`;
}
return message;
};
const getDisplayAmountText = () => {
if (isDistanceRequest) {
return requestAmount && !hasPendingWaypoints ? CurrencyUtils.convertToDisplayString(requestAmount, requestCurrency) : translate('common.tbd');
}
if (isScanning) {
return translate('iou.receiptScanning');
}
if (!isSettled && TransactionUtils.hasMissingSmartscanFields(props.transaction)) {
return Localize.translateLocal('iou.receiptMissingDetails');
}
return CurrencyUtils.convertToDisplayString(requestAmount, requestCurrency);
};
const getDisplayDeleteAmountText = () => {
const {amount, currency} = ReportUtils.getTransactionDetails(props.action.originalMessage);
if (isDistanceRequest) {
return CurrencyUtils.convertToDisplayString(TransactionUtils.getAmount(props.action.originalMessage), currency);
}
return CurrencyUtils.convertToDisplayString(amount, currency);
};
const displayAmount = isDeleted ? getDisplayDeleteAmountText() : getDisplayAmountText();
const childContainer = (
<View>
<OfflineWithFeedback
errors={props.walletTerms.errors}
onClose={() => {
PaymentMethods.clearWalletTermsError();
Report.clearIOUError(props.chatReportID);
}}
errorRowStyles={[styles.mbn1]}
needsOffscreenAlphaCompositing
>
<View
style={[
isScanning || props.isWhisper ? [styles.reportPreviewBoxHoverBorder, styles.reportContainerBorderRadius] : undefined,
!props.onPreviewPressed ? [styles.moneyRequestPreviewBox, ...props.containerStyles] : {},
]}
>
{hasReceipt && (
<ReportActionItemImages
images={receiptImages}
isHovered={props.isHovered || isScanning}
size={1}
/>
)}
{_.isEmpty(props.transaction) &&
!ReportActionsUtils.isMessageDeleted(props.action) &&
lodashGet(props.action, 'pendingAction') !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE ? (
<MoneyRequestSkeletonView />
) : (
<View style={styles.moneyRequestPreviewBoxText}>
<View style={[styles.flexRow]}>
<Text style={[styles.textLabelSupporting, styles.flex1, styles.lh20, styles.mb1]}>
{getPreviewHeaderText() + (isSettled && !props.iouReport.isCancelledIOU ? ` • ${getSettledMessage()}` : '')}
</Text>
{!isSettled && hasFieldErrors && (
<Icon
src={Expensicons.DotIndicator}
fill={theme.danger}
/>
)}
</View>
<View style={[styles.flexRow]}>
<View style={[styles.flex1, styles.flexRow, styles.alignItemsCenter]}>
<Text
style={[
styles.textHeadline,
props.isBillSplit &&
StyleUtils.getAmountFontSizeAndLineHeight(isSmallScreenWidth, windowWidth, displayAmount.length, sortedParticipantAvatars.length),
isDeleted && styles.lineThrough,
]}
numberOfLines={1}
>
{displayAmount}
</Text>
{ReportUtils.isSettled(props.iouReport.reportID) && !props.isBillSplit && (
<View style={styles.defaultCheckmarkWrapper}>
<Icon
src={Expensicons.Checkmark}
fill={theme.iconSuccessFill}
/>
</View>
)}
</View>
{props.isBillSplit && (
<View style={styles.moneyRequestPreviewBoxAvatar}>
<MultipleAvatars
icons={sortedParticipantAvatars}
shouldStackHorizontally
size="small"
isHovered={props.isHovered}
shouldUseCardBackground
/>
</View>
)}
</View>
<View style={[styles.flexRow, styles.mt1]}>
<View style={[styles.flex1]}>
{!isCurrentUserManager && props.shouldShowPendingConversionMessage && (
<Text style={[styles.textLabel, styles.colorMuted]}>{translate('iou.pendingConversionMessage')}</Text>
)}
{shouldShowDescription && <RenderHTML html={parser.replace(merchantOrDescription)} />}
{shouldShowMerchant && <Text style={[styles.textLabelSupporting, styles.textNormal]}>{merchantOrDescription}</Text>}
</View>
{props.isBillSplit && !_.isEmpty(participantAccountIDs) && requestAmount > 0 && (
<Text style={[styles.textLabel, styles.colorMuted, styles.ml1, styles.amountSplitPadding]}>
{translate('iou.amountEach', {
amount: CurrencyUtils.convertToDisplayString(
IOUUtils.calculateAmount(isPolicyExpenseChat ? 1 : participantAccountIDs.length - 1, requestAmount, requestCurrency),
requestCurrency,
),
})}
</Text>
)}
</View>
</View>
)}
</View>
</OfflineWithFeedback>
</View>
);
if (!props.onPreviewPressed) {
return childContainer;
}
const shouldDisableOnPress = props.isBillSplit && _.isEmpty(props.transaction);
return (
<PressableWithFeedback
onPress={shouldDisableOnPress ? undefined : props.onPreviewPressed}
onPressIn={() => DeviceCapabilities.canUseTouchScreen() && ControlSelection.block()}
onPressOut={() => ControlSelection.unblock()}
onLongPress={showContextMenu}
accessibilityLabel={props.isBillSplit ? translate('iou.split') : translate('iou.cash')}
accessibilityHint={CurrencyUtils.convertToDisplayString(requestAmount, requestCurrency)}
style={[styles.moneyRequestPreviewBox, ...props.containerStyles, shouldDisableOnPress && styles.cursorDefault]}
>
{childContainer}
</PressableWithFeedback>
);
}
MoneyRequestPreview.propTypes = propTypes;
MoneyRequestPreview.defaultProps = defaultProps;
MoneyRequestPreview.displayName = 'MoneyRequestPreview';
export default withOnyx({
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
chatReport: {
key: ({chatReportID}) => `${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`,
},
iouReport: {
key: ({iouReportID}) => `${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`,
},
session: {
key: ONYXKEYS.SESSION,
},
transaction: {
key: ({action}) => `${ONYXKEYS.COLLECTION.TRANSACTION}${(action && action.originalMessage && action.originalMessage.IOUTransactionID) || 0}`,
},
walletTerms: {
key: ONYXKEYS.WALLET_TERMS,
},
})(MoneyRequestPreview);