All files / src/features/Notifications/AppBar useCheckNewNotifications.tsx

100% Statements 10/10
80% Branches 8/10
100% Functions 2/2
100% Lines 10/10

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              24x 151x 151x 151x 151x   151x     49x 48x           48x                     151x              
import { QKEY_CHECK_NEW_NOTIFICATIONS } from "@constants/queryKeys";
import { useAppSelector } from "@redux/hooks";
import { selectSelectedAccount } from "@redux/slices/auth/accounts";
import { selectUser } from "@redux/slices/auth/auth";
import { getHasNewNotifications } from "@services/api/notifications/user";
import { useQuery } from "react-query";
 
const useCheckNewNotifications = () => {
  const selectedUser = useAppSelector(selectSelectedAccount);
  const user = useAppSelector(selectUser);
  const merchantId = selectedUser?.id || 0;
  const userId = user?.userAccID || 0;
 
  const { data } = useQuery(
    [QKEY_CHECK_NEW_NOTIFICATIONS, merchantId, userId],
    async () => {
      const allNotifications = await getHasNewNotifications(merchantId, userId);
      const mandatoryNotifications = await getHasNewNotifications(
        merchantId,
        userId,
        true,
      );
 
      return {
        hasNewAlerts: allNotifications?.data?.length > 0,
        hasMandatoryAlerts: mandatoryNotifications?.data?.length > 0,
      };
    },
    {
      enabled: !!userId && !!merchantId,
      refetchOnWindowFocus: true,
    },
  );
 
  return {
    hasMandatoryAlerts: data?.hasMandatoryAlerts ?? false,
    hasNewAlerts: data?.hasNewAlerts ?? false,
  };
};
 
export default useCheckNewNotifications;