All files / src/hooks useInitialLogin.ts

0% Statements 0/32
0% Branches 0/14
0% Functions 0/7
0% Lines 0/32

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                                                                                                                                                                         
import { useEffect } from "react";
import Cookies from "js-cookie";
import { useAppDispatch } from "@redux/hooks";
import { setSelectedAccount } from "@redux/slices/auth/accounts";
import { login } from "@redux/slices/auth/auth";
import { useResetApp } from "./common/useHandleLogout";
import { getAccounts } from "@services/api/onboarding/user";
import { TAccounts } from "@customTypes/accounts.types";
import { accountsParser } from "@pages/Login/modals/hooks/useAccountSelectionModal";
import { safeParse } from "@utils/index";
import { customInstance } from "@services/api";
 
const useInitialLogin = () => {
  const dispatch = useAppDispatch();
  const { handleReset } = useResetApp();
 
  const onError = () => {
    handleReset();
  };
 
  const selectedAccount = localStorage.getItem("selected-account");
 
  const dispatchLogin = (user: any) =>
    dispatch(
      login({
        ...user,
        globalName: {
          firstName: "",
          lastName: "",
          phoneNumber: "",
        },
      }),
    );
 
  useEffect(() => {
    //we need to fetch the accounts data inside useEffect to avoid problems with dependencies
    //we need to fetch all accounts bc BE does not allow to fetch single by id
    const handleFetchAccountsData = async () => {
      try {
        const user = Cookies.get("user");
        if (user) {
          const userOBJ = safeParse(user);
          if (!userOBJ) {
            onError();
            return;
          }
          dispatchLogin(userOBJ);
          if (selectedAccount) {
            const response = await getAccounts();
            const data = response.data;
            const usedAccount = data.find(
              (acc: TAccounts) => Number(acc.id) === Number(selectedAccount),
            );
            if (!usedAccount) {
              onError();
              return;
            }
            // Fire-and-forget PUT to detect if the account was closed after
            // the session cookie was set. The Axios interceptor will call
            // window.location.assign("/closed-account") on a 403, so we
            // don't need to block account setup on the response.
            if (window.location.pathname !== "/closed-account") {
              customInstance({
                url: `/accounts/${usedAccount.id}`,
                method: "PUT",
              }).catch(() => {
                // Closed-account redirect is handled by the Axios interceptor.
              });
            }
            //account parser accepts and returns array
            const parsedAccount =
              usedAccount && accountsParser([usedAccount], userOBJ.email);
            parsedAccount && dispatch(setSelectedAccount(parsedAccount[0]));
          }
        }
      } catch (error) {
        onError();
      }
    };
    handleFetchAccountsData();
  }, []);
};
 
export default useInitialLogin;