-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ExpensifyCardPage.js
327 lines (304 loc) · 14.5 KB
/
ExpensifyCardPage.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
import PropTypes from 'prop-types';
import React, {useEffect, useMemo, useState} from 'react';
import {ScrollView, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import Button from '@components/Button';
import CardPreview from '@components/CardPreview';
import DotIndicatorMessage from '@components/DotIndicatorMessage';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import * as Expensicons from '@components/Icon/Expensicons';
import MenuItem from '@components/MenuItem';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import ScreenWrapper from '@components/ScreenWrapper';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CardUtils from '@libs/CardUtils';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import FormUtils from '@libs/FormUtils';
import * as GetPhysicalCardUtils from '@libs/GetPhysicalCardUtils';
import Navigation from '@libs/Navigation/Navigation';
import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
import * as Card from '@userActions/Card';
import * as Link from '@userActions/Link';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import assignedCardPropTypes from './assignedCardPropTypes';
import RedDotCardSection from './RedDotCardSection';
import CardDetails from './WalletPage/CardDetails';
const propTypes = {
/* Onyx Props */
/** The details about the Expensify cards */
cardList: PropTypes.objectOf(assignedCardPropTypes),
/** Draft values used by the get physical card form */
draftValues: PropTypes.shape({
addressLine1: PropTypes.string,
addressLine2: PropTypes.string,
city: PropTypes.string,
state: PropTypes.string,
country: PropTypes.string,
zipPostCode: PropTypes.string,
phoneNumber: PropTypes.string,
legalFirstName: PropTypes.string,
legalLastName: PropTypes.string,
}),
loginList: PropTypes.shape({
/** The partner creating the account. It depends on the source: website, mobile, integrations, ... */
partnerName: PropTypes.string,
/** Phone/Email associated with user */
partnerUserID: PropTypes.string,
/** The date when the login was validated, used to show the brickroad status */
validatedDate: PropTypes.string,
/** Field-specific server side errors keyed by microtime */
errorFields: PropTypes.objectOf(PropTypes.objectOf(PropTypes.string)),
/** Field-specific pending states for offline UI status */
pendingFields: PropTypes.objectOf(PropTypes.objectOf(PropTypes.string)),
}),
/** User's private personal details */
privatePersonalDetails: PropTypes.shape({
legalFirstName: PropTypes.string,
legalLastName: PropTypes.string,
phoneNumber: PropTypes.string,
/** User's home address */
address: PropTypes.shape({
street: PropTypes.string,
city: PropTypes.string,
state: PropTypes.string,
zip: PropTypes.string,
country: PropTypes.string,
}),
}),
/** Navigation route context info provided by react navigation */
route: PropTypes.shape({
params: PropTypes.shape({
/** domain passed via route /settings/wallet/card/:domain */
domain: PropTypes.string,
}),
}).isRequired,
};
const defaultProps = {
cardList: null,
draftValues: {
addressLine1: '',
addressLine2: '',
city: '',
state: '',
country: '',
zipPostCode: '',
phoneNumber: '',
legalFirstName: '',
legalLastName: '',
},
loginList: {},
privatePersonalDetails: {
legalFirstName: '',
legalLastName: '',
phoneNumber: null,
address: {
street: '',
city: '',
state: '',
zip: '',
country: '',
},
},
};
function ExpensifyCardPage({
cardList,
draftValues,
loginList,
privatePersonalDetails,
route: {
params: {domain},
},
}) {
const styles = useThemeStyles();
const {isOffline} = useNetwork();
const {translate} = useLocalize();
const domainCards = useMemo(() => cardList && CardUtils.getDomainCards(cardList)[domain], [cardList, domain]);
const virtualCard = useMemo(() => (domainCards && _.find(domainCards, (card) => card.isVirtual)) || {}, [domainCards]);
const physicalCard = useMemo(() => (domainCards && _.find(domainCards, (card) => !card.isVirtual)) || {}, [domainCards]);
const [isLoading, setIsLoading] = useState(false);
const [isNotFound, setIsNotFound] = useState(false);
const [details, setDetails] = useState({});
const [cardDetailsError, setCardDetailsError] = useState('');
useEffect(() => {
if (!cardList) {
return;
}
setIsNotFound(_.isEmpty(virtualCard) && _.isEmpty(physicalCard));
}, [cardList, physicalCard, virtualCard]);
const formattedAvailableSpendAmount = CurrencyUtils.convertToDisplayString(physicalCard.availableSpend || virtualCard.availableSpend || 0);
const handleRevealDetails = () => {
setIsLoading(true);
// We can't store the response in Onyx for security reasons.
// That is why this action is handled manually and the response is stored in a local state
// Hence eslint disable here.
// eslint-disable-next-line rulesdir/no-thenable-actions-in-views
Card.revealVirtualCardDetails(virtualCard.cardID)
.then((value) => {
setDetails(value);
setCardDetailsError('');
})
.catch(setCardDetailsError)
.finally(() => setIsLoading(false));
};
const goToGetPhysicalCardFlow = () => {
const updatedDraftValues = GetPhysicalCardUtils.getUpdatedDraftValues(draftValues, privatePersonalDetails, loginList);
GetPhysicalCardUtils.goToNextPhysicalCardRoute(domain, GetPhysicalCardUtils.getUpdatedPrivatePersonalDetails(updatedDraftValues), loginList);
};
const hasDetectedDomainFraud = _.some(domainCards, (card) => card.fraud === CONST.EXPENSIFY_CARD.FRAUD_TYPES.DOMAIN);
const hasDetectedIndividualFraud = _.some(domainCards, (card) => card.fraud === CONST.EXPENSIFY_CARD.FRAUD_TYPES.INDIVIDUAL);
const cardDetailsErrorObject = cardDetailsError ? {error: cardDetailsError} : {};
if (isNotFound) {
return <NotFoundPage onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WALLET)} />;
}
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
testID={ExpensifyCardPage.displayName}
>
{({safeAreaPaddingBottomStyle}) => (
<>
<HeaderWithBackButton
title={translate('cardPage.expensifyCard')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WALLET)}
/>
<ScrollView contentContainerStyle={safeAreaPaddingBottomStyle}>
<View style={[styles.flex1, styles.mb9, styles.mt9]}>
<CardPreview />
</View>
{hasDetectedDomainFraud ? (
<DotIndicatorMessage
style={styles.pageWrapper}
textStyles={styles.walletLockedMessage}
messages={{0: translate('cardPage.cardLocked')}}
type="error"
/>
) : null}
{hasDetectedIndividualFraud && !hasDetectedDomainFraud ? (
<>
<RedDotCardSection
title={translate('cardPage.suspiciousBannerTitle')}
description={translate('cardPage.suspiciousBannerDescription')}
/>
<Button
medium
style={[styles.mh5, styles.mb5]}
text={translate('cardPage.reviewTransaction')}
onPress={() => Link.openOldDotLink('inbox')}
/>
</>
) : null}
{!hasDetectedDomainFraud ? (
<>
<MenuItemWithTopDescription
description={translate('cardPage.availableSpend')}
title={formattedAvailableSpendAmount}
interactive={false}
titleStyle={styles.newKansasLarge}
/>
{!_.isEmpty(virtualCard) && (
<>
{details.pan ? (
<CardDetails
pan={details.pan}
expiration={details.expiration}
cvv={details.cvv}
privatePersonalDetails={{address: details.address}}
domain={domain}
/>
) : (
<>
<MenuItemWithTopDescription
description={translate('cardPage.virtualCardNumber')}
title={CardUtils.maskCard(virtualCard.lastFourPAN)}
interactive={false}
titleStyle={styles.walletCardNumber}
shouldShowRightComponent
rightComponent={
<Button
medium
text={translate('cardPage.cardDetails.revealDetails')}
onPress={handleRevealDetails}
isDisabled={isLoading || isOffline}
isLoading={isLoading}
/>
}
/>
<DotIndicatorMessage
messages={cardDetailsErrorObject}
type="error"
style={[styles.ph5]}
/>
</>
)}
<MenuItemWithTopDescription
title={translate('cardPage.reportFraud')}
titleStyle={styles.walletCardMenuItem}
icon={Expensicons.Flag}
shouldShowRightIcon
onPress={() => Navigation.navigate(ROUTES.SETTINGS_REPORT_FRAUD.getRoute(domain))}
/>
</>
)}
{!_.isEmpty(physicalCard) && (
<>
<MenuItemWithTopDescription
description={translate('cardPage.physicalCardNumber')}
title={CardUtils.maskCard(physicalCard.lastFourPAN)}
interactive={false}
titleStyle={styles.walletCardMenuItem}
/>
<MenuItem
title={translate('reportCardLostOrDamaged.report')}
icon={Expensicons.Flag}
shouldShowRightIcon
onPress={() => Navigation.navigate(ROUTES.SETTINGS_WALLET_REPORT_CARD_LOST_OR_DAMAGED.getRoute(domain))}
/>
</>
)}
</>
) : null}
</ScrollView>
{physicalCard.state === CONST.EXPENSIFY_CARD.STATE.NOT_ACTIVATED && (
<Button
success
style={[styles.w100, styles.p5]}
onPress={() => Navigation.navigate(ROUTES.SETTINGS_WALLET_CARD_ACTIVATE.getRoute(domain))}
text={translate('activateCardPage.activatePhysicalCard')}
/>
)}
{physicalCard.state === CONST.EXPENSIFY_CARD.STATE.STATE_NOT_ISSUED && (
<Button
success
text={translate('cardPage.getPhysicalCard')}
pressOnEnter
onPress={goToGetPhysicalCardFlow}
style={[styles.mh5, styles.mb5]}
/>
)}
</>
)}
</ScreenWrapper>
);
}
ExpensifyCardPage.propTypes = propTypes;
ExpensifyCardPage.defaultProps = defaultProps;
ExpensifyCardPage.displayName = 'ExpensifyCardPage';
export default withOnyx({
cardList: {
key: ONYXKEYS.CARD_LIST,
},
loginList: {
key: ONYXKEYS.LOGIN_LIST,
},
privatePersonalDetails: {
key: ONYXKEYS.PRIVATE_PERSONAL_DETAILS,
},
draftValues: {
key: FormUtils.getDraftKey(ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM),
},
})(ExpensifyCardPage);