From c6611b5bba81e3d43b63e1f2db69497de2904c8b Mon Sep 17 00:00:00 2001 From: michaeltomasik Date: Fri, 17 Aug 2018 11:20:10 +0200 Subject: [PATCH] :recycle: Remove activePeer from followedAccounts --- src/actions/followedAccounts.js | 5 +++-- src/actions/followedAccounts.test.js | 7 +++++-- src/store/middlewares/followedAccounts.js | 13 +++++-------- src/store/middlewares/followedAccounts.test.js | 1 - 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/actions/followedAccounts.js b/src/actions/followedAccounts.js index c9c4dd0b15..efba72b129 100644 --- a/src/actions/followedAccounts.js +++ b/src/actions/followedAccounts.js @@ -21,8 +21,9 @@ export const followedAccountsRetrieved = accounts => ({ type: actionTypes.followedAccountsRetrieved, }); -export const followedAccountFetchedAndUpdated = ({ activePeer, account }) => - (dispatch) => { +export const followedAccountFetchedAndUpdated = ({ account }) => + (dispatch, getState) => { + const activePeer = getState().peers.data; getAccount(activePeer, account.address).then((result) => { if (result.balance !== account.balance) { account.balance = result.balance; diff --git a/src/actions/followedAccounts.test.js b/src/actions/followedAccounts.test.js index 1cab2cb2b5..0f1c79ee3e 100644 --- a/src/actions/followedAccounts.test.js +++ b/src/actions/followedAccounts.test.js @@ -18,6 +18,9 @@ describe('actions: followedAccount', () => { balance: accounts.genesis.balance, title: accounts.genesis.address, }; + const getState = () => ({ + peers: { data: {} }, + }); it('should create an action to retrieve the followed accounts list', () => { const expectedAction = { @@ -57,13 +60,13 @@ describe('actions: followedAccount', () => { // Case 1: balance does not change accountApi.getAccount.resolves({ balance: accounts.genesis.balance }); - followedAccountFetchedAndUpdated({ activePeer: {}, account: data })(dispatch); + followedAccountFetchedAndUpdated({ account: data })(dispatch, getState); expect(dispatch).to.not.have.been.calledWith(); // Case 2: balance does change accountApi.getAccount.resolves({ balance: 0 }); - followedAccountFetchedAndUpdated({ activePeer: {}, account: data })(dispatch); + followedAccountFetchedAndUpdated({ account: data })(dispatch, getState); expect(dispatch).to.been.calledWith(followedAccountUpdated({ publicKey: accounts.genesis.publicKey, balance: 0, diff --git a/src/store/middlewares/followedAccounts.js b/src/store/middlewares/followedAccounts.js index 80e9d75423..bc5334033c 100644 --- a/src/store/middlewares/followedAccounts.js +++ b/src/store/middlewares/followedAccounts.js @@ -2,16 +2,15 @@ import actionTypes from '../../constants/actions'; import { followedAccountFetchedAndUpdated } from '../../actions/followedAccounts'; const followedAccountsMiddleware = (store) => { - const updateFollowedAccounts = (peers, accounts) => { + const updateFollowedAccounts = (accounts) => { accounts.forEach((account) => { store.dispatch(followedAccountFetchedAndUpdated({ - activePeer: peers.data, account, })); }); }; - const checkTransactionsAndUpdateFollowedAccounts = (peers, tx, followedAccounts) => { + const checkTransactionsAndUpdateFollowedAccounts = (tx, followedAccounts) => { const changedAccounts = followedAccounts.accounts.filter((account) => { const relevantTransactions = tx.filter((transaction) => { const { senderId, recipientId } = transaction; @@ -21,28 +20,26 @@ const followedAccountsMiddleware = (store) => { return relevantTransactions.length > 0; }); - updateFollowedAccounts(peers, changedAccounts); + updateFollowedAccounts(changedAccounts); }; return next => (action) => { next(action); - const { peers, followedAccounts } = store.getState(); + const { followedAccounts } = store.getState(); switch (action.type) { case actionTypes.newBlockCreated: checkTransactionsAndUpdateFollowedAccounts( - peers, action.data.block.transactions || [], followedAccounts, ); break; case actionTypes.followedAccountAdded: store.dispatch(followedAccountFetchedAndUpdated({ - activePeer: peers.data, account: action.data, })); break; case actionTypes.activePeerSet: - updateFollowedAccounts(peers, followedAccounts.accounts); + updateFollowedAccounts(followedAccounts.accounts); break; default: break; diff --git a/src/store/middlewares/followedAccounts.test.js b/src/store/middlewares/followedAccounts.test.js index f6a16f013d..4c19e6370a 100644 --- a/src/store/middlewares/followedAccounts.test.js +++ b/src/store/middlewares/followedAccounts.test.js @@ -25,7 +25,6 @@ describe('FollowedAccounts middleware', () => { store = mock(); store.dispatch = spy(); store.getState = () => ({ - peers: { data: {} }, followedAccounts: { accounts: [account, account2] }, }); next = spy();