All files / src/hooks/common useHandleLogout.ts

43.58% Statements 17/39
100% Branches 0/0
37.5% Functions 3/8
43.58% Lines 17/39

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                                  2x 2x 2x 2x   2x                     2x           2x 2x 2x 2x 2x   2x                     2x               2x                 2x             2x 2x    
import { showMessage } from "@common/Toast";
import { useModal } from "@ebay/nice-modal-react";
import { useAppDispatch } from "@redux/hooks";
import { logoutAction } from "@redux/slices/auth/auth";
import { resetConfigurationToDefault } from "@redux/slices/enterprise/enterpriseSettings";
import { customInstance } from "@services/api";
import Cookies from "js-cookie";
import { useMutation, useQueryClient } from "react-query";
import { useNavigate } from "react-router-dom";
import * as Sentry from "@sentry/react";
import { DRAWER_STATE_KEY } from "@constants/constants";
import { useSessionTimeoutWithWorker } from "@hooks/useSessionTimeoutWithWorker";
import {
  STORED_MERCHANTS_ROWS_PER_PAGE,
  STORED_PROVIDER_ROWS_PER_PAGE,
} from "@constants/stringConstants";
 
export const useHandleLogout = () => {
  const { mutateAsync, ...rest } = useLogout();
  const { handleReset } = useResetApp();
  const modal = useModal();
 
  const handleSubmit = async () => {
    try {
      await mutateAsync();
      Sentry.setUser(null);
      modal.hide();
      handleReset();
    } catch (error) {
      showMessage("Error", "Error when trying to log out. Try again Later");
    }
  };
 
  return {
    submitLogout: handleSubmit,
    ...rest,
  };
};
 
export const useResetApp = () => {
  const navigate = useNavigate();
  const queryClient = useQueryClient();
  const dispatch = useAppDispatch();
  const { clearSessionData } = useSessionTimeoutWithWorker();
 
  const clearLocalData = () => {
    localStorage.removeItem("selected-account");
    localStorage.removeItem("masquerade-mode");
    localStorage.removeItem(DRAWER_STATE_KEY);
    localStorage.removeItem(STORED_MERCHANTS_ROWS_PER_PAGE);
    localStorage.removeItem(STORED_PROVIDER_ROWS_PER_PAGE);
    clearSessionData(); // Clear session timeout data
    Cookies.remove("user");
    Cookies.remove("transfer-verification");
  };
 
  const handleReset = () => {
    clearLocalData();
    dispatch(logoutAction());
    dispatch(resetConfigurationToDefault());
    queryClient.removeQueries();
    navigate("/login");
  };
 
  return {
    handleReset,
    clearLocalData: () => {
      clearLocalData();
      dispatch(resetConfigurationToDefault());
    },
  };
};
 
const _logout = () => {
  return customInstance({
    url: `/signout`,
    method: "POST",
  });
};
 
export const useLogout = () => {
  return useMutation(_logout);
};