Skip to content

Commit

Permalink
Clean up vbba code and save each substep
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuoch committed Mar 8, 2024
1 parent 15cf6ad commit 9f07370
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 37 deletions.
6 changes: 4 additions & 2 deletions src/hooks/useSubStep/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import type {SubStepProps, UseSubStep} from './types';
* @param bodyContent - array of components to display in particular step
* @param onFinished - callback triggered after finish last step
* @param startFrom - initial index for bodyContent array
* @param onNextSubStep - callback triggered after finish each step
*/
export default function useSubStep<TProps extends SubStepProps>({bodyContent, onFinished, startFrom = 0}: UseSubStep<TProps>) {
export default function useSubStep<TProps extends SubStepProps>({bodyContent, onFinished, startFrom = 0, onNextSubStep = () => {}}: UseSubStep<TProps>) {
const [screenIndex, setScreenIndex] = useState(startFrom);
const isEditing = useRef(false);

Expand Down Expand Up @@ -35,9 +36,10 @@ export default function useSubStep<TProps extends SubStepProps>({bodyContent, on
if (nextScreenIndex === bodyContent.length) {
onFinished();
} else {
onNextSubStep();
setScreenIndex(nextScreenIndex);
}
}, [screenIndex, bodyContent.length, onFinished]);
}, [screenIndex, bodyContent.length, onFinished, onNextSubStep]);

