-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathIOUPreview.js
282 lines (250 loc) · 11.4 KB
/
IOUPreview.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
import React from 'react';
import {
View,
TouchableWithoutFeedback,
} from 'react-native';
import PropTypes from 'prop-types';
import Str from 'expensify-common/lib/str';
import {withOnyx} from 'react-native-onyx';
import lodashGet from 'lodash/get';
import _ from 'underscore';
import compose from '../../libs/compose';
import styles from '../../styles/styles';
import ONYXKEYS from '../../ONYXKEYS';
import MultipleAvatars from '../MultipleAvatars';
import withLocalize, {withLocalizePropTypes} from '../withLocalize';
import * as Report from '../../libs/actions/Report';
import themeColors from '../../styles/themes/default';
import Icon from '../Icon';
import CONST from '../../CONST';
import * as Expensicons from '../Icon/Expensicons';
import Text from '../Text';
import * as PaymentMethods from '../../libs/actions/PaymentMethods';
import OfflineWithFeedback from '../OfflineWithFeedback';
import walletTermsPropTypes from '../../pages/EnablePayments/walletTermsPropTypes';
import ControlSelection from '../../libs/ControlSelection';
import * as DeviceCapabilities from '../../libs/DeviceCapabilities';
import reportActionPropTypes from '../../pages/home/report/reportActionPropTypes';
import {showContextMenuForReport} from '../ShowContextMenuContext';
import * as ReportUtils from '../../libs/ReportUtils';
import Button from '../Button';
const propTypes = {
/** Additional logic for displaying the pay button */
shouldHidePayButton: PropTypes.bool,
/** Callback for the Pay/Settle button */
onPayButtonPressed: PropTypes.func,
/** 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: PropTypes.shape({current: PropTypes.elementType}),
/** 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 */
/** Active IOU Report for current report */
iouReport: PropTypes.shape({
/** Email address of the manager in this iou report */
managerEmail: PropTypes.string,
/** Email address of the creator of this iou report */
ownerEmail: PropTypes.string,
/** Outstanding amount in cents of this transaction */
total: PropTypes.number,
/** Currency of outstanding amount of this transaction */
currency: PropTypes.string,
/** Does the iouReport have an outstanding IOU? */
hasOutstandingIOU: PropTypes.bool,
}),
/** 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.isRequired,
})),
/** 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,
/** Pending action, if any */
pendingAction: PropTypes.oneOf(_.values(CONST.RED_BRICK_ROAD_PENDING_ACTION)),
...withLocalizePropTypes,
};
const defaultProps = {
iouReport: {},
shouldHidePayButton: false,
onPayButtonPressed: null,
onPreviewPressed: () => {},
action: undefined,
contextMenuAnchor: undefined,
checkIfContextMenuActive: () => {},
containerStyles: [],
walletTerms: {},
pendingAction: null,
isHovered: false,
personalDetails: {},
session: {
email: null,
},
};
const IOUPreview = (props) => {
// Usually the parent determines whether the IOU Preview is displayed. But as the iouReport total cannot be known
// until it is stored locally, we need to make this check within the Component after retrieving it. This allows us
// to handle the loading UI from within this Component instead of needing to declare it within each parent, which
// would duplicate and complicate the logic
if (props.iouReport.total === 0) {
return null;
}
const sessionEmail = lodashGet(props.session, 'email', null);
const managerEmail = props.iouReport.managerEmail || '';
const ownerEmail = props.iouReport.ownerEmail || '';
// Pay button should only be visible to the manager of the report.
const isCurrentUserManager = managerEmail === sessionEmail;
const managerName = ReportUtils.getDisplayNameForParticipant(managerEmail, true);
const ownerName = ReportUtils.getDisplayNameForParticipant(ownerEmail, true);
const managerAvatar = {
source: ReportUtils.getAvatar(lodashGet(props.personalDetails, [managerEmail, 'avatar']), managerEmail),
type: CONST.ICON_TYPE_AVATAR,
name: managerEmail,
};
const ownerAvatar = {
source: ReportUtils.getAvatar(lodashGet(props.personalDetails, [ownerEmail, 'avatar']), ownerEmail),
type: CONST.ICON_TYPE_AVATAR,
name: ownerEmail,
};
const cachedTotal = props.iouReport.total && props.iouReport.currency
? props.numberFormat(
Math.abs(props.iouReport.total) / 100,
{style: 'currency', currency: props.iouReport.currency},
) : '';
const avatarTooltip = [Str.removeSMSDomain(managerEmail), Str.removeSMSDomain(ownerEmail)];
const showContextMenu = (event) => {
// Use action and shouldHidePayButton props to check if we are in IOUDetailsModal,
// if it's true, do nothing when user long press, otherwise show context menu.
if (!props.action && props.shouldHidePayButton) {
return;
}
showContextMenuForReport(
event,
props.contextMenuAnchor,
props.chatReportID,
props.action,
props.checkIfContextMenuActive,
);
};
return (
<TouchableWithoutFeedback
onPress={props.onPreviewPressed}
onPressIn={() => DeviceCapabilities.canUseTouchScreen() && ControlSelection.block()}
onPressOut={() => ControlSelection.unblock()}
onLongPress={showContextMenu}
>
<View>
<OfflineWithFeedback
pendingAction={props.pendingAction}
errors={props.walletTerms.errors}
onClose={() => {
PaymentMethods.clearWalletTermsError();
Report.clearIOUError(props.chatReportID);
}}
errorRowStyles={[styles.mbn1]}
needsOffscreenAlphaCompositing
>
<View style={[styles.iouPreviewBox, ...props.containerStyles]}>
<View style={[styles.flexRow]}>
<View style={[styles.flex1, styles.flexRow, styles.alignItemsCenter]}>
<Text style={styles.h1}>
{cachedTotal}
</Text>
{!props.iouReport.hasOutstandingIOU && (
<View style={styles.iouPreviewBoxCheckmark}>
<Icon src={Expensicons.Checkmark} fill={themeColors.iconSuccessFill} />
</View>
)}
</View>
<View style={styles.iouPreviewBoxAvatar}>
<MultipleAvatars
icons={[managerAvatar, ownerAvatar]}
secondAvatarStyle={[
styles.secondAvatarInline,
props.isHovered
? styles.iouPreviewBoxAvatarHover
: undefined,
]}
avatarTooltips={avatarTooltip}
/>
</View>
</View>
{isCurrentUserManager
? (
<Text>
{props.iouReport.hasOutstandingIOU
? props.translate('iou.youowe', {owner: ownerName})
: props.translate('iou.youpaid', {owner: ownerName})}
</Text>
)
: (
<>
<Text>
{props.iouReport.hasOutstandingIOU
? props.translate('iou.owesyou', {manager: managerName})
: props.translate('iou.paidyou', {manager: managerName})}
</Text>
{props.shouldShowPendingConversionMessage && (
<Text style={[styles.textLabel, styles.colorMuted]}>
{props.translate('iou.pendingConversionMessage')}
</Text>
)}
</>
)}
{(isCurrentUserManager
&& !props.shouldHidePayButton
&& props.iouReport.stateNum === CONST.REPORT.STATE_NUM.PROCESSING && (
<Button
style={styles.mt4}
onPress={props.onPayButtonPressed}
onPressIn={() => DeviceCapabilities.canUseTouchScreen() && ControlSelection.block()}
onPressOut={() => ControlSelection.unblock()}
onLongPress={showContextMenu}
text={props.translate('iou.pay')}
success
medium
/>
))}
</View>
</OfflineWithFeedback>
</View>
</TouchableWithoutFeedback>
);
};
IOUPreview.propTypes = propTypes;
IOUPreview.defaultProps = defaultProps;
IOUPreview.displayName = 'IOUPreview';
export default compose(
withLocalize,
withOnyx({
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS,
},
iouReport: {
key: ({iouReportID}) => `${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`,
},
session: {
key: ONYXKEYS.SESSION,
},
walletTerms: {
key: ONYXKEYS.WALLET_TERMS,
},
}),
)(IOUPreview);