Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RestartBankAccountSetup #9831

Merged
merged 3 commits into from
Jul 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;