All files / src/features/GiveOnboarding/hooks/businessOwners index.ts

75% Statements 15/20
33.33% Branches 1/3
80% Functions 4/5
75% Lines 15/20

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                        10x 11x     11x   11x       2x         2x           2x 2x       11x     10x                 8x   8x                   2x       2x                               8x    
import { updateFormData } from "@redux/slices/onboardingWizard";
import { useAppDispatch } from "@redux/hooks";
import { useCallback, useState } from "react";
import { NewOnboardingTBusinessOwner } from "@common/BusinessOwners/types";
import { parseApiDataToFormData } from "@features/GiveOnboarding/pages/Business/Owners/util";
import { defaultValues } from "@features/GiveOnboarding/pages/Business/Owners/consts";
import { buildMerchantEndpoints } from "@services/api/utils.api";
import { DELETE_CONFIRMATION_MODAL } from "modals/modal_names";
import NiceModal from "@ebay/nice-modal-react";
import { useQueryClient } from "react-query";
import { QKEY_BUSINESS_PROFILE_BY_ID } from "@constants/queryKeys";
 
export const useToggleForm = (ref: any) => {
  const dispatch = useAppDispatch();
 
  const [userPressAddBusinessOwner, useUserPressAddBusinessOwner] =
    useState(false);
 
  const onToggleForm = (
    ownerData?: NewOnboardingTBusinessOwner,
    newFormViewStatus = false,
  ) => {
    Iif (ownerData) {
      const formData = parseApiDataToFormData(ownerData);
      useUserPressAddBusinessOwner(true);
      ref?.current?.methods?.reset(formData);
    } else {
      dispatch(
        updateFormData({
          key: "business_owners",
          data: {},
        }),
      );
      useUserPressAddBusinessOwner(newFormViewStatus);
      ref?.current?.methods?.reset(defaultValues);
    }
  };
 
  return { userPressAddBusinessOwner, onToggleForm };
};
 
export const useDeleteBusinessOwner = ({
  legalEntityID,
  merchantId,
  onSuccess,
}: {
  legalEntityID: number;
  merchantId: string;
  onSuccess: () => void;
}) => {
  const client = useQueryClient();
 
  const handleDeleteBusinessOwner = useCallback(
    ({
      ownerID,
      firstName,
      lastName,
    }: {
      ownerID: number | string;
      firstName: string;
      lastName: string;
    }) => {
      const url = buildMerchantEndpoints(
        `legal-entities/${legalEntityID}/principals/${ownerID}`,
        merchantId,
      );
      NiceModal.show(DELETE_CONFIRMATION_MODAL, {
        variant: "owner",
        itemName: `${firstName} ${lastName}`,
        url: url,
        onSuccess: async () => {
          await client.invalidateQueries([
            QKEY_BUSINESS_PROFILE_BY_ID,
            legalEntityID,
          ]);
          onSuccess(); // if the form is opened as edit with the deleted data, is better to clear the form, even close it
        },
      });
    },
    [legalEntityID, merchantId],
  );
 
  return { handleDeleteBusinessOwner };
};