All files / src/features/GiveNotificationModal/hooks useGetNotifications.tsx

100% Statements 10/10
75% Branches 9/12
100% Functions 3/3
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 45 46 47 48 49 50            3x 38x 38x 38x     38x                             38x   15x   15x           38x                        
import { useMemo } from "react";
import { QKEY_ALL_NOTIFICATIONS } from "@constants/queryKeys";
import useGetInfiniteList from "@hooks/common/useGetInfiniteList";
import { useGetCurrentMerchantId } from "@hooks/common";
import { NotificationType } from "../giveNotificationTypes";
 
const useGetNotifications = (enabled = true) => {
  const { selectedUser } = useGetCurrentMerchantId();
  const merchantId = selectedUser?.id || 0;
  const userId = selectedUser?.userAccID || 0;
 
  const { data, isLoading, fetchNextPage, hasNextPage, isFetchingNextPage } =
    useGetInfiniteList(
      {
        max: 20,
        sort: `alertType,-createdAt`,
      },
      {
        queryKey: QKEY_ALL_NOTIFICATIONS,
        baseURL: `/accounts/${merchantId}/members/${userId}/push-notifications`,
        dependencies: [userId, merchantId],
      },
      {
        enabled: !!userId && !!merchantId && enabled,
      },
    );
 
  const allList = useMemo(() => {
    const list: NotificationType[] =
      data?.pages?.map((row: any) => row.data).flat() ?? [];
    //TODO: group them by date
    return {
      list: list,
      total: list.length,
    };
  }, [data]);
 
  return {
    isLoading,
    hasNextPage,
    fetchNextPage,
    isFetchingNextPage,
    total: allList.total || 0,
    list: allList.list,
    isEmpty: allList.total < 1,
  };
};
 
export default useGetNotifications;