From 532062d3905d0b9aa70c38310ec39981fcfd2f9f Mon Sep 17 00:00:00 2001 From: sophian Date: Thu, 28 Sep 2023 19:39:02 -0400 Subject: [PATCH] Add count and txTypes to cent-js --- .../src/components/Portfolio/Transactions.tsx | 13 +++++------ centrifuge-app/src/utils/usePools.ts | 6 ++--- centrifuge-js/src/modules/pools.ts | 22 +++++++++---------- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/centrifuge-app/src/components/Portfolio/Transactions.tsx b/centrifuge-app/src/components/Portfolio/Transactions.tsx index 27e436520a..de6095f98b 100644 --- a/centrifuge-app/src/components/Portfolio/Transactions.tsx +++ b/centrifuge-app/src/components/Portfolio/Transactions.tsx @@ -38,15 +38,14 @@ type AddressTransactionsProps = { export function Transactions({ count, txTypes }: AddressTransactionsProps) { const { formatAddress } = useCentrifugeUtils() const address = useAddress() - const transactions = useTransactionsByAddress(formatAddress(address || '')) + const transactions = useTransactionsByAddress(formatAddress(address || ''), count, txTypes) const match = useRouteMatch('/portfolio/transactions') const [sortKey, setSortKey] = React.useState<'date' | 'amount'>('date') const [sortOrder, setSortOrder] = React.useState<'asc' | 'desc'>('desc') - const investorTransactions = React.useMemo(() => { + const investorTransactions: TransactionCardProps[] = React.useMemo(() => { const txs = transactions?.investorTransactions - .filter((tx) => (txTypes ? txTypes?.includes(tx.type) : tx)) .map((tx) => { return { date: new Date(tx.timestamp).getTime(), @@ -66,10 +65,10 @@ export function Transactions({ count, txTypes }: AddressTransactionsProps) { return 1 } }) || [] - return sortOrder === 'asc' ? txs : txs.reverse() + return sortOrder === 'asc' ? txs.reverse() : txs }, [sortKey, transactions, sortOrder]) - return !!investorTransactions.slice(0, count ?? investorTransactions.length) ? ( + return !!investorTransactions ? ( {match ? null : 'Transaction history'} @@ -127,7 +126,7 @@ export function Transactions({ count, txTypes }: AddressTransactionsProps) { - {investorTransactions.slice(0, count ?? investorTransactions.length).map((transaction, index) => ( + {investorTransactions.map((transaction, index) => ( @@ -136,7 +135,7 @@ export function Transactions({ count, txTypes }: AddressTransactionsProps) { {match ? null : ( - + View all )} diff --git a/centrifuge-app/src/utils/usePools.ts b/centrifuge-app/src/utils/usePools.ts index cfdc1ab69e..db1e5f64cb 100644 --- a/centrifuge-app/src/utils/usePools.ts +++ b/centrifuge-app/src/utils/usePools.ts @@ -1,4 +1,4 @@ -import Centrifuge, { Pool, PoolMetadata } from '@centrifuge/centrifuge-js' +import Centrifuge, { InvestorTransactionType, Pool, PoolMetadata } from '@centrifuge/centrifuge-js' import { useCentrifuge, useCentrifugeQuery, useWallet } from '@centrifuge/centrifuge-react' import { useEffect } from 'react' import { useQuery } from 'react-query' @@ -49,10 +49,10 @@ export function useMonthlyPoolStates(poolId: string, from?: Date, to?: Date) { return result } -export function useTransactionsByAddress(address?: string) { +export function useTransactionsByAddress(address?: string, count?: number, txTypes?: InvestorTransactionType[]) { const [result] = useCentrifugeQuery( ['txByAddress', address], - (cent) => cent.pools.getTransactionsByAddress([address!]), + (cent) => cent.pools.getTransactionsByAddress([address!, count, txTypes]), { enabled: !!address, } diff --git a/centrifuge-js/src/modules/pools.ts b/centrifuge-js/src/modules/pools.ts index 625eea78f2..7b01b24ee3 100644 --- a/centrifuge-js/src/modules/pools.ts +++ b/centrifuge-js/src/modules/pools.ts @@ -2015,18 +2015,17 @@ export function getPoolsModule(inst: Centrifuge) { ) } - function getTransactionsByAddress(args: [address: string]) { - const [address] = args + function getTransactionsByAddress(args: [address: string, count?: number, txTypes?: InvestorTransactionType[]]) { + const [address, count, txTypes] = args const $query = inst.getSubqueryObservable<{ investorTransactions: { nodes: SubqueryInvestorTransaction[] } }>( - `query($address: String!) { - investorTransactions(filter: { - accountId: { - equalTo: $address - } - }) { + `query ($address: String) { + investorTransactions( + filter: {accountId: {equalTo: $address}} + orderBy: TIMESTAMP_DESC + ) { nodes { timestamp type @@ -2064,11 +2063,10 @@ export function getPoolsModule(inst: Centrifuge) { return forkJoin([$investorTransactions]).pipe( map(([investorTransactions]) => { - investorTransactions.sort((a, b) => { - return new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime() - }) return { - investorTransactions, + investorTransactions: investorTransactions + .filter((tx) => (txTypes ? txTypes?.includes(tx.type) : tx)) + .slice(0, count || investorTransactions.length), } }) )