All files / src/components/common/BusinessProfileInputs GiveOwnershipTypeSelect.tsx

72.72% Statements 16/22
53.84% Branches 7/13
100% Functions 3/3
72.72% Lines 16/22

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                          47x           295x     295x 295x   295x 51x   14x 14x           37x   51x 50x 50x   1x                 1x       295x         556x                  
import React from "react";
import { useFormContext } from "react-hook-form";
import HFGiveSelect from "@shared/HFInputs/HFGiveSelect/HFGiveSelect";
 
type GiveOwnershipTypeSelectProps = {
  name: string;
  businessTypeName?: string;
  label: string;
  disabled?: boolean;
  initialValue?: "private" | "public";
  hasArrowDownIcon?: boolean;
};
 
const GiveOwnershipTypeSelect = ({
  name,
  label,
  disabled,
  businessTypeName = "businessDetails.businessType",
}: GiveOwnershipTypeSelectProps) => {
  const [allowedOwnershipTypes, setAllowedOwnershipTypes] = React.useState<
    string[]
  >(["Private", "Public"]);
  const { watch, setValue } = useFormContext();
  const isMount = React.useRef(true);
 
  React.useEffect(() => {
    switch (watch(businessTypeName)) {
      case "individual_sole_proprietorship":
        setAllowedOwnershipTypes(["Private"]);
        break;
      case "tax_exempt_organization":
      case "government_agency":
        setAllowedOwnershipTypes(["Public"]);
        break;
      default:
        setAllowedOwnershipTypes(["Public", "Private"]);
    }
    if (isMount.current) {
      isMount.current = false;
      return;
    }
    switch (watch(businessTypeName)) {
      case "individual_sole_proprietorship":
        setValue(name, "private");
        break;
      case "tax_exempt_organization":
      case "government_agency":
        setValue(name, "public");
        break;
      default:
        setValue(name, "private");
    }
  }, [watch(businessTypeName)]);
 
  return (
    <HFGiveSelect
      name={name}
      label={label}
      disabled={disabled || allowedOwnershipTypes.length === 1}
      options={allowedOwnershipTypes.map((item) => ({
        value: item.toLowerCase(),
        label: item,
      }))}
    />
  );
};
 
export default GiveOwnershipTypeSelect;