All files / src/components/Settings/Permissions/hooks useGetPermissions.ts

93.93% Statements 31/33
76.92% Branches 20/26
100% Functions 6/6
93.75% Lines 30/32

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                5x                   5x       42x         41x 21x     20x 2x           18x           5x       42x   42x 42x     42x 21x     42x     5x             101x 123x     101x   101x                 21x                   101x                           21x   21x   21x       21x 21x   21x       21x               101x                
import { QKEY_LIST_TEAM_MEMBER_PERMISSIONS } from "@constants/queryKeys";
import { useAppSelector } from "@redux/hooks";
import { selectQueryString } from "@redux/slices/search";
import { AxiosError } from "axios";
import { useQuery } from "react-query";
import { PermissionType, TPermissionQueryData } from "../types";
import { customInstance } from "@services/api";
 
const ITEMS_PER_PAGE = 50;
 
interface IUseGetData {
  page?: number;
  memberId?: string;
  accountId?: number;
  type?: PermissionType;
  global?: boolean;
}
 
export const getPermissions = async (
  baseURL: string,
  returnFullData?: boolean,
) => {
  const data: TPermissionQueryData = await customInstance({
    url: encodeURIComponent(baseURL),
    method: "GET",
  });
 
  if (returnFullData) {
    return data;
  }
 
  if (!data?.data || !data?.data?.length) {
    return {
      total: data.total,
      data: [],
    };
  }
 
  return {
    total: Object.keys(data.data).length,
    data: data?.data,
  };
};
 
const buildFilterQuery = ({
  type,
  global,
}: Pick<IUseGetData, "type" | "global">) => {
  const filters = [];
 
  Eif (type) {
    filters.push(`(categoryName:%22${type}%22)`);
  }
 
  if (global) {
    filters.push("(isAttached:true)", "(isGlobal:false)");
  }
 
  return filters.length ? `filter=${filters.join("%3B")}` : "";
};
 
export const useGetPermissions = ({
  page,
  accountId,
  memberId,
  type,
  global,
}: IUseGetData) => {
  const searchQuery = useAppSelector((state) =>
    selectQueryString(state, QKEY_LIST_TEAM_MEMBER_PERMISSIONS),
  );
 
  const baseURL = `/accounts/${accountId}/members/${memberId}/permissions`;
 
  const { data: modifiedData } = useQuery<{ total: number }, AxiosError>(
    [
      QKEY_LIST_TEAM_MEMBER_PERMISSIONS,
      accountId,
      memberId,
      type,
      "modifiedCount",
    ],
    () =>
      getPermissions(
        `${baseURL}?page=1&max=1&${buildFilterQuery({ type, global: true })}`,
        true,
      ),
    {
      retry: 3,
      enabled: !!accountId && !!memberId,
    },
  );
 
  const { data, error, isLoading, isFetching, status } = useQuery<
    TPermissionQueryData,
    AxiosError
  >(
    [
      QKEY_LIST_TEAM_MEMBER_PERMISSIONS,
      accountId,
      memberId,
      page,
      searchQuery,
      type,
      global,
    ],
    async () => {
      let url = baseURL;
 
      Eif (page || searchQuery || type || global) url += "?";
 
      Iif (page) {
        url += `&page=${page}&max=${ITEMS_PER_PAGE}`;
      }
 
      const filterQuery = buildFilterQuery({ type, global });
      url += filterQuery;
 
      Iif (searchQuery) {
        url += `&q='${searchQuery}'`;
      }
 
      return getPermissions(url);
    },
    {
      retry: false,
      enabled: !!accountId && !!memberId,
    },
  );
 
  return {
    data: { ...data, modifiedCount: modifiedData?.total },
    error,
    isLoading,
    isFetching,
    status,
  };
};