-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathInternationalDepositAccountContent.tsx
140 lines (119 loc) · 5.73 KB
/
InternationalDepositAccountContent.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
import React, {useCallback, useMemo} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import useLocalize from '@hooks/useLocalize';
import useSubStep from '@hooks/useSubStep';
import * as FormActions from '@libs/actions/FormActions';
import Navigation from '@libs/Navigation/Navigation';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {InternationalBankAccountForm} from '@src/types/form';
import type {BankAccountList, CorpayFields, PrivatePersonalDetails} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import AccountHolderInformation from './substeps/AccountHolderInformation';
import AccountType from './substeps/AccountType';
import BankAccountDetails from './substeps/BankAccountDetails';
import BankInformation from './substeps/BankInformation';
import Confirmation from './substeps/Confirmation';
import CountrySelection from './substeps/CountrySelection';
import Success from './substeps/Success';
import type {CustomSubStepProps} from './types';
import {getFieldsMap, getInitialPersonalDetailsValues, getInitialSubstep, getSubstepValues, testValidation} from './utils';
type InternationalDepositAccountContentProps = {
privatePersonalDetails: OnyxEntry<PrivatePersonalDetails>;
corpayFields: OnyxEntry<CorpayFields>;
bankAccountList: OnyxEntry<BankAccountList>;
draftValues: OnyxEntry<InternationalBankAccountForm>;
country: OnyxEntry<string>;
isAccountLoading: boolean;
};
const formSteps = [CountrySelection, BankAccountDetails, AccountType, BankInformation, AccountHolderInformation, Confirmation, Success];
function getSkippedSteps(skipAccountTypeStep: boolean, skipAccountHolderInformationStep: boolean) {
const skippedSteps = [];
if (skipAccountTypeStep) {
skippedSteps.push(CONST.CORPAY_FIELDS.INDEXES.MAPPING.ACCOUNT_TYPE);
}
if (skipAccountHolderInformationStep) {
skippedSteps.push(CONST.CORPAY_FIELDS.INDEXES.MAPPING.ACCOUNT_HOLDER_INFORMATION);
}
return skippedSteps;
}
function InternationalDepositAccountContent({privatePersonalDetails, corpayFields, bankAccountList, draftValues, country, isAccountLoading}: InternationalDepositAccountContentProps) {
const {translate} = useLocalize();
const fieldsMap = useMemo(() => getFieldsMap(corpayFields), [corpayFields]);
const values = useMemo(
() => getSubstepValues(privatePersonalDetails, corpayFields, bankAccountList, draftValues, country, fieldsMap),
[privatePersonalDetails, corpayFields, bankAccountList, draftValues, country, fieldsMap],
);
const initialAccountHolderDetailsValues = useMemo(() => getInitialPersonalDetailsValues(privatePersonalDetails), [privatePersonalDetails]);
const startFrom = useMemo(() => getInitialSubstep(values, fieldsMap), [fieldsMap, values]);
const skipAccountTypeStep = isEmptyObject(fieldsMap[CONST.CORPAY_FIELDS.STEPS_NAME.ACCOUNT_TYPE]);
const skipAccountHolderInformationStep = testValidation(initialAccountHolderDetailsValues, fieldsMap[CONST.CORPAY_FIELDS.STEPS_NAME.ACCOUNT_HOLDER_INFORMATION]);
const skippedSteps = getSkippedSteps(skipAccountTypeStep, skipAccountHolderInformationStep);
const handleFinishStep = useCallback(() => {
FormActions.clearDraftValues(ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM);
Navigation.goBack();
}, []);
const {
componentToRender: SubStep,
isEditing,
nextScreen,
prevScreen,
screenIndex,
moveTo,
resetScreenIndex,
} = useSubStep<CustomSubStepProps>({bodyContent: formSteps, startFrom, onFinished: handleFinishStep, skipSteps: skippedSteps});
const handleBackButtonPress = () => {
if (isEditing) {
resetScreenIndex(CONST.CORPAY_FIELDS.INDEXES.MAPPING.CONFIRMATION);
return;
}
// Clicking back on the first screen should dismiss the modal
if (screenIndex === CONST.CORPAY_FIELDS.INDEXES.MAPPING.COUNTRY_SELECTOR) {
FormActions.clearDraftValues(ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM);
Navigation.goBack();
return;
}
// Clicking back on the success screen should dismiss the modal
if (screenIndex === CONST.CORPAY_FIELDS.INDEXES.MAPPING.SUCCESS) {
FormActions.clearDraftValues(ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM);
Navigation.goBack();
return;
}
prevScreen();
};
const handleNextScreen = useCallback(() => {
if (isEditing) {
resetScreenIndex(CONST.CORPAY_FIELDS.INDEXES.MAPPING.CONFIRMATION);
return;
}
nextScreen();
}, [resetScreenIndex, isEditing, nextScreen]);
if (isAccountLoading) {
return <FullScreenLoadingIndicator />;
}
return (
<ScreenWrapper
shouldEnableMaxHeight
testID={InternationalDepositAccountContent.displayName}
>
<HeaderWithBackButton
title={translate('bankAccount.addBankAccount')}
onBackButtonPress={handleBackButtonPress}
/>
<SubStep
isEditing={isEditing}
onNext={handleNextScreen}
onMove={moveTo}
screenIndex={screenIndex}
resetScreenIndex={resetScreenIndex}
formValues={values}
fieldsMap={fieldsMap}
/>
</ScreenWrapper>
);
}
InternationalDepositAccountContent.displayName = 'InternationalDepositAccountContent';
export default InternationalDepositAccountContent;