Skip to content

Commit

Permalink
code arrangement updates
Browse files Browse the repository at this point in the history
  • Loading branch information
keisyrzk committed Oct 16, 2023
1 parent 164ee64 commit c3b85d7
Showing 1 changed file with 72 additions and 77 deletions.
149 changes: 72 additions & 77 deletions src/pages/ReimbursementAccount/ReimbursementAccountPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ const defaultProps = {
},
};

const ROUTE_NAMES = {
COMPANY: 'company',
PERSONAL_INFORMATION: 'personal-information',
CONTRACT: 'contract',
VALIDATE: 'validate',
ENABLE: 'enable',
NEW: 'new',
};
function ReimbursementAccountPage({
reimbursementAccount,
route,
Expand All @@ -120,27 +128,71 @@ function ReimbursementAccountPage({
const {isOffline} = useNetwork();
const prevIsOfflineRef = useRef(isOffline);
const prevReimbursementAccountRef = useRef(reimbursementAccount);

const requestorStepRef = useRef(null);

// Will run whenever reimbursementAccount prop changes, and update the hasACHDataBeenLoaded state if necessary
useEffect(() => {
setHasACHDataBeenLoaded(reimbursementAccount !== ReimbursementAccountProps.reimbursementAccountDefaultProps);
}, [reimbursementAccount]);

useEffect(() => {
fetchData();
}, []); // The empty dependency array ensures this runs only once after the component mounts.

useEffect(() => {
// Check for network change from offline to online
if (prevIsOfflineRef.current && !isOffline && prevReimbursementAccountRef.current.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
fetchData();
}

if (!hasACHDataBeenLoaded) {
if (reimbursementAccount !== ReimbursementAccountProps.reimbursementAccountDefaultProps && !reimbursementAccount.isLoading) {
setShouldShowContinueSetupButton(getShouldShowContinueSetupButtonInitialValue());
setHasACHDataBeenLoaded(true);
}
return;
}

if (
prevReimbursementAccountRef.current.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE &&
reimbursementAccount.pendingAction !== prevReimbursementAccountRef.current.pendingAction
) {
setShouldShowContinueSetupButton(hasInProgressVBBA());
}

const currentStep = lodashGet(reimbursementAccount, 'achData.currentStep') || CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT;

if (shouldShowContinueSetupButton) {
return;
}

const currentStepRouteParam = getStepToOpenFromRouteParams();
if (currentStepRouteParam === currentStep) {
return;
}
if (currentStepRouteParam !== '') {
BankAccounts.hideBankAccountErrors();
}

const backTo = lodashGet(route.params, 'backTo');
const policyId = lodashGet(route.params, 'policyID');
Navigation.navigate(ROUTES.BANK_ACCOUNT_WITH_STEP_TO_OPEN.getRoute(getRouteForCurrentStep(currentStep), policyId, backTo));

// Update refs with current values
prevIsOfflineRef.current = isOffline;
prevReimbursementAccountRef.current = reimbursementAccount;
}, [isOffline, reimbursementAccount, route, hasACHDataBeenLoaded, shouldShowContinueSetupButton]);

const continueFunction = () => {
setShouldShowContinueSetupButton(false);
fetchData(true);
};

const ROUTE_NAMES = {
COMPANY: 'company',
PERSONAL_INFORMATION: 'personal-information',
CONTRACT: 'contract',
VALIDATE: 'validate',
ENABLE: 'enable',
NEW: 'new',
};


/**
* @param {String} fieldName
* @param {*} defaultValue
* @param {String} defaultValue
*
* @returns {*}
* @returns {String}
*/
const getDefaultStateForField = (fieldName, defaultValue = '') => {
return lodashGet(reimbursementAccount, ['achData', fieldName], defaultValue);
Expand Down Expand Up @@ -208,13 +260,6 @@ function ReimbursementAccountPage({
&& achData.state !== BankAccount.STATE.LOCKED;
};

const requestorStepRef = useRef(null);

// Will run whenever reimbursementAccount prop changes, and update the hasACHDataBeenLoaded state if necessary
useEffect(() => {
setHasACHDataBeenLoaded(reimbursementAccount !== ReimbursementAccountProps.reimbursementAccountDefaultProps);
}, [reimbursementAccount]);

/*
* Calculates the state used to show the "Continue with setup" view. If a bank account setup is already in progress and
* no specific further step was passed in the url we'll show the workspace bank account reset modal if the user wishes to start over
Expand All @@ -235,27 +280,23 @@ function ReimbursementAccountPage({
*/
const getStepToOpenFromRouteParams = () => {
switch (lodashGet(route, ['params', 'stepToOpen'], '')) {
case 'new':
case ROUTE_NAMES.NEW:
return CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT;
case 'company':
case ROUTE_NAMES.COMPANY:
return CONST.BANK_ACCOUNT.STEP.COMPANY;
case 'personal-information':
case ROUTE_NAMES.PERSONAL_INFORMATION:
return CONST.BANK_ACCOUNT.STEP.REQUESTOR;
case 'contract':
case ROUTE_NAMES.CONTRACT:
return CONST.BANK_ACCOUNT.STEP.ACH_CONTRACT;
case 'validate':
case ROUTE_NAMES.VALIDATE:
return CONST.BANK_ACCOUNT.STEP.VALIDATION;
case 'enable':
case ROUTE_NAMES.ENABLE:
return CONST.BANK_ACCOUNT.STEP.ENABLE;
default:
return '';
}
}

useEffect(() => {
fetchData();
}, []); // The empty dependency array ensures this runs only once after the component mounts.

/**
* Retrieve verified business bank account currently being set up.
* @param {boolean} ignoreLocalCurrentStep Pass true if you want the last "updated" view (from db), not the last "viewed" view (from onyx).
Expand All @@ -273,50 +314,6 @@ function ReimbursementAccountPage({
BankAccounts.openReimbursementAccountPage(stepToOpen, subStep, ignoreLocalCurrentStep ? '' : localCurrentStep);
};

useEffect(() => {
// Check for network change from offline to online
if (prevIsOfflineRef.current && !isOffline && prevReimbursementAccountRef.current.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
fetchData();
}

if (!hasACHDataBeenLoaded) {
if (reimbursementAccount !== ReimbursementAccountProps.reimbursementAccountDefaultProps && !reimbursementAccount.isLoading) {
setShouldShowContinueSetupButton(getShouldShowContinueSetupButtonInitialValue());
setHasACHDataBeenLoaded(true);
}
return;
}

if (
prevReimbursementAccountRef.current.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE &&
reimbursementAccount.pendingAction !== prevReimbursementAccountRef.current.pendingAction
) {
setShouldShowContinueSetupButton(hasInProgressVBBA());
}

const currentStep = lodashGet(reimbursementAccount, 'achData.currentStep') || CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT;

if (shouldShowContinueSetupButton) {
return;
}

const currentStepRouteParam = getStepToOpenFromRouteParams();
if (currentStepRouteParam === currentStep) {
return;
}
if (currentStepRouteParam !== '') {
BankAccounts.hideBankAccountErrors();
}

const backTo = lodashGet(route.params, 'backTo');
const policyId = lodashGet(route.params, 'policyID');
Navigation.navigate(ROUTES.BANK_ACCOUNT_WITH_STEP_TO_OPEN.getRoute(getRouteForCurrentStep(currentStep), policyId, backTo));

// Update refs with current values
prevIsOfflineRef.current = isOffline;
prevReimbursementAccountRef.current = reimbursementAccount;
}, [isOffline, reimbursementAccount, route, hasACHDataBeenLoaded, shouldShowContinueSetupButton]);

/**
* @param {String} currentStep
* @returns {String}
Expand All @@ -339,9 +336,6 @@ function ReimbursementAccountPage({
}
}


// Rendering

if (_.isEmpty(policy) || !PolicyUtils.isPolicyAdmin(policy)) {
return (
<ScreenWrapper testID={ReimbursementAccountPage.displayName}>
Expand Down Expand Up @@ -489,6 +483,7 @@ function ReimbursementAccountPage({

ReimbursementAccountPage.propTypes = propTypes;
ReimbursementAccountPage.defaultProps = defaultProps;
ReimbursementAccountPage.displayName = 'ReimbursementAccountPage';

export default compose(
withOnyx({
Expand Down

0 comments on commit c3b85d7

Please sign in to comment.