diff --git a/src/libs/actions/ReimbursementAccount/resetFreePlanBankAccount.js b/src/libs/actions/ReimbursementAccount/resetFreePlanBankAccount.js index 4f54352169ff..3ef49fbc854f 100644 --- a/src/libs/actions/ReimbursementAccount/resetFreePlanBankAccount.js +++ b/src/libs/actions/ReimbursementAccount/resetFreePlanBankAccount.js @@ -1,12 +1,10 @@ -import Onyx from 'react-native-onyx'; import lodashGet from 'lodash/get'; import ONYXKEYS from '../../../ONYXKEYS'; -import * as DeprecatedAPI from '../../deprecatedAPI'; import CONST from '../../../CONST'; import * as store from './store'; -import Growl from '../../Growl'; import Navigation from '../../Navigation/Navigation'; import ROUTES from '../../../ROUTES'; +import * as API from '../../API'; /** * Reset user's reimbursement account. This will delete the bank account. @@ -20,35 +18,33 @@ function resetFreePlanBankAccount() { throw new Error('Missing credentials when attempting to reset free plan bank account'); } - // Create a copy of the reimbursementAccount data since we are going to optimistically wipe it so the UI changes quickly. - // If the API request fails we will set this data back into Onyx. - const previousACHData = {...store.getReimbursementAccountInSetup()}; - Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {achData: null, shouldShowResetModal: false}); - DeprecatedAPI.DeleteBankAccount({bankAccountID, ownerEmail: store.getCredentials().login}) - .then((response) => { - if (response.jsonCode !== 200) { - // Unable to delete bank account so we restore the bank account details - Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {achData: previousACHData}); - Growl.error('Sorry we were unable to delete this bank account. Please try again later'); - return; - } + const achData = { + useOnfido: true, + policyID: '', + isInSetup: true, + domainLimit: 0, + currentStep: CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT, + }; - // Reset reimbursement account, and clear draft user input - const achData = { - useOnfido: true, - policyID: '', - isInSetup: true, - domainLimit: 0, - currentStep: CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT, - }; - - Onyx.set(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {achData}); - Onyx.set(ONYXKEYS.REIMBURSEMENT_ACCOUNT_DRAFT, null); - - // Clear the NVP for the bank account so the user can add a new one and navigate back to bank account page - DeprecatedAPI.SetNameValuePair({name: CONST.NVP.FREE_PLAN_BANK_ACCOUNT_ID, value: ''}); - Navigation.navigate(ROUTES.getBankAccountRoute()); + API.write('RestartBankAccountSetup', + { + bankAccountID, + ownerEmail: store.getCredentials().login, + }, + { + optimisticData: [{ + onyxMethod: 'merge', + key: ONYXKEYS.REIMBURSEMENT_ACCOUNT, + value: {achData, shouldShowResetModal: false}, + }, + { + onyxMethod: 'set', + key: ONYXKEYS.REIMBURSEMENT_ACCOUNT_DRAFT, + value: null, + }], }); + + Navigation.navigate(ROUTES.getBankAccountRoute()); } export default resetFreePlanBankAccount;