Skip to content

Commit

Permalink
fix: add periodic polling
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Sep 25, 2020
1 parent 1821940 commit 08599a8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
9 changes: 9 additions & 0 deletions app/pages/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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]);
Expand Down
9 changes: 6 additions & 3 deletions app/store/transaction/transaction.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ export const pendingTransactionSuccessful = createAction<Transaction>(
export const addNewTransaction = createAction<Transaction>('transactions/new-transaction');

const fetchTxName = 'transactions/fetch-transactions';
export const fetchTransactions = createAction(fetchTxName);
export const fetchTransactions = createAction<{ displayLoading?: boolean }>(fetchTxName);
export const fetchTransactionsDone = createAction<Transaction[]>(fetchTxName + '-done');
export const fetchTransactionsFail = createAction<string>(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));
Expand Down
6 changes: 5 additions & 1 deletion app/store/transaction/transaction.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 08599a8

Please sign in to comment.