All files / src/features/Permissions/Modal/api index.ts

81.25% Statements 13/16
76.47% Branches 13/17
100% Functions 4/4
86.66% Lines 13/15

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                    9x   9x 430x 2x   430x                           9x                   306x           29x   29x   29x     29x       29x                       306x    
import { UseQueryOptions, useMutation, useQuery } from "react-query";
import { TPermissionQueryReturnedData } from "./types";
import { AxiosError } from "axios";
import { QKEY_LIST_TEAM_MEMBER_PERMISSIONS } from "@constants/queryKeys";
import { updateUserPermissions } from "@services/api/accessControl/accessControl";
import {
  getAssignedPermissions,
  getGlobalPermissions,
} from "./getPermissionData";
 
export const ITEMS_PER_PAGE = 15;
 
export const useUpdatePermissions = (accountId: number, memberId: number) => {
  const request = async (data: any) => {
    return updateUserPermissions(accountId, memberId, data);
  };
  return useMutation(request);
};
 
interface IUseGetData {
  page: number;
  memberId: number;
  accountId: number;
  searchQuery?: string;
  withPagination?: boolean;
  assignedOnly?: boolean;
  queryKey?: string;
  options?: Omit<UseQueryOptions<any, any, any, any>, "queryKey" | "queryFn">;
}
 
export const useGetData = ({
  page,
  accountId,
  memberId,
  searchQuery,
  options,
  withPagination = true,
  assignedOnly = false,
  queryKey = QKEY_LIST_TEAM_MEMBER_PERMISSIONS,
}: IUseGetData) => {
  const { data, error, isLoading, isFetching, status } = useQuery<
    TPermissionQueryReturnedData,
    AxiosError
  >(
    [queryKey, accountId, memberId, page, searchQuery],
    async () => {
      let baseURL = `/accounts/${accountId}/members/${memberId}/permissions`;
 
      Iif (withPagination || searchQuery) baseURL += "?";
 
      Iif (withPagination) {
        baseURL += `&page=${page}&max=${ITEMS_PER_PAGE}`;
      }
      Iif (searchQuery) {
        baseURL += `&q='${searchQuery}'`;
      }
 
      return assignedOnly
        ? getAssignedPermissions(baseURL, searchQuery || "")
        : getGlobalPermissions(baseURL, searchQuery || "");
    },
    {
      refetchOnWindowFocus: false,
      retry: false,
      staleTime: Infinity,
      ...options,
    },
  );
 
  return { data, error, isLoading, isFetching, status };
};