All files / src/componentsV2/SideMenu/components/hooks useNotifications.tsx

100% Statements 9/9
100% Branches 12/12
100% Functions 2/2
100% Lines 9/9

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            14x 45x 45x 45x   45x     10x       10x           10x                       45x              
import { QKEY_CHECK_NEW_NOTIFICATIONS } from "@constants/queryKeys";
import { GENERAL_STALE_TIME } from "@features/Merchants/MerchantSidePanel/constants";
import { useGetCurrentMerchantId } from "@hooks/common";
import { getHasNewNotifications } from "@services/api/notifications/user";
import { useQuery } from "react-query";
 
const useNotifications = () => {
  const { selectedUser } = useGetCurrentMerchantId();
  const merchantId = selectedUser?.id || 0;
  const userId = selectedUser?.userAccID || 0;
 
  const { data } = useQuery(
    [QKEY_CHECK_NEW_NOTIFICATIONS, merchantId, userId],
    async () => {
      const unreadNotifications = await getHasNewNotifications(
        merchantId,
        userId,
      );
      const mandatoryNotifications = await getHasNewNotifications(
        merchantId,
        userId,
        true,
      );
 
      return {
        unreadNotifications: unreadNotifications?.total || 0,
        hasMandatoryAlerts: mandatoryNotifications?.data?.length > 0,
      };
    },
    {
      enabled: !!userId && !!merchantId,
      refetchOnWindowFocus: false,
      refetchInterval: GENERAL_STALE_TIME,
    },
  );
 
  return {
    unreadNotifications: data?.unreadNotifications || 0,
    hasMandatoryAlerts: data?.hasMandatoryAlerts ?? false,
  };
};
 
export default useNotifications;