@@ -37,11 +37,11 @@ const defaultData: TransactionHistoryData = {
3737export function useTransactionHistory (
3838 account ?: string
3939) : UseMyTransactionsResult {
40- const [ data , setData ] = useState < TransactionHistoryData > ( defaultData ) ;
40+ const [ txData , setTxData ] = useState < TransactionHistoryData > ( defaultData ) ;
4141 const [ loading , setLoading ] = useState < boolean > ( false ) ;
4242
43- const fetchData = useCallback ( async ( ) => {
44- if ( ! account ) return ;
43+ const fetchTxData = useCallback ( async ( ) : Promise < TransactionHistoryData > => {
44+ if ( ! account ) return defaultData ;
4545 setLoading ( true ) ;
4646
4747 const response = await getSinkTxsForRecipient ( {
@@ -50,39 +50,50 @@ export function useTransactionHistory(
5050
5151 if ( response . data === undefined ) {
5252 setLoading ( false ) ;
53- return ;
53+ return defaultData ;
5454 }
5555
5656 const serializedTransactions =
5757 TransactionHistoryService . serializeTxsResponse ( response . data ) ;
5858
59- setData ( {
59+ setLoading ( false ) ;
60+
61+ return {
6062 myTransactions : serializedTransactions ,
6163 totalSunk : Number ( response . data . total_carbon_sunk ) ,
6264 totalPending : Number ( response . data . total_carbon_pending ) ,
6365 retirementGraceDays : response . data . retirement_grace_days ,
64- } ) ;
66+ } ;
6567
66- setLoading ( false ) ;
68+ // setTxData({
69+ // myTransactions: serializedTransactions,
70+ // totalSunk: Number(response.data.total_carbon_sunk),
71+ // totalPending: Number(response.data.total_carbon_pending),
72+ // retirementGraceDays: response.data.retirement_grace_days,
73+ // });
74+
75+ // setLoading(false);
6776 } , [ account ] ) ;
6877
6978 async function pollForNewTransaction (
7079 maxRetries : number = 5 ,
71- delay : number = 3000 // delay in milliseconds
80+ delay : number = 800 // delay in milliseconds
7281 ) : Promise < void > {
7382 if ( ! account ) throw new Error ( "No account for polling" ) ;
7483
7584 let retries = 0 ;
76- const oldTransactions = data . myTransactions ;
85+ const oldTransactions = txData . myTransactions ;
7786
7887 while ( retries < maxRetries ) {
79- const transactionRecords =
80- await TransactionHistoryService . fetchAccountHistory ( account ) ;
81-
82- if ( hasNewItem ( transactionRecords ) ) {
83- setData ( ( prev ) => {
84- return { ...prev , myTransactions : transactionRecords } ;
85- } ) ;
88+ console . log ( "polling" , retries ) ;
89+ const transactionRecords = await fetchTxData ( ) ;
90+
91+ if ( hasNewItem ( transactionRecords . myTransactions ) ) {
92+ console . log (
93+ transactionRecords . totalPending ,
94+ transactionRecords . totalSunk
95+ ) ;
96+ setTxData ( transactionRecords ) ;
8697 return ;
8798 }
8899
@@ -107,21 +118,23 @@ export function useTransactionHistory(
107118
108119 useEffect ( ( ) => {
109120 if ( ! account ) {
110- setData ( defaultData ) ;
121+ setTxData ( defaultData ) ;
111122 } else {
112- fetchData ( ) ;
123+ fetchTxData ( ) . then ( ( txData ) => {
124+ setTxData ( txData ) ;
125+ } ) ;
113126 }
114- } , [ fetchData , account ] ) ;
127+ } , [ fetchTxData , account ] ) ;
115128
116129 const refetch = useCallback ( ( ) => {
117- if ( account ) fetchData ( ) ;
118- } , [ account , fetchData ] ) ;
130+ if ( account ) fetchTxData ( ) ;
131+ } , [ account , fetchTxData ] ) ;
119132
120133 return {
121- myTransactions : data . myTransactions ,
122- totalSunk : data . totalSunk ,
123- totalPending : data . totalPending ,
124- retirementGraceDays : data . retirementGraceDays ,
134+ myTransactions : txData . myTransactions ,
135+ totalSunk : txData . totalSunk ,
136+ totalPending : txData . totalPending ,
137+ retirementGraceDays : txData . retirementGraceDays ,
125138 loading,
126139 refetch,
127140 pollForNewTransaction,
0 commit comments