All files / src/components/ProfilePage/BusinessProfileSetupNew/BusinessDetailsStep/hooks useBusinessProfileLinkRequest.tsx

66.66% Statements 8/12
33.33% Branches 3/9
60% Functions 3/5
66.66% Lines 8/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                      25x         3x       3x       3x 1x             3x       1x                                 3x    
import { showMessage } from "@common/Toast";
import { useGetMerchantById } from "@hooks/enterprise-api/account/useGetMerchants";
import { customInstance } from "@services/api";
import { useMutation } from "react-query";
 
type Props = {
  accID: number;
  merchantAccID: number;
  isPending?: boolean;
};
 
export const useBusinessProfileLinkRequest = ({
  accID, //legal entity owner // logged in
  merchantAccID, // opened merchant panel // requesting
  isPending = false,
}: Props) => {
  const { data: merchant } = useGetMerchantById({
    merchantId: isPending ? merchantAccID : undefined,
  });
 
  const legalEntityID = isPending
    ? merchant?.pendingLegalEntityID
    : merchant?.legalEntityID;
 
  const linkMutation = useMutation((payload: any) => {
    return customInstance({
      url: `/merchants/${accID}/legal-entities/${legalEntityID}/link`,
      method: "PATCH",
      data: payload,
    });
  });
 
  const handleUpdateLinkStatus = (
    status: "accepted" | "rejected",
    onSuccess?: () => void,
  ) => {
    linkMutation.mutate(
      { entityLinkStatus: status, merchantAccID },
      {
        onSuccess: () => {
          const message = `Linking request ${
            status === "accepted" ? "approved" : "declined"
          }.`;
          showMessage("Success", "", true, message, false);
          onSuccess && onSuccess();
        },
        onError: (err: any) => {
          showMessage("Error", err?.response?.data?.message);
        },
      },
    );
  };
 
  return { isLoading: linkMutation.isLoading, handleUpdateLinkStatus };
};