All files / src/componentsV2/AccountProfile/hooks useAccountProfile.tsx

64.7% Statements 11/17
33.33% Branches 1/3
40% Functions 2/5
64.7% Lines 11/17

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                            1x 25x 25x 25x   25x   25x             25x             25x                 25x 15x     25x    
import { useProfileForms } from "@components/ProfileMenu/hooks/useProfileForms";
import { TProfileTab } from "../AccountProfileModal/types";
import TFAModalWrapper from "@components/ProfileMenu/modals/ProfileModal2FAWrapper";
import NiceModal from "@ebay/nice-modal-react";
import { selectUser } from "@redux/slices/auth/auth";
import { useAppSelector } from "@redux/hooks";
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import { useEffect } from "react";
import { SECURITY_CHECK_MODAL } from "modals/modal_names";
 
type Props = {
  activeTab: TProfileTab | "";
};
 
export const useAccountProfile = ({ activeTab }: Props) => {
  const form = useProfileForms(activeTab);
  const { open } = useNiceModal();
  const { onSubmit, methods = {} } = form;
 
  const { email } = useAppSelector(selectUser);
 
  const trigger2FA = () => {
    NiceModal.show(TFAModalWrapper, {
      email: email,
      onSuccessFn: methods?.handleSubmit?.(onSubmit),
    });
  };
 
  const handleClickSubmit = () => {
    NiceModal.show(SECURITY_CHECK_MODAL, {
      variant: "changePassword",
      onConfirm: trigger2FA,
    });
  };
 
  const handleSubmit = (e: any) => {
    e?.preventDefault();
    if (activeTab === "Security") {
      return handleClickSubmit();
    } else {
      methods?.handleSubmit?.(onSubmit).call();
    }
  };
 
  useEffect(() => {
    methods?.reset?.();
  }, [open, activeTab]);
 
  return { form, handleSubmit };
};