Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor api calls in middlewares - Closes #770 #882

Merged
merged 4 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 58 additions & 34 deletions src/actions/account.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import i18next from 'i18next';
import actionTypes from '../constants/actions';
import { setSecondPassphrase, send, getAccount } from '../utils/api/account';
import { setSecondPassphrase, getAccount } from '../utils/api/account';
import { registerDelegate, getDelegate, getVotes, getVoters } from '../utils/api/delegate';
import { loadTransactionsFinish } from './transactions';
import { loadTransactionsFinish, transactionsUpdated } from './transactions';
import { delegateRegisteredFailure } from './delegate';
import { errorAlertDialogDisplayed } from './dialog';
import { activePeerUpdate } from './peers';
import Fees from '../constants/fees';
import { toRawLsk } from '../utils/lsk';
import transactionTypes from '../constants/transactionTypes';

/**
Expand Down Expand Up @@ -113,6 +113,18 @@ export const secondPassphraseRegistered = ({
dispatch(passphraseUsed(passphrase));
};


export const updateDelegateAccount = ({ activePeer, publicKey }) =>
(dispatch) => {
getDelegate(activePeer, { publicKey })
.then((delegateData) => {
dispatch(accountUpdated(Object.assign(
{},
{ delegate: delegateData.delegate, isDelegate: true },
)));
});
};

/**
*
*/
Expand Down Expand Up @@ -142,36 +154,6 @@ export const delegateRegistered = ({
dispatch(passphraseUsed(passphrase));
};

/**
*
*/
export const sent = ({
activePeer, account, recipientId, amount, passphrase, secondPassphrase,
}) =>
(dispatch) => {
send(activePeer, recipientId, toRawLsk(amount), passphrase, secondPassphrase)
.then((data) => {
dispatch({
data: {
id: data.transactionId,
senderPublicKey: account.publicKey,
senderId: account.address,
recipientId,
amount: toRawLsk(amount),
fee: Fees.send,
type: transactionTypes.send,
},
type: actionTypes.transactionAdded,
});
})
.catch((error) => {
const errorMessage = error && error.message ? `${error.message}.` : i18next.t('An error occurred while creating the transaction.');
dispatch({ data: { errorMessage }, type: actionTypes.transactionFailed });
});
dispatch(passphraseUsed(passphrase));
};


export const loadDelegate = ({ activePeer, publicKey }) =>
(dispatch) => {
getDelegate(activePeer, { publicKey }).then((response) => {
Expand All @@ -190,7 +172,6 @@ export const loadAccount = ({
transactionsResponse,
isSameAccount,
}) =>

(dispatch) => {
getAccount(activePeer, address)
.then((response) => {
Expand All @@ -215,3 +196,46 @@ export const loadAccount = ({
dispatch(loadTransactionsFinish(accountDataUpdated));
});
};

export const updateTransactionsIfNeeded = ({ transactions, activePeer, account }, windowFocus) =>
(dispatch) => {
const hasRecentTransactions = txs => (
txs.confirmed.filter(tx => tx.confirmations < 1000).length !== 0 ||
txs.pending.length !== 0
);

if (windowFocus || !hasRecentTransactions(transactions)) {
const { filter } = transactions;
const address = transactions.account ? transactions.account.address : account.address;

dispatch(transactionsUpdated({
pendingTransactions: transactions.pending,
activePeer,
address,
limit: 25,
filter,
}));
}
};

export const accountDataUpdated = ({
peers, account, windowIsFocused, transactions,
}) =>
(dispatch) => {
getAccount(peers.data, account.address).then((result) => {
if (result.balance !== account.balance) {
dispatch(updateTransactionsIfNeeded(
{
transactions,
activePeer: peers.data,
account,
},
!windowIsFocused,
));
}
dispatch(accountUpdated(result));
dispatch(activePeerUpdate({ online: true }));
}).catch((res) => {
dispatch(activePeerUpdate({ online: false, code: res.error.code }));
});
};
Loading