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 | 1x | import React, { useEffect, useMemo, useRef } from "react";
import { useGetCustomers } from "@services/api/customer";
// utils
import { detectMobile } from "@utils/index";
// redux
import { useAppSelector } from "@redux/hooks";
import { ROWS_PER_PAGE, usePagination } from "@hooks/common/usePagination";
import { useCachedList } from "@hooks/common/useCachedList";
import { selectQueryFilters } from "@redux/slices/customers";
import { encodedQueryFilterMap } from "@services/filtering";
import { sorting as sortingReducer } from "@redux/slices/fundraisers";
import { QKEY_LIST_CUSTOMERS } from "@constants/queryKeys";
import { selectQueryString } from "@redux/slices/search";
const useListCustomers = () => {
const queryFilters = useAppSelector(selectQueryFilters);
const sorting = useAppSelector(sortingReducer);
const searchQuery = useAppSelector((state) =>
selectQueryString(state, QKEY_LIST_CUSTOMERS),
);
const loadingRef = useRef<boolean>(false);
const { page, setPage } = usePagination(0, "");
const { allData, invalidateCache } = useCachedList(
QKEY_LIST_CUSTOMERS,
false,
page,
);
const queryFilter = useMemo(
() => encodedQueryFilterMap(queryFilters),
[queryFilters],
);
const queryString = queryFilter.customers || "";
const { data, isLoading, isError, isFetching } = useGetCustomers(
{
page,
queryString,
searchQuery,
sorting,
},
{
refetchOnWindowFocus: false,
onSuccess(_data) {
setTimeout(() => {
loadingRef.current = false;
}, 700);
},
},
);
useEffect(() => {
if (detectMobile()) invalidateCache();
setPage(1);
}, [sorting, searchQuery, queryString]);
const usedData = detectMobile() ? allData : data?.data ?? [];
const formatedRows = useMemo(
() => (usedData.length ? usedData : []),
[usedData],
);
const handlePageChange = (
event: React.ChangeEvent<unknown>,
value: number,
) => {
setPage(value);
};
return {
isError,
page,
rowsPerPage: ROWS_PER_PAGE,
currentPageRows: formatedRows,
handlePageChange,
totalRows: data?.total ?? 0,
setPage: (_e: any, v: number) => {
detectMobile() ? setPage((current) => current + 1) : setPage(Number(v));
},
allRows: formatedRows,
loadingRef,
isLoading,
isFetching,
state: {
isEmpty: !isLoading && !queryString && !searchQuery && data?.total === 0,
isError,
},
};
};
export default useListCustomers;
|