All files / src/components/Analytics/SidePanels/CampaignSidePanel/hooks useAnalyticsData.ts

0% Statements 0/23
0% Branches 0/12
0% Functions 0/4
0% Lines 0/23

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                                                                                                                                                                                                               
import { useEffect } from "react";
import useAnalyticsDataParser from "../helpers/parsers";
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import { applyDateFilter, selectFilterQuery } from "@redux/slices/analytics";
import { QKEY_ANALYTICS_CAMPAIGNS } from "@constants/queryKeys";
import { useLocation } from "react-router-dom";
import { useQuery } from "react-query";
import {
  getTransactionChart,
  getTransactionStats,
} from "@services/api/analytics/transactions";
import { getCustomersAnalytics } from "@services/api/analytics/customers";
import { useModal } from "@ebay/nice-modal-react";
import { useGetCurrentMerchantId } from "@hooks/common";
import { useGetSettledPayments } from "./useGetSettledPayments";
import { useGetRecurringCustomers } from "./useGetRecurringCustomers";
import { GENERAL_STALE_TIME_15 } from "@features/Merchants/MerchantSidePanel/constants";
 
const useAnalyticsData = (
  isEmpty: boolean,
  payformAnalytics?: any,
  isExpanded?: boolean,
) => {
  const dispatch = useAppDispatch();
  const modal = useModal();
  const { merchantId } = useGetCurrentMerchantId();
  const filterQuery = useAppSelector((state) =>
    selectFilterQuery(state, QKEY_ANALYTICS_CAMPAIGNS),
  );
  const location = useLocation();
  const section = location.pathname.replace("/merchant/", "");
 
  const { data, isLoading, isFetching } = useQuery(
    [QKEY_ANALYTICS_CAMPAIGNS, merchantId, filterQuery],
    async () => {
      const filter = filterQuery.dateFilter;
      const data = await getTransactionStats({ filter });
      const mainChart = await getTransactionChart({
        filter,
        productType: section,
      });
 
      const chart = mainChart?.data;
 
      const customers = await getCustomersAnalytics({
        filter,
      });
 
      return {
        data: data,
        customers,
        chart: {
          mainChart: chart || [],
        },
      };
    },
    {
      refetchOnWindowFocus: false,
      enabled: !!modal.visible && !isEmpty,
      retry: 2,
    },
  );
 
  const isEnabledPaymentsCustomers = modal.visible && !!isExpanded;
 
  const { data: postedPayments, isLoading: paymentsLoading } =
    useGetSettledPayments(isEnabledPaymentsCustomers);
 
  const { data: recurringCustomers, isLoading: recurringCustomersLoading } =
    useGetRecurringCustomers(isEnabledPaymentsCustomers);
 
  useEffect(() => {
    if (!modal.visible) {
      dispatch(
        applyDateFilter({
          queryKey: QKEY_ANALYTICS_CAMPAIGNS,
          filter: { type: undefined, amount: 0 },
        }),
      );
    }
  }, [modal.visible]);
 
  const customData = useAnalyticsDataParser(
    {
      ...data,
      recurringCustomers: recurringCustomers?.customers,
      chart: {
        ...data?.chart,
        recurrenceChart: recurringCustomers?.chart,
        ...postedPayments,
      },
    },
    payformAnalytics,
  );
 
  return {
    data: customData,
    isLoading:
      isLoading || isFetching || paymentsLoading || recurringCustomersLoading,
  };
};
 
export default useAnalyticsData;