All files / src/components/ProfileMenu/modals/hooks useGetAlerts.ts

100% Statements 12/12
80% Branches 4/5
100% Functions 3/3
100% Lines 12/12

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              8x           167x   167x   167x 167x   167x     29x     29x 29x               167x 11x             167x                
import { QKEY_ALERTS_LIST } from "@constants/queryKeys";
import { useGetCurrentMerchantId } from "@hooks/common";
import { customInstance } from "@services/api";
import { UseQueryOptions, useMutation, useQuery } from "react-query";
import { useModal } from "@ebay/nice-modal-react";
import { groupAlerts } from "../utils/groupAlerts";
 
const useGetAlerts = (
  queryOptions: Omit<
    UseQueryOptions<any, unknown, any, any>,
    "queryKey" | "queryFn"
  >,
) => {
  const { selectedUser, isSponsor } = useGetCurrentMerchantId();
 
  const modal = useModal();
 
  const merchantId = selectedUser?.id;
  const userId = selectedUser?.userAccID;
 
  const { data, isLoading } = useQuery(
    [QKEY_ALERTS_LIST, merchantId, userId, isSponsor],
    async () => {
      const res = await customInstance({
        url: `/accounts/${merchantId}/members/${userId}/alerts`,
      });
      const data = groupAlerts(res?.data || [], { isSponsor });
      return data;
    },
    {
      enabled: !!merchantId && !!userId && modal.visible,
      ...queryOptions,
    },
  );
 
  const alertsMutation = useMutation((data: any) => {
    return customInstance({
      url: `/accounts/${merchantId}/members/${userId}/alerts`,
      method: "PATCH",
      data,
    });
  });
 
  return {
    data,
    isLoading,
    alertsMutation,
  };
};
 
export default useGetAlerts;