Skip to content

Commit

Permalink
Merge pull request #2129 from LiskHQ/2027-refactor-votes-redux
Browse files Browse the repository at this point in the history
Refactor votes redux - Closes #2027
  • Loading branch information
slaweet committed Jul 23, 2019
2 parents 45ed122 + 1540fa2 commit ea8c311
Show file tree
Hide file tree
Showing 19 changed files with 386 additions and 451 deletions.
41 changes: 3 additions & 38 deletions src/actions/voting.js
Expand Up @@ -33,22 +33,6 @@ export const voteToggled = data => ({
data,
});


/**
* Updates vote lookup status of the given delegate name
*/
export const voteLookupStatusUpdated = data => ({
type: actionTypes.voteLookupStatusUpdated,
data,
});

/**
* Clears all vote lookup statuses
*/
export const voteLookupStatusCleared = () => ({
type: actionTypes.voteLookupStatusCleared,
});

export const clearVotes = () => ({
type: actionTypes.votesCleared,
});
Expand Down Expand Up @@ -99,7 +83,7 @@ export const votePlaced = ({
* Gets the list of delegates current account has voted for
*
*/
export const loadVotes = ({ address, type }) =>
export const loadVotes = ({ address, type, callback = () => null }) =>
(dispatch, getState) => {
const liskAPIClient = getAPIClient(tokenMap.LSK.key, getState());
getVotes(liskAPIClient, { address })
Expand All @@ -108,14 +92,15 @@ export const loadVotes = ({ address, type }) =>
type: type === 'update' ? actionTypes.votesUpdated : actionTypes.votesAdded,
data: { list: response.data.votes },
});
callback(response.data.votes);
});
};

/**
* Gets list of all delegates
*/
export const loadDelegates = ({
offset, refresh, q, callback = () => {},
offset = 0, refresh, q, callback = () => {},
}) =>
(dispatch, getState) => {
const liskAPIClient = getAPIClient(tokenMap.LSK.key, getState());
Expand All @@ -136,23 +121,3 @@ export const loadDelegates = ({
})
.catch(callback);
};


/**
* Get list of delegates current account has voted for and dispatch it with votes from url
*/
export const urlVotesFound = ({
upvotes, unvotes, address,
}) =>
(dispatch, getState) => {
const liskAPIClient = getAPIClient(tokenMap.LSK.key, getState());
const processUrlVotes = (votes) => {
dispatch({
type: actionTypes.votesAdded,
data: { list: votes, upvotes, unvotes },
});
};
getVotes(liskAPIClient, { address })
.then((response) => { processUrlVotes(response.data.votes); })
.catch(() => { processUrlVotes([]); });
};
65 changes: 0 additions & 65 deletions src/actions/voting.test.js
Expand Up @@ -3,10 +3,8 @@ import sinon from 'sinon';
import actionTypes from '../constants/actions';
import {
voteToggled,
voteLookupStatusUpdated,
votePlaced,
loadVotes,
urlVotesFound,
loadDelegates,
delegatesAdded,
} from './voting';
Expand Down Expand Up @@ -55,20 +53,6 @@ describe('actions: voting', () => {
});
});

describe('voteLookupStatusUpdated', () => {
it('should create an action to update lookup status of any given delegate name', () => {
const data = {
label: 'dummy',
};
const expectedAction = {
data,
type: actionTypes.voteLookupStatusUpdated,
};

expect(voteLookupStatusUpdated(data)).to.be.deep.equal(expectedAction);
});
});

describe('votePlaced', () => {
let delegateApiMock;
const account = {
Expand Down Expand Up @@ -230,53 +214,4 @@ describe('actions: voting', () => {
delegateApiMock.restore();
});
});

describe('urlVotesFound', () => {
let delegateApiMock;
const data = {
address: '8096217735672704724L',
upvotes: [],
unvotes: [],
};
const delegates = delegateList;
let expectedAction = {
list: delegates,
upvotes: [],
unvotes: [],
};

beforeEach(() => {
delegateApiMock = sinon.stub(delegateApi, 'getVotes').returnsPromise();
});

afterEach(() => {
delegateApiMock.restore();
});

it('should create an action function', () => {
expect(typeof urlVotesFound(data)).to.be.deep.equal('function');
});

it('should dispatch votesAdded action when resolved', () => {
const dispatch = sinon.spy();


urlVotesFound(data)(dispatch, getState);
delegateApiMock.resolves({ data: { votes: delegates } });
expect(dispatch).to.have.been.calledWith(votesAdded(expectedAction));
});

it('should dispatch votesAdded action when rejected', () => {
const dispatch = sinon.spy();

expectedAction = {
...expectedAction,
list: [],
};

urlVotesFound(data)(dispatch, getState);
delegateApiMock.rejects();
expect(dispatch).to.have.been.calledWith(votesAdded(expectedAction));
});
});
});
11 changes: 2 additions & 9 deletions src/components/voting/index.js
Expand Up @@ -2,23 +2,16 @@
import { connect } from 'react-redux';
import { translate } from 'react-i18next';
import { withRouter } from 'react-router';
import Voting from './voting';
import { clearVotes, votePlaced } from '../../actions/voting';
import { filterObjectPropsWithValue } from '../../utils/helpers';
import { getActiveTokenAccount } from '../../utils/account';
import { votePlaced } from '../../actions/voting';
import Voting from './voting';

const mapStateToProps = state => ({
votes: state.voting.votes,
account: getActiveTokenAccount(state),
voteLookupStatus: {
pending: filterObjectPropsWithValue(state.voting.voteLookupStatus, 'pending'),
alreadyVoted: filterObjectPropsWithValue(state.voting.voteLookupStatus, 'alreadyVoted').concat(filterObjectPropsWithValue(state.voting.voteLookupStatus, 'notVotedYet')),
notFound: filterObjectPropsWithValue(state.voting.voteLookupStatus, 'notFound'),
},
});

const mapDispatchToProps = {
clearVotes,
votePlaced,
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/voting/voteList.js
Expand Up @@ -19,7 +19,7 @@ const VoteList = ({
<div className={`${styles.votesContainer} ${className}`}>
{list.map(vote => (
<span key={vote} className={`${styles.voteTag} vote`}>
{ votes[vote]
{ votes[vote] && votes[vote].rank
? (
<span className={styles.rank}>
#
Expand Down
15 changes: 12 additions & 3 deletions src/components/voting/voteUrlProcessor/index.js
Expand Up @@ -3,17 +3,26 @@ import { connect } from 'react-redux';
import { translate } from 'react-i18next';
import { withRouter } from 'react-router';
import {
urlVotesFound,
loadDelegates,
loadVotes,
voteLookupStatusCleared,
voteToggled,
} from '../../../actions/voting';
import VoteUrlProcessor from './voteUrlProcessor';

const mapDispatchToProps = {
urlVotesFound,
voteLookupStatusCleared,
voteToggled,
loadVotes,
loadDelegates,
};

const mapStateToProps = state => ({
delegates: state.voting.delegates,
liskAPIClient: state.peers.liskAPIClient,
});

export default withRouter(connect(
null,
mapStateToProps,
mapDispatchToProps,
)(translate()(VoteUrlProcessor)));

0 comments on commit ea8c311

Please sign in to comment.