All files / src/hooks useNavigatingAway.ts

69.23% Statements 9/13
50% Branches 3/6
75% Functions 3/4
69.23% Lines 9/13

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    9x 110x 80x           80x 80x 80x 80x 80x       110x              
import { useEffect } from "react";
 
export const useNavigatingAway = (stepRef: any) => {
  useEffect(() => {
    Iif (
      process.env.VITE_ENVIRONMENT === "local.development" ||
      process.env.VITE_ENVIRONMENT === "local.staging"
    ) {
      return;
    }
    window.addEventListener("beforeunload", alertUser);
    window.addEventListener("popstate", alertUser);
    return () => {
      window.removeEventListener("beforeunload", alertUser);
      window.removeEventListener("popstate", alertUser);
    };
  }, [stepRef.current]);
 
  const alertUser = (e: any) => {
    if (stepRef.current?.methods?.formState?.isDirty) {
      e.preventDefault();
      e.returnValue = "";
    }
  };
};