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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | 1x 15x 15x 15x 15x 15x 33x 15x 15x 15x 15x 15x 15x 33x 15x 15x 5x 15x 15x 15x 15x 12x 15x 15x 9x 4x 15x 9x 15x 15x 15x 15x 15x 5x 5x 15x 15x 5x 5x 15x 15x | import { parseSettlmentRow } from "@components/ManageMoney/TransactionTable/transactions.helpers";
import { TSettlement } from "@components/Settlement/data.types";
import { CursorValue, queryFunctionBuilder } from "@components/VirtualList/api";
import { useFiltersRepository } from "@components/VirtualList/hooks";
import { DEFAULT_QUERY_CONFIG } from "@components/VirtualList/hooks/queries";
import { QKEY_SETTLEMENT, QKEY_TRANSACTION_REPORT } from "@constants/queryKeys";
import { useGetCurrentMerchantId } from "@hooks/common";
import { useStateEffect } from "@hooks/customReactCore";
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import { resetFilterByKey } from "@redux/slices/dynamicFilterSlice";
import { sortingKey } from "@redux/slices/fundraisers";
import {
selectSelectedMerchant,
setSelectedDate,
} from "@redux/slices/merchantFilters";
import { useTableFilters } from "@redux/slices/tableFilters";
import { useTable } from "componentsV2/Table/hooks/useTable";
import useTableSearch from "componentsV2/Table/hooks/useTableSearch";
import { TRANSACTION_INFO_MODAL } from "modals/modal_names";
import { useMemo, useRef, useState } from "react";
import { useInfiniteQuery, useQueryClient } from "react-query";
import { useNavigate } from "react-router-dom";
type UseListSettlementsProps = {
type?: "settlements" | "transactions";
settlementId?: string;
};
export const useListSettlements = ({
type = "settlements",
settlementId,
}: UseListSettlementsProps = {}) => {
const navigate = useNavigate();
const { queryString: filtersRepositoryQueryString } = useFiltersRepository();
const { queryStringQuickFilter, resetTableFilters } = useTableFilters();
const { merchantId } = useGetCurrentMerchantId();
const selectedMerchant = useAppSelector((state) =>
selectSelectedMerchant(state, "settlement-merchants"),
);
const latestSettlementDate = useRef<Date | null>(null);
const selectedMerchantID = selectedMerchant?.accID || merchantId;
const dispatch = useAppDispatch();
const queryClient = useQueryClient();
const [page, setPageDispatcher] = useState(1);
const nextCursorValue = useRef<null | CursorValue>(null);
const sorting = useAppSelector((state) => sortingKey(state, QKEY_SETTLEMENT));
const isTransactions = type === "transactions";
//TODO: check after API is ready, it may need to be different
// Combine query strings from filters repository and quick filter (processor dropdown)
const queryString = useMemo(() => {
return [filtersRepositoryQueryString, queryStringQuickFilter]
.filter(Boolean)
.join("%3B");
}, [filtersRepositoryQueryString, queryStringQuickFilter]);
const queryKey = isTransactions
? [QKEY_TRANSACTION_REPORT, settlementId, queryString, sorting]
: [QKEY_SETTLEMENT, selectedMerchantID, queryString, sorting];
const { search, searchQuery, handleSearchChange } = useTableSearch({
queryKey: QKEY_TRANSACTION_REPORT,
});
const path = isTransactions
? `transaction-settlements/${settlementId}/transactions`
: "transaction-settlements";
const {
data,
isFetchingNextPage,
fetchNextPage,
hasNextPage,
isLoading,
isFetching,
error,
} = useInfiniteQuery(
[queryKey, searchQuery, merchantId, sorting],
queryFunctionBuilder({
queryString,
rowsPerPage: 20,
path,
sorting,
merchantId: selectedMerchantID,
searchQuery,
}),
{
...DEFAULT_QUERY_CONFIG,
getNextPageParam: (lastPage: any) => {
return lastPage.nextCursor;
},
enabled: isTransactions ? typeof settlementId === "number" : true,
retry: false,
},
);
const isNotAuthorized = Boolean(
typeof error === "object" &&
error !== null &&
"not_authorized" in error &&
error.not_authorized,
);
// Flatten all pages into one array
const allRows = useMemo<any[]>(
() =>
data?.pages?.[0]?.data
? data.pages.flatMap((page: any) => page.data ?? [])
: [],
[data],
);
const parsedData = useMemo(
() =>
type === "transactions"
? allRows.map((row) => parseSettlmentRow(row))
: allRows,
[type, allRows],
);
const totalRows = allRows.length;
const hasFilters = !!queryString;
const handleOpenSettlement = (row: TSettlement) => {
// timestamp to ISO
const dateISO = new Date(row.settlementDate * 1000).toISOString();
const encodedDate = encodeURIComponent(dateISO);
dispatch(setSelectedDate({ queryKey: "settlement-date", value: dateISO }));
navigate(`/acquirer/settlements/${encodedDate}`, {
state: {
latestSettlementDate: latestSettlementDate.current,
},
});
latestSettlementDate.current = null;
};
// Pagination behavior
const handlePageChange = () => {
if (!isFetchingNextPage && hasNextPage) {
const nextPage = page + 1;
fetchNextPage({ pageParam: nextPage });
setPageDispatcher((prev) => prev + 1);
}
};
const resetPagination = () => {
nextCursorValue.current = null;
return;
};
// Filter reset
const handleResetFilters = () => {
dispatch(resetFilterByKey({ filterKey: QKEY_SETTLEMENT }));
// Also reset the quick filter (processor dropdown)
resetTableFilters(true);
};
// Reset pagination when filters/sorting/search change
useStateEffect(() => {
queryClient.invalidateQueries([QKEY_SETTLEMENT]);
resetPagination();
}, [sorting, queryString]);
const { setSelectedRowIdx, loadMore, numberOfPages, selectedRowIdx } =
useTable({
useIdForRows: true,
allRows,
isLoading: isLoading,
totalRows,
page,
setPageDispatcher,
isFetching,
setPage: handlePageChange,
openModal: isTransactions ? TRANSACTION_INFO_MODAL : "-",
paginationMethod: "cursor",
nextCursor: page + 1,
enabledInfiniteScroll: true,
...(!isTransactions
? {
onOpenRow: handleOpenSettlement,
}
: {}),
});
return {
allRows: parsedData,
setSelectedRowIdx,
loadMore,
numberOfPages,
selectedRowIdx,
handlePageChange,
isFetchingNextPage,
isLoading,
totalRows,
setPageDispatcher,
page,
handleResetFilters,
hasFilters,
search,
searchQuery,
handleSearchChange,
isNotAuthorized,
};
};
function isAuthError(err: unknown): err is { not_authorized: boolean } {
return typeof err === "object" && err !== null && "not_authorized" in err;
}
|