All files / src/pages/MerchantCheckout/hooks useGetCheckoutInfo.tsx

94.11% Statements 16/17
100% Branches 6/6
100% Functions 3/3
94.11% Lines 16/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            1x 231x 231x 231x 231x   231x 21x 4x     17x 17x 17x 17x           231x 21x 4x       231x        
import { useEffect, useMemo } from "react";
import { TypeCheckoutInfo } from "@pages/MerchantCheckout/types";
import { generateCheckoutInfo } from "@pages/MerchantCheckout/utils";
import { useLocation, useNavigate } from "react-router-dom";
import { safeParse } from "@utils/index";
 
const useGetCheckoutInfo = () => {
  const { search } = useLocation();
  const urlSearchParams = new URLSearchParams(search);
  const params = urlSearchParams.get("params") as string;
  const navigate = useNavigate();
 
  const checkoutInfo = useMemo(() => {
    if (!params) {
      return {} as TypeCheckoutInfo;
    }
 
    try {
      const jsonString = atob(params);
      const jsonObject = safeParse(jsonString);
      return generateCheckoutInfo(jsonObject);
    } catch (e) {
      return {} as TypeCheckoutInfo;
    }
  }, [params]);
 
  useEffect(() => {
    if (!params || !Object.keys(checkoutInfo).length) {
      navigate("/");
    }
  }, [checkoutInfo]);
 
  return checkoutInfo;
};
 
export default useGetCheckoutInfo;