All files / src/features/Notifications/NotificationsCenter/hooks useGetNoticationsList.ts

100% Statements 22/22
100% Branches 24/24
100% Functions 6/6
100% Lines 20/20

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                                              25x 403x 403x 403x   403x               403x                                               403x                             403x   111x 111x 111x 395x   111x             403x   403x   110x 110x 110x           403x                                               403x            
import { TNotificationsFilter } from "../types";
import { useMemo } from "react";
import { TGroupedList, groupData } from "../utils";
import {
  QKEY_UNREAD_NOTIFICATIONS,
  QKEY_ALL_NOTIFICATIONS,
} from "@constants/queryKeys";
import useGetInfiniteList from "@hooks/common/useGetInfiniteList";
import { useUpdateSponsorTable } from "./useUpdateSponsorTable";
import { useGetCurrentMerchantId } from "@hooks/common";
 
type ListState = {
  isLoading: boolean;
  isEmpty: boolean;
  total: number;
  data: TGroupedList;
  hasUnreadMandatory?: boolean;
  hasNextPage?: boolean;
  fetchNextPage: any;
  isFetchingNextPage: boolean;
  totalPages: number;
};
 
const useGetNotificationsList = (disabled: boolean) => {
  const { selectedUser } = useGetCurrentMerchantId();
  const merchantId = selectedUser?.id || 0;
  const userId = selectedUser?.userAccID || 0;
 
  const baseURL = `/accounts/${merchantId}/members/${userId}/push-notifications`;
 
  const {
    data: unreadNotifications,
    isLoading: isLoadingUnread,
    fetchNextPage: fetchNextPageUnread,
    hasNextPage: hasNextPageUnread,
    isFetchingNextPage: isFetchingNextPageUnread,
  } = useGetInfiniteList(
    {
      max: 20,
      sort: `alertType,-createdAt`,
      filter: {
        readAt: null,
      },
    },
    {
      queryKey: QKEY_UNREAD_NOTIFICATIONS,
      baseURL,
      dependencies: [userId, merchantId],
    },
    {
      enabled: !!userId && !!merchantId && !disabled,
    },
  );
 
  const {
    data: allNotifications,
    isLoading: isLoadingAll,
    fetchNextPage: fetchNextPageAll,
    hasNextPage: hasNextPageAll,
    isFetchingNextPage: isFetchingNextPageAll,
  } = useGetInfiniteList(
    {
      max: 20,
      sort: `alertType,-createdAt`,
    },
    {
      queryKey: QKEY_ALL_NOTIFICATIONS,
      baseURL,
      dependencies: [userId, merchantId],
    },
    {
      enabled: !!userId && !!merchantId && !disabled,
    },
  );
 
  const unreadList = useMemo(() => {
    const list =
      unreadNotifications?.pages?.map((row: any) => row.data).flat() ?? [];
    const groupedData = groupData(list);
    const unreadMandatoryList = groupedData.mandatory.filter(
      (a) => !a.firstCheckedAt,
    );
    return {
      list: groupedData,
      total: unreadNotifications?.pages[0]?.total ?? 0,
      hasUnreadMandatory: unreadMandatoryList.length > 0,
    };
  }, [unreadNotifications]);
 
  useUpdateSponsorTable({ unreadNotifications });
 
  const allList = useMemo(() => {
    const list =
      allNotifications?.pages?.map((row: any) => row.data).flat() ?? [];
    const groupedList = groupData(list);
    return {
      list: groupedList,
      total: groupedList.mandatory.length + groupedList.generic.length,
    };
  }, [allNotifications]);
 
  const listState: Record<TNotificationsFilter, ListState> = {
    unread: {
      isLoading: isLoadingUnread,
      isEmpty: unreadList.total === 0,
      total: unreadList.total || 0,
      data: unreadList.list,
      hasUnreadMandatory: unreadList.hasUnreadMandatory,
      hasNextPage: hasNextPageUnread,
      fetchNextPage: fetchNextPageUnread,
      isFetchingNextPage: isFetchingNextPageUnread,
      totalPages: unreadNotifications?.pages?.length || 0,
    },
    all: {
      isLoading: isLoadingAll,
      isEmpty: allList.total === 0,
      total: allList.total || 0,
      data: allList.list,
      hasNextPage: hasNextPageAll,
      fetchNextPage: fetchNextPageAll,
      isFetchingNextPage: isFetchingNextPageAll,
      totalPages: allNotifications?.pages?.length || 0,
    },
  };
 
  return {
    listState,
  };
};
 
export default useGetNotificationsList;