All files / src/services/api/products events.ts

43.47% Statements 10/23
0% Branches 0/4
0% Functions 0/10
43.47% Lines 10/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                  1x               1x                                 1x 1x   1x     1x     1x             1x             1x             1x                    
import { Axios, AxiosInstance, AxiosRequestConfig } from "axios";
import { axiosInstance } from "../index";
import { customInstance } from "../index";
import { UseQueryOptions, useQuery } from "react-query";
import { ROWS_PER_PAGE } from "@hooks/common/usePagination";
import { ProductParams, useQueryFactory, useQueryFactoryInfinite } from "./queryFactory";
import { buildMerchantEndpoints } from "../utils.api";
import { TCampaignType } from "features/Products/types";
 
export const addEvent = (query: any) => {
  return customInstance({
    url: "merchants/2/customers",
    method: "POST",
    data: query,
  });
};
 
export const getEvents = ({
  queryString,
  page,
  sorting,
  searchQuery,
}: ProductParams) => {
  const search = searchQuery ? `&q="${searchQuery}"` : "";
  return customInstance({
    url: buildMerchantEndpoints(
      `products?filter=typeName:"event"${queryString}&sort=${
        sorting ?? "-id"
      }${search}&page=${page}&max=${ROWS_PER_PAGE}`,
    ),
    method: "GET",
  });
};
 
export const useGetEvents = useQueryFactory("event");
export const useGetProducts = useQueryFactory("standard");
 
export const useGetProductsByType = (type: TCampaignType) =>
  useQueryFactory(type);
 
export const useGetInfiniteProductsByType = (type: TCampaignType) =>
  useQueryFactoryInfinite(type);
 
export const getProductById = (id: string) => {
  return customInstance({
    url: buildMerchantEndpoints(`products/${id}?filter=typeName:"event"`),
    method: "GET",
  });
};
 
export const getProductVariants = (id: string) => {
  return customInstance({
    url: buildMerchantEndpoints(`products/${id}/variants`),
    method: "GET",
  });
};
 
export const useGetEventVariants = (id: string) => {
  return useQuery("event-variants", async () => {
    const data = await getProductVariants(id);
    return data;
  });
};
 
export const useGetProductById = (id: string) => {
  return useQuery(
    ["get-event-by-id", id],
    async () => {
      const data = await getProductById(id);
      return data;
    },
    { cacheTime: 0, refetchOnWindowFocus: false },
  );
};