-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ReportVirtualCardFraudPage.tsx
124 lines (108 loc) · 5.08 KB
/
ReportVirtualCardFraudPage.tsx
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
import React, {useCallback, useEffect, useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import FormAlertWithSubmitButton from '@components/FormAlertWithSubmitButton';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import Text from '@components/Text';
import ValidateCodeActionModal from '@components/ValidateCodeActionModal';
import useBeforeRemove from '@hooks/useBeforeRemove';
import useLocalize from '@hooks/useLocalize';
import usePrevious from '@hooks/usePrevious';
import useThemeStyles from '@hooks/useThemeStyles';
import {requestValidateCodeAction} from '@libs/actions/User';
import * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
import * as Card from '@userActions/Card';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
type ReportVirtualCardFraudPageProps = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.WALLET.REPORT_VIRTUAL_CARD_FRAUD>;
function ReportVirtualCardFraudPage({
route: {
params: {cardID = ''},
},
}: ReportVirtualCardFraudPageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST);
const [account] = useOnyx(ONYXKEYS.ACCOUNT);
const [cardList] = useOnyx(ONYXKEYS.CARD_LIST);
const [formData] = useOnyx(ONYXKEYS.FORMS.REPORT_VIRTUAL_CARD_FRAUD);
const primaryLogin = account?.primaryLogin ?? '';
const loginData = loginList?.[primaryLogin];
const virtualCard = cardList?.[cardID];
const virtualCardError = ErrorUtils.getLatestErrorMessage(virtualCard);
const validateError = ErrorUtils.getLatestErrorMessageField(virtualCard);
const [isValidateCodeActionModalVisible, setIsValidateCodeActionModalVisible] = useState(false);
const prevIsLoading = usePrevious(formData?.isLoading);
useBeforeRemove(() => setIsValidateCodeActionModalVisible(false));
useEffect(() => {
if (!prevIsLoading || formData?.isLoading) {
return;
}
if (!isEmptyObject(virtualCard?.errors)) {
return;
}
Navigation.navigate(ROUTES.SETTINGS_WALLET_DOMAINCARD.getRoute(cardID));
}, [cardID, formData?.isLoading, prevIsLoading, virtualCard?.errors]);
const handleValidateCodeEntered = useCallback(
(validateCode: string) => {
if (!virtualCard) {
return;
}
Card.reportVirtualExpensifyCardFraud(virtualCard, validateCode);
},
[virtualCard],
);
const sendValidateCode = () => {
if (loginData?.validateCodeSent) {
return;
}
requestValidateCodeAction();
};
const handleSubmit = useCallback(() => {
setIsValidateCodeActionModalVisible(true);
}, [setIsValidateCodeActionModalVisible]);
if (isEmptyObject(virtualCard)) {
return <NotFoundPage />;
}
return (
<ScreenWrapper testID={ReportVirtualCardFraudPage.displayName}>
<HeaderWithBackButton
title={translate('reportFraudPage.title')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WALLET_DOMAINCARD.getRoute(cardID))}
/>
<View style={[styles.flex1, styles.justifyContentBetween]}>
<Text style={[styles.webViewStyles.baseFontStyle, styles.mh5]}>{translate('reportFraudPage.description')}</Text>
<FormAlertWithSubmitButton
isAlertVisible={!!virtualCardError}
onSubmit={handleSubmit}
message={virtualCardError}
isLoading={formData?.isLoading}
buttonText={translate('reportFraudPage.deactivateCard')}
containerStyles={[styles.m5]}
/>
<ValidateCodeActionModal
handleSubmitForm={handleValidateCodeEntered}
sendValidateCode={sendValidateCode}
validateError={validateError}
clearError={() => {
Card.clearCardListErrors(virtualCard.cardID);
}}
onClose={() => setIsValidateCodeActionModalVisible(false)}
isVisible={isValidateCodeActionModalVisible}
title={translate('cardPage.validateCardTitle')}
descriptionPrimary={translate('cardPage.enterMagicCode', {contactMethod: primaryLogin})}
hasMagicCodeBeenSent={!!loginData?.validateCodeSent}
/>
</View>
</ScreenWrapper>
);
}
ReportVirtualCardFraudPage.displayName = 'ReportVirtualCardFraudPage';
export default ReportVirtualCardFraudPage;