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 | 28x 6x 1004x 1004x 1004x 1004x 1004x 1004x 1004x 1004x 1004x 243x 243x 243x 243x 20x 20x 20x 20x 20x 1004x 223x 200x 1004x 98x 98x 7x 1004x | import { useCallback, useEffect, useRef } from "react";
import NiceModal, { useModal } from "@ebay/nice-modal-react";
import useIterator from "./useIterator";
import { TableProps } from "../Table.types";
import { useAppSelector } from "@redux/hooks";
import { selectMerchantTab } from "@redux/slices/enterprise/merchants";
import { TabNames } from "features/Merchants/MerchantsTable/hooks/useTabs";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { useRowsPerPage } from "./useRowsPerPage";
export const useTable = ({
openModal,
allRows,
isLoading,
totalRows,
page,
setPageDispatcher,
isFetching,
setPage,
useIdForRows,
canOpenModal = () => true,
paginationMethod = "offset",
nextCursor,
enabledInfiniteScroll,
storageKey,
onOpenRow,
}: Omit<
TableProps,
"columns" | "handlePageChange" | "type" | "queryKey" | "rowsPerPage"
>) => {
const { isNewApprovalFlowEnabled } = useGetFeatureFlagValues();
const { rowsPerPage } = useRowsPerPage(storageKey || "");
const numberOfPages = Math.ceil(Number(totalRows ?? 0) / rowsPerPage);
const reOpening = useRef(false);
const setReopening = (val: boolean) => {
reOpening.current = val;
};
const { onIterator, selectedRowIdx, setSelectedRowIdx } = useIterator({
dataLen: allRows.length,
rowsPerPage,
});
const tabName = useAppSelector(selectMerchantTab);
const modal = useModal(openModal);
useEffect(() => {
Iif ((isLoading || isFetching) && modal.visible) {
return;
}
const selectedRowData = allRows[selectedRowIdx];
const id = useIdForRows ? selectedRowData?.id : selectedRowData?.accID;
if (id && canOpenModal(selectedRowData)) {
const isFirstItem =
paginationMethod === "offset" ? page === 1 : allRows.length > 1;
const isLastItem =
paginationMethod === "offset"
? page === numberOfPages
: !!allRows.length && !nextCursor;
Iif (onOpenRow) {
onOpenRow(selectedRowData);
return;
}
Iif (
allRows[selectedRowIdx + 1] === undefined &&
!isLastItem &&
enabledInfiniteScroll
) {
loadMore();
}
NiceModal.show(openModal, {
id,
setReopening,
tableRowData: allRows[selectedRowIdx],
//TODO: refactor this after rebranding is done; data field is sent because filter modal expects data field
data: allRows[selectedRowIdx],
setSelectedRow: onIterator(
setPageDispatcher,
page,
!enabledInfiniteScroll,
),
isFirst: allRows[selectedRowIdx - 1] === undefined && isFirstItem,
isLast: allRows[selectedRowIdx + 1] === undefined && isLastItem,
...(tabName === TabNames.risk &&
!isNewApprovalFlowEnabled && {
initialSection: "riskProfile",
}),
rowData: allRows[selectedRowIdx],
});
}
}, [selectedRowIdx, isFetching]);
useEffect(() => {
if (!modal.visible && !reOpening.current) {
setSelectedRowIdx(-1);
}
}, [modal.visible]);
const loadMore = useCallback(() => {
const canLoad =
paginationMethod === "offset" ? page < numberOfPages : !!nextCursor;
if (!isLoading && !isFetching && canLoad) {
setPage();
}
}, [
isLoading,
isFetching,
page,
numberOfPages,
nextCursor,
paginationMethod,
setPage,
]);
return {
setSelectedRowIdx,
loadMore,
numberOfPages,
selectedRowIdx,
};
};
|