import React, { createContext } from 'react'; import { defaultTo } from 'lodash'; import * as R from 'ramda'; import { useGetBankTransactionsMatches } from '@/hooks/query/bank-rules'; interface MatchingTransactionBootValues { isMatchingTransactionsLoading: boolean; isMatchingTransactionsFetching: boolean; isMatchingTransactionsSuccess: boolean; possibleMatches: Array; perfectMatchesCount: number; perfectMatches: Array; matches: Array; } const RuleFormBootContext = createContext( {} as MatchingTransactionBootValues, ); interface RuleFormBootProps { uncategorizedTransactionId: number; children: React.ReactNode; } function MatchingTransactionBoot({ uncategorizedTransactionId, ...props }: RuleFormBootProps) { const { data: matchingTransactions, isLoading: isMatchingTransactionsLoading, isFetching: isMatchingTransactionsFetching, isSuccess: isMatchingTransactionsSuccess, } = useGetBankTransactionsMatches(uncategorizedTransactionId); const possibleMatches = defaultTo(matchingTransactions?.possibleMatches, []); const perfectMatchesCount = matchingTransactions?.perfectMatches?.length || 0; const perfectMatches = defaultTo(matchingTransactions?.perfectMatches, []); const matches = R.concat(perfectMatches, possibleMatches); const provider = { isMatchingTransactionsLoading, isMatchingTransactionsFetching, isMatchingTransactionsSuccess, possibleMatches, perfectMatchesCount, perfectMatches, matches, } as MatchingTransactionBootValues; return ; } const useMatchingTransactionBoot = () => React.useContext(RuleFormBootContext); export { MatchingTransactionBoot, useMatchingTransactionBoot };