All files / src/features/GiveNotificationModal/provider GiveNotificationProvider.tsx

82.5% Statements 33/40
70% Branches 14/20
62.5% Functions 5/8
87.09% Lines 27/31

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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180                                      3x                     38x     38x 38x           38x       38x 6x   6x 6x     38x               38x                                             38x                       38x               38x     38x   38x                 38x   38x 38x 36x 34x     38x 38x 36x 34x     38x                                                                                               191x  
import React, { createContext, useContext, useState } from "react";
import {
  GiveNotificationContextType,
  GiveNotificationTypes,
  NotificationTabs,
  NotificationThread,
  AccountOption,
} from "../giveNotificationTypes";
import { DEFAULT_ACCOUNT_OPTION } from "../utils/accountUtils";
import useCheckPAH from "@hooks/common/useCheckPAH";
import {
  useGetConversationStats,
  useGetConversationThreads,
  useGetMessageConversationChat,
} from "@features/GiveConversation/hooks/useManageApi";
import { useGetCurrentMerchantId } from "@hooks/common";
import useGetNotifications from "../hooks/useGetNotifications";
import useNotifications from "componentsV2/SideMenu/components/hooks/useNotifications";
 
const NotificationContext = createContext<GiveNotificationContextType>(
  {} as any,
);
 
function GiveNotificationProvider({
  children,
  props: { initialTab, initialThreadId },
}: {
  children: React.ReactElement;
  props: GiveNotificationTypes;
}) {
  const [tab, setTab] = useState<NotificationTabs>(
    initialTab ?? "notifications",
  );
  const { merchantId } = useGetCurrentMerchantId();
  const [currentThread, setCurrentThread] = useState<
    NotificationThread | undefined
  >({
    id: initialThreadId,
  });
 
  const [teamAccountType, setTeamAccountType] = useState<AccountOption>(
    DEFAULT_ACCOUNT_OPTION,
  );
 
  const handleSwitchTabs = (newState: NotificationTabs) => {
    if (currentThread) setCurrentThread(undefined);
    // Reset team account type selection when switching tabs
    setTeamAccountType(DEFAULT_ACCOUNT_OPTION);
    setTab(newState);
  };
 
  const { isLoggedInPAH } = useCheckPAH();
  const {
    messages,
    isLoadingGetMessages: isThreadMessagesLoading,
    fetchNextPageMessages,
    hasNextPageMessages,
    isFetchingNextPageMessages,
    isBackgroundFetchingMessages,
  } = useGetMessageConversationChat({
    id: currentThread?.id,
    merchantId,
    nthFromLast: currentThread?.count,
    onSuccess: (data) => {
      if (currentThread?.name && currentThread?.id) return;
      const list = data?.pages.flatMap((page: any) => page?.data) ?? [];
      const item = list?.[0];
 
      setCurrentThread((prev) => ({
        ...prev,
        name: item?.body,
        count: list?.length,
      }));
    },
  });
 
  const {
    threads,
    isLoading: isThreadListLoading,
    fetchNextPageThreadList,
    hasNextPageThreadList,
    isFetchingNextPageThreadList,
  } = useGetConversationThreads({
    merchantId,
    enabled: tab === "messages",
    selectedTab: "merchant",
  });
 
  const {
    threads: teamThreads,
    isLoading: isTeamThreadListLoading,
    fetchNextPageThreadList: fetchNextPageTeamThreadList,
    hasNextPageThreadList: hasNextPageTeamThreadList,
    isFetchingNextPageThreadList: isFetchingNextPageTeamThreadList,
  } = useGetConversationThreads({
    merchantId,
    enabled: tab === "team",
    selectedTab: "team",
    accountType: teamAccountType.type,
  });
 
  const { data: messageStats, isLoading: isStatsLoading } =
    useGetConversationStats({
      merchantID: merchantId,
    });
  const { unreadNotifications, hasMandatoryAlerts } = useNotifications();
  const { totalUnreadMessagesExternal: unreadMessagesCount } =
    messageStats || {};
 
  const {
    list,
    total,
    isLoading: isLoadingNotifications,
    fetchNextPage: fetchNotificationsNextPage,
    hasNextPage: hasNotificationsNextPage,
    isFetchingNextPage: isFetchingNotificationsNextPage,
  } = useGetNotifications(tab === "notifications");
 
  const getIsLoadingState = () => {
    if (tab === "messages") return isThreadListLoading;
    if (tab === "team") return isTeamThreadListLoading;
    return isLoadingNotifications;
  };
 
  const getEmptyState = () => {
    if (tab === "messages") return threads?.length < 1;
    if (tab === "team") return teamThreads?.length < 1;
    return total < 1;
  };
 
  return (
    <NotificationContext.Provider
      value={{
        isLoading: getIsLoadingState(),
        isEmptyData: getEmptyState(),
        tab,
        handleSwitchTabs,
        isPAHloggedIn: isLoggedInPAH,
        threadList: threads,
        teamThreadList: teamThreads,
        notificationList: list,
        isBackgroundFetchingMessages,
        currentThread,
        setCurrentThread,
        messages,
        fetchNextPageMessages,
        isFetchingNextPageMessages,
        hasNextPageMessages,
        isThreadMessagesLoading,
        merchantId,
        isLoadingNotifications,
        fetchNotificationsNextPage,
        hasNotificationsNextPage,
        isFetchingNotificationsNextPage,
        fetchNextPageThreadList,
        hasNextPageThreadList,
        isFetchingNextPageThreadList,
        // team messages
        isTeamThreadListLoading,
        fetchNextPageTeamThreadList,
        hasNextPageTeamThreadList,
        isFetchingNextPageTeamThreadList,
        // Stats
        unreadMessagesCount,
        unreadNotifications,
        hasMandatoryAlerts,
        // Team account type selection state
        teamAccountType,
        setTeamAccountType,
      }}
    >
      {children}
    </NotificationContext.Provider>
  );
}
 
export default GiveNotificationProvider;
 
export const useGiveNotificationContext = () => useContext(NotificationContext);