All files / src/features/ChangeLog/hooks useListCategories.tsx

100% Statements 13/13
100% Branches 2/2
100% Functions 7/7
100% Lines 13/13

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                                    117x 24x   24x 4x           24x     4x               24x 23x 114x             24x   23x 114x         24x                  
import { customInstance } from "@services/api";
import { useQuery } from "react-query";
import { QKEY_GET_CHANGELOG_CATEGORIES } from "@constants/queryKeys";
import { useMemo, useState } from "react";
import { GENERAL_STALE_TIME_15 } from "@features/Merchants/MerchantSidePanel/constants";
 
type CategoryType = {
  id: number;
  name: string;
  DisplayName: string;
  accID: number;
};
 
type QueryReturnType = {
  total: number;
  data: CategoryType[] | null;
};
 
const useListCategories = ({ merchantId }: { merchantId: number }) => {
  const [searchValue, setSearchValue] = useState("");
 
  const getChangelogCategories = () => {
    return customInstance({
      url: `/merchants/${merchantId}/changelogs/categories`,
      method: "GET",
    });
  };
 
  const { data, isLoading } = useQuery<QueryReturnType>(
    [QKEY_GET_CHANGELOG_CATEGORIES],
    async () => {
      return await getChangelogCategories();
    },
    {
      enabled: !!merchantId,
      refetchOnWindowFocus: false,
      staleTime: GENERAL_STALE_TIME_15,
    },
  );
  const formattedData = useMemo(() => {
    return (
      data?.data?.map((category: CategoryType) => ({
        label: category.DisplayName,
        value: category.DisplayName,
        id: category.id,
      })) ?? []
    );
  }, [data]);
  const filteredData = useMemo(
    () =>
      formattedData.filter((item) =>
        item?.label?.toLowerCase().includes(searchValue.toLowerCase()),
      ),
    [searchValue, formattedData],
  );
 
  return {
    data: filteredData,
    isLoading,
    handleSearch: setSearchValue,
    searchValue,
  };
};
 
export default useListCategories;