All files / src/features/GiveOnboarding/pages/Business/Owners index.tsx

92.3% Statements 12/13
92.3% Branches 12/13
83.33% Functions 5/6
91.66% Lines 11/12

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                                                          10x   11x   11x   11x 11x                                     2x                                                                                                                   8x             8x     8x                             19x           10x      
import { IStep } from "@features/GiveOnboarding/types";
import { BusinessOwnersForm } from "./Form";
import { Box } from "@mui/material";
import { styled } from "@theme/v2/Provider";
import {
  IBusinessAddressFormValues,
  IBusinessOwners,
} from "@features/GiveOnboarding/types/form";
import { BusinessOwnerItem } from "./BusinessOwnerItem";
import { forwardRef } from "react";
import { NewOnboardingTBusinessOwner } from "@components/common/BusinessOwners/types";
import GiveButton from "@shared/Button/GiveButton";
import {
  useDeleteBusinessOwner,
  useToggleForm,
} from "@features/GiveOnboarding/hooks/businessOwners";
import { PagesContainer } from "../../pages.atoms";
 
interface Props {
  data_key: IStep;
  apiData: {
    business_owners: IBusinessOwners;
    business_address: IBusinessAddressFormValues;
    type?: string;
  };
  legalEntityID: number;
  merchantId: string;
}
 
const BusinessOwners = forwardRef(
  ({ data_key, apiData, legalEntityID, merchantId }: Props, ref: any) => {
    const { userPressAddBusinessOwner, onToggleForm } = useToggleForm(ref);
 
    const { business_owners, business_address, type } = apiData;
 
    const isSole = "individual_sole_proprietorship" === type;
    return (
      <PagesContainer>
        {business_owners.list.length > 0 && (
          <>
            <BusinessOwnerList
              onToggleForm={onToggleForm}
              owners={business_owners.list}
              businessType={type || ""}
              legalEntityID={legalEntityID}
              merchantId={merchantId}
            />
            {!userPressAddBusinessOwner && !isSole && (
              <Box
                justifyContent="center"
                alignItems="center"
                display="flex"
                width="100%"
              >
                <GiveButton
                  onClick={() => onToggleForm(undefined, true)}
                  label="Add business owner"
                  variant="filled"
                  color="light"
                  size="small"
                />
              </Box>
            )}
          </>
        )}
 
        <StyledBox
          p={2}
          sx={{
            visibility: userPressAddBusinessOwner
              ? "visible"
              : business_owners.list.length === 0
              ? "visible"
              : "hidden",
          }}
        >
          <BusinessOwnersForm
            data_key={data_key}
            businessOwnersLength={business_owners.list.length}
            userPressAddBusinessOwner={userPressAddBusinessOwner}
            onToggleForm={onToggleForm}
            ref={ref}
            hideForm={
              business_owners.list.length === 0
                ? false
                : !userPressAddBusinessOwner
            }
            legalEntityID={legalEntityID}
            businessAddress={business_address}
            type={type}
          />
        </StyledBox>
      </PagesContainer>
    );
  },
);
 
function BusinessOwnerList({
  owners,
  businessType,
  onToggleForm,
  legalEntityID,
  merchantId,
}: {
  owners: NewOnboardingTBusinessOwner[];
  businessType: string;
  legalEntityID: number;
  merchantId: string;
  onToggleForm: (
    formData?: NewOnboardingTBusinessOwner,
    newFormViewStatus?: boolean,
  ) => void;
}) {
  const { handleDeleteBusinessOwner } = useDeleteBusinessOwner({
    legalEntityID,
    merchantId,
    onSuccess: () => {
      onToggleForm();
    },
  });
  return (
    <>
      {owners.map((owner) => {
        return (
          <StyledBox key={owner.id} sx={{ width: "100%" }}>
            <BusinessOwnerItem
              owner={owner}
              businessType={businessType}
              onEdit={onToggleForm}
              onDelete={handleDeleteBusinessOwner}
            />
          </StyledBox>
        );
      })}
    </>
  );
}
 
const StyledBox = styled(Box)(({ theme }) => ({
  padding: " 16px 20px",
  borderRadius: theme.customs?.radius.medium,
  border: `1px solid ${theme.palette?.border?.primary}`,
}));
 
BusinessOwners.displayName = "BusinessOwners";
 
export { BusinessOwners };