Skip to content

Commit e3d5bbf

Browse files
committed
Reapply "Merge pull request #65081 from callstack-internal/fix/remove-confirmation-sideEffect"
This reverts commit 7b0ebee.
1 parent 7a3cb1d commit e3d5bbf

3 files changed

Lines changed: 72 additions & 32 deletions

File tree

src/libs/API/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,6 @@ const SIDE_EFFECT_REQUEST_COMMANDS = {
11751175
DISCONNECT_AS_DELEGATE: 'DisconnectAsDelegate',
11761176
COMPLETE_HYBRID_APP_ONBOARDING: 'CompleteHybridAppOnboarding',
11771177
CONNECT_POLICY_TO_QUICKBOOKS_DESKTOP: 'ConnectPolicyToQuickbooksDesktop',
1178-
BANK_ACCOUNT_CREATE_CORPAY: 'BankAccount_CreateCorpay',
11791178
MERGE_INTO_ACCOUNT_AND_LOGIN: 'MergeIntoAccountAndLogIn',
11801179

11811180
// PayMoneyRequestOnSearch only works online (pattern C) and we need to play the success sound only when the request is successful
@@ -1201,7 +1200,6 @@ type SideEffectRequestCommandParameters = {
12011200
[SIDE_EFFECT_REQUEST_COMMANDS.DISCONNECT_AS_DELEGATE]: EmptyObject;
12021201
[SIDE_EFFECT_REQUEST_COMMANDS.COMPLETE_HYBRID_APP_ONBOARDING]: EmptyObject;
12031202
[SIDE_EFFECT_REQUEST_COMMANDS.CONNECT_POLICY_TO_QUICKBOOKS_DESKTOP]: Parameters.ConnectPolicyToQuickBooksDesktopParams;
1204-
[SIDE_EFFECT_REQUEST_COMMANDS.BANK_ACCOUNT_CREATE_CORPAY]: Parameters.BankAccountCreateCorpayParams;
12051203
[SIDE_EFFECT_REQUEST_COMMANDS.PAY_MONEY_REQUEST_ON_SEARCH]: Parameters.PayMoneyRequestOnSearchParams;
12061204
[SIDE_EFFECT_REQUEST_COMMANDS.MERGE_INTO_ACCOUNT_AND_LOGIN]: Parameters.MergeIntoAccountAndLogInParams;
12071205
[SIDE_EFFECT_REQUEST_COMMANDS.LOG_OUT]: Parameters.LogOutParams;

src/libs/actions/BankAccounts.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type {
1616
} from '@libs/API/parameters';
1717
import type {SaveCorpayOnboardingCompanyDetails} from '@libs/API/parameters/SaveCorpayOnboardingCompanyDetailsParams';
1818
import type SaveCorpayOnboardingDirectorInformationParams from '@libs/API/parameters/SaveCorpayOnboardingDirectorInformationParams';
19-
import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
19+
import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
2020
import {getMicroSecondOnyxErrorWithTranslationKey} from '@libs/ErrorUtils';
2121
import {translateLocal} from '@libs/Localize';
2222
import Navigation from '@libs/Navigation/Navigation';
@@ -1057,8 +1057,51 @@ function createCorpayBankAccountForWalletFlow(data: InternationalBankAccountForm
10571057
country: data.bankCountry,
10581058
currency: data.bankCurrency,
10591059
};
1060-
// eslint-disable-next-line rulesdir/no-api-side-effects-method
1061-
return API.makeRequestWithSideEffects(SIDE_EFFECT_REQUEST_COMMANDS.BANK_ACCOUNT_CREATE_CORPAY, {isWithdrawal: false, isSavings: true, inputs: JSON.stringify(inputData)});
1060+
1061+
const parameters = {
1062+
isWithdrawal: false,
1063+
isSavings: true,
1064+
inputs: JSON.stringify(inputData),
1065+
};
1066+
1067+
const onyxData: OnyxData = {
1068+
optimisticData: [
1069+
{
1070+
onyxMethod: Onyx.METHOD.MERGE,
1071+
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
1072+
value: {
1073+
isLoading: true,
1074+
isCreateCorpayBankAccount: true,
1075+
},
1076+
},
1077+
],
1078+
successData: [
1079+
{
1080+
onyxMethod: Onyx.METHOD.MERGE,
1081+
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
1082+
value: {
1083+
isLoading: false,
1084+
isCreateCorpayBankAccount: false,
1085+
errors: null,
1086+
isSuccess: true,
1087+
},
1088+
},
1089+
],
1090+
failureData: [
1091+
{
1092+
onyxMethod: Onyx.METHOD.MERGE,
1093+
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
1094+
value: {
1095+
isLoading: false,
1096+
isCreateCorpayBankAccount: false,
1097+
isSuccess: false,
1098+
errors: getMicroSecondOnyxErrorWithTranslationKey('walletPage.addBankAccountFailure'),
1099+
},
1100+
},
1101+
],
1102+
};
1103+
1104+
return API.write(WRITE_COMMANDS.BANK_ACCOUNT_CREATE_CORPAY, parameters, onyxData);
10621105
}
10631106

10641107
export {

src/pages/settings/Wallet/InternationalDepositAccount/substeps/Confirmation.tsx

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useCallback, useState} from 'react';
1+
import React, {useCallback, useEffect} from 'react';
22
import CheckboxWithLabel from '@components/CheckboxWithLabel';
33
import FormProvider from '@components/Form/FormProvider';
44
import InputWrapper from '@components/Form/InputWrapper';
@@ -13,8 +13,9 @@ import useNetwork from '@hooks/useNetwork';
1313
import useOnyx from '@hooks/useOnyx';
1414
import useThemeStyles from '@hooks/useThemeStyles';
1515
import {getCurrencySymbol} from '@libs/CurrencyUtils';
16+
import {getLatestErrorMessage} from '@libs/ErrorUtils';
1617
import type CustomSubStepProps from '@pages/settings/Wallet/InternationalDepositAccount/types';
17-
import {createCorpayBankAccountForWalletFlow} from '@userActions/BankAccounts';
18+
import {clearReimbursementAccountBankCreation, createCorpayBankAccountForWalletFlow} from '@userActions/BankAccounts';
1819
import CONST from '@src/CONST';
1920
import type {TranslationPaths} from '@src/languages/types';
2021
import ONYXKEYS from '@src/ONYXKEYS';
@@ -44,9 +45,8 @@ function TermsAndConditionsLabel() {
4445
function Confirmation({onNext, onMove, formValues, fieldsMap}: CustomSubStepProps) {
4546
const {translate} = useLocalize();
4647
const styles = useThemeStyles();
47-
const [isSubmitting, setIsSubmitting] = useState(false);
48-
const [error, setError] = useState('');
4948
const [corpayFields] = useOnyx(ONYXKEYS.CORPAY_FIELDS, {canBeMissing: false});
49+
const [reimbursementAccount] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {canBeMissing: false});
5050
const {isOffline} = useNetwork();
5151

5252
const getTitle = (field: CorpayFormField, fieldName: string) => {
@@ -62,25 +62,20 @@ function Confirmation({onNext, onMove, formValues, fieldsMap}: CustomSubStepProp
6262
};
6363

6464
const getDataAndGoToNextStep = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM>) => {
65-
setError('');
66-
setIsSubmitting(true);
67-
createCorpayBankAccountForWalletFlow(
68-
{...formValues, ...values},
69-
corpayFields?.classification ?? '',
70-
corpayFields?.destinationCountry ?? '',
71-
corpayFields?.preferredMethod ?? '',
72-
).then((response) => {
73-
setIsSubmitting(false);
74-
if (response?.jsonCode) {
75-
if (response.jsonCode === CONST.JSON_CODE.SUCCESS) {
76-
onNext();
77-
} else {
78-
setError(response.message ?? '');
79-
}
80-
}
81-
});
65+
createCorpayBankAccountForWalletFlow({...formValues, ...values}, corpayFields?.classification ?? '', corpayFields?.destinationCountry ?? '', corpayFields?.preferredMethod ?? '');
8266
};
8367

68+
useEffect(() => {
69+
if (reimbursementAccount?.isLoading === true || !!reimbursementAccount?.errors) {
70+
return;
71+
}
72+
73+
if (reimbursementAccount?.isSuccess === true) {
74+
onNext();
75+
clearReimbursementAccountBankCreation();
76+
}
77+
}, [reimbursementAccount?.isLoading, reimbursementAccount?.isSuccess, reimbursementAccount?.errors, onNext]);
78+
8479
const summaryItems: MenuItemProps[] = [
8580
{
8681
description: translate('common.country'),
@@ -162,6 +157,8 @@ function Confirmation({onNext, onMove, formValues, fieldsMap}: CustomSubStepProp
162157
[translate],
163158
);
164159

160+
const errorMessage = getLatestErrorMessage(reimbursementAccount);
161+
165162
return (
166163
<ScrollView contentContainerStyle={styles.flexGrow1}>
167164
<Text style={[styles.textHeadlineLineHeightXXL, styles.ph5, styles.mb3]}>{translate('addPersonalBankAccount.confirmationStepHeader')}</Text>
@@ -185,7 +182,7 @@ function Confirmation({onNext, onMove, formValues, fieldsMap}: CustomSubStepProp
185182
submitButtonText={translate('common.confirm')}
186183
style={[styles.mh5, styles.flexGrow1]}
187184
enabledWhenOffline={false}
188-
isLoading={isSubmitting}
185+
isLoading={reimbursementAccount?.isLoading}
189186
shouldHideFixErrorsAlert
190187
>
191188
<InputWrapper
@@ -196,11 +193,13 @@ function Confirmation({onNext, onMove, formValues, fieldsMap}: CustomSubStepProp
196193
style={[styles.mt3]}
197194
shouldSaveDraft
198195
/>
199-
<FormHelpMessage
200-
style={[styles.mt3, styles.mbn1]}
201-
isError
202-
message={error}
203-
/>
196+
{!!errorMessage && (
197+
<FormHelpMessage
198+
style={[styles.mt3, styles.mbn1]}
199+
isError
200+
message={errorMessage}
201+
/>
202+
)}
204203
</FormProvider>
205204
</ScrollView>
206205
);

0 commit comments

Comments
 (0)