-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBaseGetPhysicalCard.tsx
216 lines (187 loc) · 9.14 KB
/
BaseGetPhysicalCard.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
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
import React, {useCallback, useEffect, useRef, useState} from 'react';
import type {ReactNode} from 'react';
import {useOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import FormProvider from '@components/Form/FormProvider';
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 useThemeStyles from '@hooks/useThemeStyles';
import * as FormActions from '@libs/actions/FormActions';
import * as User from '@libs/actions/User';
import * as Wallet from '@libs/actions/Wallet';
import * as CardUtils from '@libs/CardUtils';
import * as ErrorUtils from '@libs/ErrorUtils';
import * as GetPhysicalCardUtils from '@libs/GetPhysicalCardUtils';
import Navigation from '@libs/Navigation/Navigation';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {GetPhysicalCardForm} from '@src/types/form';
import type {Errors} from '@src/types/onyx/OnyxCommon';
import type ChildrenProps from '@src/types/utils/ChildrenProps';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
type OnValidate = (values: OnyxEntry<GetPhysicalCardForm>) => Errors;
type RenderContentProps = ChildrenProps & {
onSubmit: () => void;
submitButtonText: string;
onValidate: OnValidate;
};
type BaseGetPhysicalCardProps = {
/** Text displayed below page title */
headline: string;
/** Children components that will be rendered by renderContent */
children?: ReactNode;
/** Current route from ROUTES */
currentRoute: string;
/** Expensify card domain */
domain: string;
/** Whether or not the current step of the get physical card flow is the confirmation page */
isConfirmation?: boolean;
/** Render prop, used to render form content */
renderContent?: (args: RenderContentProps) => React.ReactNode;
/** Text displayed on bottom submit button */
submitButtonText: string;
/** Title displayed on top of the page */
title: string;
/** Callback executed when validating get physical card form data */
onValidate?: OnValidate;
};
function DefaultRenderContent({onSubmit, submitButtonText, children, onValidate}: RenderContentProps) {
const styles = useThemeStyles();
return (
<FormProvider
formID={ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM}
submitButtonText={submitButtonText}
submitButtonStyles={styles.mh5}
onSubmit={onSubmit}
style={styles.flex1}
validate={onValidate}
>
{children}
</FormProvider>
);
}
function BaseGetPhysicalCard({
children,
currentRoute,
domain,
headline,
isConfirmation = false,
renderContent = DefaultRenderContent,
submitButtonText,
title,
onValidate = () => ({}),
}: BaseGetPhysicalCardProps) {
const styles = useThemeStyles();
const isRouteSet = useRef(false);
const {translate} = useLocalize();
const [cardList] = useOnyx(ONYXKEYS.CARD_LIST);
const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST);
const [session] = useOnyx(ONYXKEYS.SESSION);
const [privatePersonalDetails] = useOnyx(ONYXKEYS.PRIVATE_PERSONAL_DETAILS);
const [validateCodeAction] = useOnyx(ONYXKEYS.VALIDATE_ACTION_CODE);
const [draftValues] = useOnyx(ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM_DRAFT);
const [account] = useOnyx(ONYXKEYS.ACCOUNT);
const [isActionCodeModalVisible, setActionCodeModalVisible] = useState(false);
const [formData] = useOnyx(ONYXKEYS.FORMS.REPORT_PHYSICAL_CARD_FORM);
const domainCards = CardUtils.getDomainCards(cardList)[domain] || [];
const cardToBeIssued = domainCards.find((card) => !card?.nameValuePairs?.isVirtual && card?.state === CONST.EXPENSIFY_CARD.STATE.STATE_NOT_ISSUED);
const [currentCardID, setCurrentCardID] = useState<string | undefined>(cardToBeIssued?.cardID.toString());
const errorMessage = ErrorUtils.getLatestErrorMessageField(cardToBeIssued);
useBeforeRemove(() => setActionCodeModalVisible(false));
useEffect(() => {
if (isRouteSet.current || !privatePersonalDetails || !cardList) {
return;
}
// When there are no cards for the specified domain, user is redirected to the wallet page
if (domainCards.length === 0 || !cardToBeIssued) {
Navigation.goBack(ROUTES.SETTINGS_WALLET);
return;
}
// When there's no physical card or it exists but it doesn't have the required state for this flow,
// redirect user to the espensify card page
if (cardToBeIssued.state !== CONST.EXPENSIFY_CARD.STATE.STATE_NOT_ISSUED) {
Navigation.goBack(ROUTES.SETTINGS_WALLET_DOMAINCARD.getRoute(cardToBeIssued.cardID.toString()));
return;
}
if (!draftValues) {
const updatedDraftValues = GetPhysicalCardUtils.getUpdatedDraftValues(undefined, privatePersonalDetails, loginList);
// Form draft data needs to be initialized with the private personal details
// If no draft data exists
FormActions.setDraftValues(ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM, updatedDraftValues);
return;
}
// Redirect user to previous steps of the flow if he hasn't finished them yet
GetPhysicalCardUtils.setCurrentRoute(currentRoute, domain, GetPhysicalCardUtils.getUpdatedPrivatePersonalDetails(draftValues, privatePersonalDetails));
isRouteSet.current = true;
}, [cardList, currentRoute, domain, domainCards.length, draftValues, loginList, cardToBeIssued, privatePersonalDetails]);
useEffect(() => {
// Current step of the get physical card flow should be the confirmation page; and
// Card has NOT_ACTIVATED state when successfully being issued so cardToBeIssued should be undefined
if (!isConfirmation || !!cardToBeIssued || !currentCardID) {
return;
}
// Form draft data needs to be erased when the flow is complete,
// so that no stale data is left on Onyx
FormActions.clearDraftValues(ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM);
Wallet.clearPhysicalCardError(currentCardID);
Navigation.navigate(ROUTES.SETTINGS_WALLET_DOMAINCARD.getRoute(currentCardID));
setCurrentCardID(undefined);
}, [currentCardID, isConfirmation, cardToBeIssued]);
const onSubmit = useCallback(() => {
const updatedPrivatePersonalDetails = GetPhysicalCardUtils.getUpdatedPrivatePersonalDetails(draftValues, privatePersonalDetails);
if (isConfirmation) {
setActionCodeModalVisible(true);
return;
}
GetPhysicalCardUtils.goToNextPhysicalCardRoute(domain, updatedPrivatePersonalDetails);
}, [isConfirmation, domain, draftValues, privatePersonalDetails]);
const handleIssuePhysicalCard = useCallback(
(validateCode: string) => {
setCurrentCardID(cardToBeIssued?.cardID.toString());
const updatedPrivatePersonalDetails = GetPhysicalCardUtils.getUpdatedPrivatePersonalDetails(draftValues, privatePersonalDetails);
Wallet.requestPhysicalExpensifyCard(cardToBeIssued?.cardID ?? -1, session?.authToken ?? '', updatedPrivatePersonalDetails, validateCode);
},
[cardToBeIssued?.cardID, draftValues, session?.authToken, privatePersonalDetails],
);
const handleBackButtonPress = useCallback(() => {
if (currentCardID) {
Navigation.goBack(ROUTES.SETTINGS_WALLET_DOMAINCARD.getRoute(currentCardID));
return;
}
Navigation.goBack();
}, [currentCardID]);
return (
<ScreenWrapper
shouldEnablePickerAvoiding={false}
shouldShowOfflineIndicator={false}
testID={BaseGetPhysicalCard.displayName}
>
<HeaderWithBackButton
title={title}
onBackButtonPress={handleBackButtonPress}
/>
<Text style={[styles.textHeadline, styles.mh5, styles.mb5]}>{headline}</Text>
{renderContent({onSubmit, submitButtonText, children, onValidate})}
<ValidateCodeActionModal
isLoading={formData?.isLoading}
hasMagicCodeBeenSent={validateCodeAction?.validateCodeSent}
isVisible={isActionCodeModalVisible}
sendValidateCode={() => User.requestValidateCodeAction()}
clearError={() => Wallet.clearPhysicalCardError(currentCardID)}
validateError={!isEmptyObject(formData?.errors) ? formData?.errors : errorMessage}
handleSubmitForm={handleIssuePhysicalCard}
title={translate('cardPage.validateCardTitle')}
onClose={() => setActionCodeModalVisible(false)}
descriptionPrimary={translate('cardPage.enterMagicCode', {contactMethod: account?.primaryLogin ?? ''})}
/>
</ScreenWrapper>
);
}
BaseGetPhysicalCard.displayName = 'BaseGetPhysicalCard';
export default BaseGetPhysicalCard;
export type {RenderContentProps};