From 08599a8f14400ed882c0a3ad147ddbabcb2ae99c Mon Sep 17 00:00:00 2001 From: kyranjamie Date: Tue, 15 Sep 2020 14:34:28 +0200 Subject: [PATCH] fix: add periodic polling --- app/pages/app.tsx | 9 +++++++++ app/store/transaction/transaction.actions.ts | 9 ++++++--- app/store/transaction/transaction.reducer.ts | 6 +++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/pages/app.tsx b/app/pages/app.tsx index 09733aba8..74e3ad4dc 100644 --- a/app/pages/app.tsx +++ b/app/pages/app.tsx @@ -16,6 +16,7 @@ import { safeAwait } from '@utils/safe-await'; import { Api } from '../api/api'; import { selectActiveNodeApi } from '@store/stacks-node'; import urljoin from 'url-join'; +import { useInterval } from '../hooks/use-interval'; export const App: FC = ({ children }) => { const dispatch = useDispatch(); @@ -32,10 +33,18 @@ export const App: FC = ({ children }) => { dispatch(getAddressDetails(address)); }, [address, dispatch]); + const refreshWalletDetailsWithoutLoader = useCallback(() => { + if (!address) return; + dispatch(getAddressTransactions(address, { displayLoading: false })); + dispatch(getAddressDetails(address)); + }, [address, dispatch]); + useNavigatorOnline({ onReconnect: initAppWithStxAddressInfo, }); + useInterval(() => refreshWalletDetailsWithoutLoader(), 60_000); + useEffect(() => { initAppWithStxAddressInfo(); }, [address, activeNode, initAppWithStxAddressInfo]); diff --git a/app/store/transaction/transaction.actions.ts b/app/store/transaction/transaction.actions.ts index 575485a7f..8460c47db 100644 --- a/app/store/transaction/transaction.actions.ts +++ b/app/store/transaction/transaction.actions.ts @@ -18,13 +18,16 @@ export const pendingTransactionSuccessful = createAction( export const addNewTransaction = createAction('transactions/new-transaction'); const fetchTxName = 'transactions/fetch-transactions'; -export const fetchTransactions = createAction(fetchTxName); +export const fetchTransactions = createAction<{ displayLoading?: boolean }>(fetchTxName); export const fetchTransactionsDone = createAction(fetchTxName + '-done'); export const fetchTransactionsFail = createAction(fetchTxName + '-fail'); -export function getAddressTransactions(address: string) { +export function getAddressTransactions( + address: string, + options: { displayLoading?: boolean } = {} +) { return async (dispatch: Dispatch, getState: GetState) => { - dispatch(fetchTransactions()); + dispatch(fetchTransactions(options)); const activeNode = selectActiveNodeApi(getState()); const client = new Api(activeNode.url); const [error, response] = await safeAwait(client.getAddressTransactions(address)); diff --git a/app/store/transaction/transaction.reducer.ts b/app/store/transaction/transaction.reducer.ts index 7453f5b79..0bd678b39 100644 --- a/app/store/transaction/transaction.reducer.ts +++ b/app/store/transaction/transaction.reducer.ts @@ -30,7 +30,11 @@ const initialState = transactionAdapter.getInitialState({ export const transactionReducer = createReducer(initialState, builder => builder - .addCase(fetchTransactions, state => ({ ...state, fetchTxError: null, loading: true })) + .addCase(fetchTransactions, (state, action) => ({ + ...state, + fetchTxError: null, + loading: action.payload.displayLoading ?? true, + })) .addCase(broadcastTx, state => ({ ...state, mostRecentBroadcastError: null })) .addCase(fetchTransactionsDone, (state, action) => ({ ...transactionAdapter.addMany({ ...state }, action),