const moveTo = useCallback((step: number) => {
isEditing.current = true;
Expand Down
3 changes: 3 additions & 0 deletions src/hooks/useSubStep/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type UseSubStep<TProps extends SubStepProps> = {
/** array of components that will become sub steps */
bodyContent: Array<ComponentType<SubStepProps & TProps>>;

/** called after each sub step */
onNextSubStep?: () => void;

/** called on last sub step */
onFinished: () => void;

Expand Down
2 changes: 1 addition & 1 deletion src/libs/API/parameters/AcceptACHContractForBankAccount.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {ACHContractStepProps} from '@src/types/form/ReimbursementAccountForm';

type AcceptACHContractForBankAccount = ACHContractStepProps & {bankAccountID: number; policyID: string; canUseNewVbbaFlow?: boolean};
type AcceptACHContractForBankAccount = ACHContractStepProps & {bankAccountID: number; policyID: string};

export default AcceptACHContractForBankAccount;
1 change: 0 additions & 1 deletion src/libs/API/parameters/ConnectBankAccountParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ type ConnectBankAccountParams = {
plaidMask?: string;
isSavings?: boolean;
policyID?: string;
canUseNewVbbaFlow?: boolean;
};

export default ConnectBankAccountParams;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ type OpenReimbursementAccountPageParams = {
stepToOpen: ReimbursementAccountStep;
subStep: ReimbursementAccountSubStep;
localCurrentStep: ReimbursementAccountStep;
canUseNewVbbaFlow?: boolean;
policyID: string;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {ACHContractStepProps} from '@src/types/form/ReimbursementAccountForm';
import type {BeneficialOwnersStepProps} from '@src/types/form/ReimbursementAccountForm';

type UpdateBeneficialOwnersForBankAccountParams = Partial<ACHContractStepProps> & {bankAccountID: number; policyID: string; canUseNewVbbaFlow?: boolean};
type UpdateBeneficialOwnersForBankAccountParams = Partial<BeneficialOwnersStepProps> & {bankAccountID: number; policyID: string};

export default UpdateBeneficialOwnersForBankAccountParams;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import type {BankAccountStepProps, CompanyStepProps, ReimbursementAccountProps}

type BankAccountCompanyInformation = BankAccountStepProps & CompanyStepProps & ReimbursementAccountProps;

type UpdateCompanyInformationForBankAccountParams = Partial<BankAccountCompanyInformation> & {bankAccountID: number; policyID: string; canUseNewVbbaFlow?: boolean};
type UpdateCompanyInformationForBankAccountParams = Partial<BankAccountCompanyInformation> & {bankAccountID: number; policyID: string; confirm: boolean};

export default UpdateCompanyInformationForBankAccountParams;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {RequestorStepProps} from '@src/types/form/ReimbursementAccountForm';

type UpdatePersonalInformationForBankAccountParams = RequestorStepProps & {bankAccountID: number; policyID: string; canUseNewVbbaFlow: boolean};
type UpdatePersonalInformationForBankAccountParams = RequestorStepProps & {bankAccountID: number; policyID: string; confirm: boolean};

export default UpdatePersonalInformationForBankAccountParams;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ type VerifyIdentityForBankAccountParams = {
bankAccountID: number;
onfidoData: string;
policyID: string;
canUseNewVbbaFlow?: boolean;
};
export default VerifyIdentityForBankAccountParams;
18 changes: 8 additions & 10 deletions src/libs/actions/BankAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ function connectBankAccountWithPlaid(bankAccountID: number, selectedPlaidBankAcc
plaidAccessToken: selectedPlaidBankAccount.plaidAccessToken,
plaidMask: selectedPlaidBankAccount.mask,
isSavings: selectedPlaidBankAccount.isSavings,
canUseNewVbbaFlow: true,
policyID,
};

Expand Down Expand Up @@ -254,15 +253,17 @@ function deletePaymentBankAccount(bankAccountID: number) {
* This action is called by the requestor step in the Verified Bank Account flow
* @param bankAccountID - ID for bank account
* @param params - User personal data
* @param policyID - ID of the policy we're setting the bank account on
* @param isConfirmPage - If we're submitting from the confirmation substep, to trigger all external checks
*/
function updatePersonalInformationForBankAccount(bankAccountID: number, params: RequestorStepProps, policyID: string) {
function updatePersonalInformationForBankAccount(bankAccountID: number, params: RequestorStepProps, policyID: string, isConfirmPage: boolean) {
API.write(
WRITE_COMMANDS.UPDATE_PERSONAL_INFORMATION_FOR_BANK_ACCOUNT,
{
...params,
bankAccountID,
policyID,
canUseNewVbbaFlow: true,
confirm: isConfirmPage,
},
getVBBADataForOnyx(CONST.BANK_ACCOUNT.STEP.REQUESTOR),
);
Expand Down Expand Up @@ -356,7 +357,6 @@ function openReimbursementAccountPage(stepToOpen: ReimbursementAccountStep, subS
subStep,
localCurrentStep,
policyID,
canUseNewVbbaFlow: true,
};

return API.read(READ_COMMANDS.OPEN_REIMBURSEMENT_ACCOUNT_PAGE, parameters, onyxData);
Expand All @@ -365,15 +365,17 @@ function openReimbursementAccountPage(stepToOpen: ReimbursementAccountStep, subS
/**
* Updates the bank account in the database with the company step data
* @param params - Business step form data
* @param policyID - ID of the policy we're setting the bank account on
* @param isConfirmPage - If we're submitting from the confirmation substep, to trigger all external checks
*/
function updateCompanyInformationForBankAccount(bankAccountID: number, params: Partial<CompanyStepProps>, policyID: string) {
function updateCompanyInformationForBankAccount(bankAccountID: number, params: Partial<CompanyStepProps>, policyID: string, isConfirmPage: boolean) {
API.write(
WRITE_COMMANDS.UPDATE_COMPANY_INFORMATION_FOR_BANK_ACCOUNT,
{
...params,
bankAccountID,
policyID,
canUseNewVbbaFlow: true,
confirm: isConfirmPage,
},
getVBBADataForOnyx(CONST.BANK_ACCOUNT.STEP.COMPANY),
);
Expand All @@ -390,7 +392,6 @@ function updateBeneficialOwnersForBankAccount(bankAccountID: number, params: Par
...params,
bankAccountID,
policyID,
canUseNewVbbaFlow: true,
},
getVBBADataForOnyx(),
);
Expand All @@ -407,7 +408,6 @@ function acceptACHContractForBankAccount(bankAccountID: number, params: ACHContr
...params,
bankAccountID,
policyID,
canUseNewVbbaFlow: true,
},
getVBBADataForOnyx(),
);
Expand All @@ -426,7 +426,6 @@ function connectBankAccountManually(bankAccountID: number, bankAccount: PlaidBan
plaidAccessToken: bankAccount.plaidAccessToken,
plaidMask: bankAccount.mask,
isSavings: bankAccount.isSavings,
canUseNewVbbaFlow: true,
policyID,
};

Expand All @@ -441,7 +440,6 @@ function verifyIdentityForBankAccount(bankAccountID: number, onfidoData: Record<
bankAccountID,
onfidoData: JSON.stringify(onfidoData),
policyID,
canUseNewVbbaFlow: true,
};

API.write(WRITE_COMMANDS.VERIFY_IDENTITY_FOR_BANK_ACCOUNT, parameters, getVBBADataForOnyx());
Expand Down
38 changes: 25 additions & 13 deletions src/pages/ReimbursementAccount/BusinessInfo/BusinessInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,34 @@ function BusinessInfo({reimbursementAccount, reimbursementAccountDraft, onBackBu
const policyID = reimbursementAccount?.achData?.policyID ?? '';
const values = useMemo(() => getSubstepValues(BUSINESS_INFO_STEP_KEYS, reimbursementAccountDraft, reimbursementAccount), [reimbursementAccount, reimbursementAccountDraft]);

const submit = useCallback(() => {
BankAccounts.updateCompanyInformationForBankAccount(
Number(reimbursementAccount?.achData?.bankAccountID ?? '0'),
{
...values,
...getBankAccountFields(['routingNumber', 'accountNumber', 'bankName', 'plaidAccountID', 'plaidAccessToken', 'isSavings']),
companyTaxID: values.companyTaxID?.replace(CONST.REGEX.NON_NUMERIC, ''),
companyPhone: parsePhoneNumber(values.companyPhone ?? '', {regionCode: CONST.COUNTRY.US}).number?.significant,
},
policyID,
);
}, [reimbursementAccount, values, getBankAccountFields, policyID]);
const submit = useCallback(
(isConfirmPage: boolean) => {
BankAccounts.updateCompanyInformationForBankAccount(
Number(reimbursementAccount?.achData?.bankAccountID ?? '0'),
{
...values,
...getBankAccountFields(['routingNumber', 'accountNumber', 'bankName', 'plaidAccountID', 'plaidAccessToken', 'isSavings']),
companyTaxID: values.companyTaxID?.replace(CONST.REGEX.NON_NUMERIC, ''),
companyPhone: parsePhoneNumber(values.companyPhone ?? '', {regionCode: CONST.COUNTRY.US}).number?.significant,
},
policyID,
isConfirmPage,
);
},
[reimbursementAccount, values, getBankAccountFields, policyID],
);

const startFrom = useMemo(() => getInitialSubstepForBusinessInfo(values), [values]);

const {componentToRender: SubStep, isEditing, screenIndex, nextScreen, prevScreen, moveTo, goToTheLastStep} = useSubStep({bodyContent, startFrom, onFinished: submit});
const {
componentToRender: SubStep,
isEditing,
screenIndex,
nextScreen,
prevScreen,
moveTo,
goToTheLastStep,
} = useSubStep({bodyContent, startFrom, onFinished: () => submit(true), onNextSubStep: () => submit(false)});

const handleBackButtonPress = () => {
if (isEditing) {
Expand Down
19 changes: 15 additions & 4 deletions src/pages/ReimbursementAccount/PersonalInfo/PersonalInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,23 @@ function PersonalInfo({reimbursementAccount, reimbursementAccountDraft, onBackBu
const policyID = reimbursementAccount?.achData?.policyID ?? '';
const values = useMemo(() => getSubstepValues(PERSONAL_INFO_STEP_KEYS, reimbursementAccountDraft, reimbursementAccount), [reimbursementAccount, reimbursementAccountDraft]);
const bankAccountID = Number(reimbursementAccount?.achData?.bankAccountID ?? '0');
const submit = useCallback(() => {
BankAccounts.updatePersonalInformationForBankAccount(bankAccountID, {...values}, policyID);
}, [values, bankAccountID, policyID]);
const submit = useCallback(
(isConfirmPage: boolean) => {
BankAccounts.updatePersonalInformationForBankAccount(bankAccountID, {...values}, policyID, isConfirmPage);
},
[values, bankAccountID, policyID],
);
const startFrom = useMemo(() => getInitialSubstepForPersonalInfo(values), [values]);

const {componentToRender: SubStep, isEditing, screenIndex, nextScreen, prevScreen, moveTo, goToTheLastStep} = useSubStep({bodyContent, startFrom, onFinished: submit});
const {
componentToRender: SubStep,
isEditing,
screenIndex,
nextScreen,
prevScreen,
moveTo,
goToTheLastStep,
} = useSubStep({bodyContent, startFrom, onFinished: () => submit(true), onNextSubStep: () => submit(false)});

const handleBackButtonPress = () => {
if (isEditing) {
Expand Down

0 comments on commit 9f07370

Please sign in to comment.