All files / src/features/GiveOnboarding/pages/Bank/Connect Form.tsx

79.16% Statements 19/24
90% Branches 9/10
28.57% Functions 2/7
79.16% Lines 19/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198                                                                    10x 10x 10x   10x 17x     10x         10x       10x                                                           10x             10x           10x           10x 10x 10x 10x 10x   10x   10x                                                                                                                                                                           10x      
import { forwardRef } from "react";
import { Grid } from "@mui/material";
import {
  DeepPartial,
  DeepMap,
  FormProvider,
  SubmitHandler,
} from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
 
import { StepViewContainer } from "@features/GiveOnboarding/container/StepViewContainer";
import { useGetCurrentMerchantId } from "@hooks/common";
 
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import HFGiveSelect from "@shared/HFInputs/HFGiveSelect/HFGiveSelect";
 
import { IStep } from "@features/GiveOnboarding/types";
import { useFormStep } from "@features/GiveOnboarding/hooks/useFormStep";
import { IBusinessBankAccountConnectFormValues } from "@features/GiveOnboarding/types/form";
import { getSchema } from "./schema";
import { useAccessControl } from "features/Permissions/AccessControl";
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
import { useBankAccountConnect } from "./useBankAccountConnect";
import { normalizeInput } from "./utils";
import { useSelector } from "react-redux";
import { RootState } from "@redux/types/store";
import { GiveBankProviderText } from "@components/BankAccounts/BankProviderText";
 
interface Props {
  data_key: IStep;
  bankData: any;
  legalEntityID?: number;
}
 
const BankConnect = forwardRef((props: Props, ref: any) => {
  const { merchantId } = useGetCurrentMerchantId();
  const { bankData } = props;
 
  const globalFormData = useSelector(
    (state: RootState) => state.onboardingWizard.formData,
  );
 
  const { onSubmitInformation, defaultValues } = useBankAccountConnect({
    merchantId,
    firstAccount: bankData,
  });
 
  const schema = getSchema({
    firstAccount: bankData,
  });
 
  const handleSubmit = async (
    formData: {
      data: IBusinessBankAccountConnectFormValues;
      dirtyFields: Partial<
        Readonly<
          DeepMap<DeepPartial<IBusinessBankAccountConnectFormValues>, boolean>
        >
      >;
    },
    onParentValid?: SubmitHandler<IBusinessBankAccountConnectFormValues>,
    onParentInvalid?: (error: any) => void,
  ) => {
    return await onSubmitInformation(
      formData,
      (data, apiData) => {
        onParentValid?.({
          ...formData.data,
          bankDataAux: {
            ...globalFormData?.business_bank_connect?.bankDataAux,
            id: apiData.id,
            isLinked: false,
          },
        } as any); // Signal success to parent
      },
      (apiError) => {
        onParentInvalid?.({ type: "api", error: apiError }); // Signal API useStateerror to parent
      },
    );
  };
 
  const { methods } = useFormStep<IBusinessBankAccountConnectFormValues>(ref, {
    data_key: props.data_key,
    onSubmitStepData: handleSubmit,
    resolver: yupResolver(schema),
    defaultValues,
  });
 
  const isAddAllowed = useAccessControl({
    resource: RESOURCE_BASE.BANK_ACCOUNT,
    operation: OPERATIONS.CREATE,
    withPortal: true,
  });
 
  const isEditAllowed = useAccessControl({
    resource: RESOURCE_BASE.BANK_ACCOUNT,
    operation: OPERATIONS.UPDATE,
    withPortal: true,
  });
 
  const isLinked = bankData?.isLinked;
  const isApproved = bankData?.status === "approved";
  const cantAdd = !bankData?.id && !isAddAllowed;
  const cantEdit = (bankData?.id && !isEditAllowed) || isLinked;
  const isInputDisabled = cantAdd || cantEdit || isApproved;
 
  const routingNumber = methods.watch("routingNumber");
 
  return (
    <FormProvider {...methods}>
      <StepViewContainer>
        <Grid container spacing={3}>
          <Grid item xs={12} md={4}>
            <HFGiveSelect
              name="type"
              label="Account type"
              options={[
                {
                  value: "checking",
                  label: "Checking",
                },
                {
                  value: "savings",
                  label: "Savings",
                },
              ]}
              disabled={isInputDisabled}
            />
          </Grid>
          <Grid item xs={12} md={8}>
            <HFGiveInput
              name="name"
              fullWidth
              type="text"
              label="Business Name on Account"
              disabled={isInputDisabled}
            />
          </Grid>
        </Grid>
        <Grid container spacing={3}>
          <Grid item xs={12} md={4}>
            <HFGiveInput
              name="routingNumber"
              fullWidth
              type="string"
              label="Routing Number"
              inputProps={{ maxLength: 9 }}
              disabled={cantAdd || cantEdit}
              onChange={(e) => {
                methods.setValue(
                  "routingNumber",
                  normalizeInput(e.target.value),
                  {
                    shouldDirty: true,
                    shouldTouch: true,
                  },
                );
              }}
            />
            <GiveBankProviderText routingNumber={routingNumber} />
          </Grid>
          <Grid item xs={12} md={8}>
            <HFGiveInput
              name="accountNumber"
              fullWidth
              type="string"
              label="Account Number"
              disabled={isInputDisabled}
              onChange={(e) => {
                methods.setValue(
                  "accountNumber",
                  normalizeInput(e.target.value),
                  {
                    shouldDirty: true,
                    shouldTouch: true,
                  },
                );
              }}
            />
          </Grid>
        </Grid>
        <HFGiveInput
          fullWidth
          label="Notes (Optional)"
          name="notes"
          maxLength={400}
          multiline
          rows={6}
        />
      </StepViewContainer>
    </FormProvider>
  );
});
 
BankConnect.displayName = "BankConnect";
 
export { BankConnect };