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 | 741x 741x 741x 50x 741x 741x 88x 741x 98x 98x 98x 98x 98x 33x 98x 741x 3x 741x 67x | // /merchants/3/category-codes?q=%22amuse%22
import { useInfiniteQuery, useQuery } from "react-query";
import { useMemo, useRef } from "react";
import { queryBuilder } from "@components/VirtualList/api";
import { TCategory } from "@common/BusinessProfileInputs/types";
import { customInstance } from "@services/api";
import { GENERAL_STALE_TIME_15 } from "@features/Merchants/MerchantSidePanel/constants";
import { ProcessorValue } from "@features/Merchants/MerchantSidePanel/types";
interface Props {
id?: number;
searchKey?: string;
sorting?: string;
localValue?: number;
processor?: ProcessorValue | "";
defaultCode?: string;
}
export default function useGetEnterpriseCategoryCodes({
id,
searchKey,
sorting,
localValue,
processor,
defaultCode,
}: Props) {
const page = useRef(1);
const searchQuery = searchKey ? `"${searchKey}"` : "";
const { data: singleList, isLoading: isFetchingSingle } = useQuery(
["get-category-Code-by-id", localValue],
async () => {
return customInstance({
url: `/merchants/${id}/category-codes?filter=categoryCodes.id:${localValue}`,
method: "GET",
});
},
{
enabled: !!localValue,
refetchOnWindowFocus: false,
refetchOnMount: false,
staleTime: GENERAL_STALE_TIME_15,
},
);
const { data: singleCodeList, isLoading: isFetchingSingleCodeList } =
useQuery(
["get-category-Code-by-id", defaultCode],
async () => {
return customInstance({
url: `/merchants/${id}/category-codes?filter=categoryCodes.code:${defaultCode}`,
method: "GET",
});
},
{
enabled: !!defaultCode,
refetchOnWindowFocus: false,
refetchOnMount: false,
staleTime: GENERAL_STALE_TIME_15,
},
);
const {
data,
isFetchingNextPage,
fetchNextPage,
hasNextPage,
isLoading,
refetch,
} = useInfiniteQuery(
["category-codes", id, searchKey, processor],
queryBuilder({
searchQuery: searchQuery,
rowsPerPage: 20,
queryString: `${
processor
? `processor=${processorFilterMap[processor]}&filter=` //processor is a query param and shoul not be included in filter, and should use =
: "filter="
}isAllowed:true`,
path: "category-codes",
merchantId: id,
isQueryParam: true,
...(sorting && { sorting }),
}),
{
enabled: Boolean(id),
refetchOnWindowFocus: false,
refetchOnMount: false,
staleTime: GENERAL_STALE_TIME_15,
getNextPageParam: (lastPage: any) => {
return lastPage.nextCursor;
},
},
);
const uiData: TCategory[] = useMemo(() => {
const allList = data?.pages?.flatMap((row) => row?.data ?? []) ?? [];
const singleListArray = singleList?.data ?? [];
const singleCodeListArray = singleCodeList?.data ?? [];
const combinedList = [
...allList,
...singleListArray,
...singleCodeListArray,
];
// Use a Map to ensure uniqueness based on categoryCodes.id
const uniqueMap = new Map(
combinedList.map((item) => [item.categoryCodes.id, item]),
);
return Array.from(uniqueMap.values());
}, [data, singleList]);
const onEndReached = () => {
Iif (!isFetchingNextPage && hasNextPage) {
fetchNextPage({ pageParam: ++page.current });
}
};
return {
data: uiData,
total: data?.pages[0]?.total,
isFetchingNextPage,
fetchNextPage,
hasNextPage,
isLoading: isLoading || isFetchingSingle || isFetchingSingleCodeList,
refetch,
onEndReached,
};
}
const processorFilterMap = {
worldpay: "Worldpay",
rs2_ches: "RS2",
rs2_test: "RS2",
};
|