Skip to content

Commit

Permalink
Merge pull request #9831 from Expensify/maxence-rrbas
Browse files Browse the repository at this point in the history
Refactor RestartBankAccountSetup
  • Loading branch information
ctkochan22 committed Jul 28, 2022
2 parents 1857e44 + aa15907 commit b7367fb
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions src/libs/actions/ReimbursementAccount/resetFreePlanBankAccount.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;

0 comments on commit b7367fb

Please sign in to comment.