mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
95 lines
2.9 KiB
TypeScript
95 lines
2.9 KiB
TypeScript
// @ts-nocheck
|
|
|
|
import React from 'react';
|
|
import { flatten, map } from 'lodash';
|
|
import * as R from 'ramda';
|
|
import { IntersectionObserver } from '@/components';
|
|
import { useAccountUncategorizedTransactionsInfinity } from '@/hooks/query';
|
|
import { useAccountTransactionsContext } from './AccountTransactionsProvider';
|
|
import { withBanking } from '../withBanking';
|
|
|
|
const AccountUncategorizedTransactionsContext = React.createContext();
|
|
|
|
function flattenInfinityPagesData(data) {
|
|
return flatten(map(data.pages, (page) => page.data));
|
|
}
|
|
|
|
/**
|
|
* Account un-categorized transactions provider.
|
|
*/
|
|
function AccountUncategorizedTransactionsBootRoot({
|
|
// #withBanking
|
|
uncategorizedTransactionsFilter,
|
|
|
|
// #ownProps
|
|
children,
|
|
}) {
|
|
const { accountId } = useAccountTransactionsContext();
|
|
|
|
// Fetches the uncategorized transactions.
|
|
const {
|
|
data: uncategorizedTransactionsPage,
|
|
isFetching: isUncategorizedTransactionFetching,
|
|
isLoading: isUncategorizedTransactionsLoading,
|
|
isSuccess: isUncategorizedTransactionsSuccess,
|
|
isFetchingNextPage: isUncategorizedTransactionFetchNextPage,
|
|
fetchNextPage: fetchNextUncategorizedTransactionsPage,
|
|
hasNextPage: hasUncategorizedTransactionsNextPage,
|
|
} = useAccountUncategorizedTransactionsInfinity(accountId, {
|
|
page_size: 50,
|
|
min_date: uncategorizedTransactionsFilter?.fromDate,
|
|
max_date: uncategorizedTransactionsFilter?.toDate,
|
|
});
|
|
// Memorized the cashflow account transactions.
|
|
const uncategorizedTransactions = React.useMemo(
|
|
() =>
|
|
isUncategorizedTransactionsSuccess
|
|
? flattenInfinityPagesData(uncategorizedTransactionsPage)
|
|
: [],
|
|
[uncategorizedTransactionsPage, isUncategorizedTransactionsSuccess],
|
|
);
|
|
// Handle the observer ineraction.
|
|
const handleObserverInteract = React.useCallback(() => {
|
|
if (
|
|
!isUncategorizedTransactionFetching &&
|
|
hasUncategorizedTransactionsNextPage
|
|
) {
|
|
fetchNextUncategorizedTransactionsPage();
|
|
}
|
|
}, [
|
|
isUncategorizedTransactionFetching,
|
|
hasUncategorizedTransactionsNextPage,
|
|
fetchNextUncategorizedTransactionsPage,
|
|
]);
|
|
// Provider payload.
|
|
const provider = {
|
|
uncategorizedTransactions,
|
|
isUncategorizedTransactionFetching,
|
|
isUncategorizedTransactionsLoading,
|
|
};
|
|
|
|
return (
|
|
<AccountUncategorizedTransactionsContext.Provider value={provider}>
|
|
{children}
|
|
<IntersectionObserver
|
|
onIntersect={handleObserverInteract}
|
|
enabled={!isUncategorizedTransactionFetchNextPage}
|
|
/>
|
|
</AccountUncategorizedTransactionsContext.Provider>
|
|
);
|
|
}
|
|
|
|
const AccountUncategorizedTransactionsBoot = R.compose(
|
|
withBanking(({ uncategorizedTransactionsFilter }) => ({
|
|
uncategorizedTransactionsFilter,
|
|
})),
|
|
)(AccountUncategorizedTransactionsBootRoot);
|
|
|
|
const useAccountUncategorizedTransactionsContext = () =>
|
|
React.useContext(AccountUncategorizedTransactionsContext);
|
|
|
|
export {
|
|
AccountUncategorizedTransactionsBoot,
|
|
useAccountUncategorizedTransactionsContext,
|
|
};
|