diff --git a/src/components/voting/votingBar.js b/src/components/voting/votingBar.js index 350a7688b..6ecf37903 100644 --- a/src/components/voting/votingBar.js +++ b/src/components/voting/votingBar.js @@ -4,15 +4,13 @@ import grid from 'flexboxgrid/dist/flexboxgrid.css'; import style from './votingBar.css'; import votingConst from '../../constants/voting'; +import { getTotalVotesCount, getVoteList, getUnvoteList } from './../../utils/voting'; const VotingBar = ({ votes, t }) => { const { maxCountOfVotes, maxCountOfVotesInOneTurn } = votingConst; - const votedList = Object.keys(votes).filter(key => votes[key].confirmed); - const voteList = Object.keys(votes).filter( - key => votes[key].unconfirmed && !votes[key].confirmed); - const unvoteList = Object.keys(votes).filter( - key => !votes[key].unconfirmed && votes[key].confirmed); - const totalVotesCount = (votedList.length - unvoteList.length) + voteList.length; + const voteList = getVoteList(votes); + const unvoteList = getUnvoteList(votes); + const totalVotesCount = getTotalVotesCount(votes); const totalNewVotesCount = voteList.length + unvoteList.length; return (voteList.length + unvoteList.length ? diff --git a/src/store/middlewares/voting.js b/src/store/middlewares/voting.js index 807d58d38..9da581fd9 100644 --- a/src/store/middlewares/voting.js +++ b/src/store/middlewares/voting.js @@ -5,6 +5,7 @@ import { getDelegate } from '../../utils/api/delegate'; import { voteLookupStatusUpdated, voteToggled } from '../../actions/voting'; import actionTypes from '../../constants/actions'; import votingConst from '../../constants/voting'; +import { getTotalVotesCount } from './../../utils/voting'; const updateLookupStatus = (store, list, username) => { store.dispatch(voteLookupStatusUpdated({ @@ -68,8 +69,7 @@ const checkVoteLimits = (store, action) => { store.dispatch(newAction); } - const voteCount = Object.keys(votes).filter( - key => (votes[key].confirmed && !votes[key].unconfirmed) || votes[key].unconfirmed).length; + const voteCount = getTotalVotesCount(votes); if (voteCount === votingConst.maxCountOfVotes + 1 && currentVote.unconfirmed !== currentVote.confirmed) { const label = i18next.t('Maximum of {{n}} votes exceeded.', { n: votingConst.maxCountOfVotes }); diff --git a/src/utils/voting.js b/src/utils/voting.js new file mode 100644 index 000000000..771f3439a --- /dev/null +++ b/src/utils/voting.js @@ -0,0 +1,13 @@ + +const getVotedList = votes => (Object.keys(votes).filter(key => votes[key].confirmed)); + +const getUnvoteList = votes => (Object.keys(votes).filter( + key => !votes[key].unconfirmed && votes[key].confirmed)); + +const getVoteList = votes => (Object.keys(votes).filter( + key => votes[key].unconfirmed && !votes[key].confirmed)); + +const getTotalVotesCount = votes => ((getVotedList(votes).length - getUnvoteList(votes).length) + + getVoteList(votes).length); + +export { getTotalVotesCount, getVotedList, getVoteList, getUnvoteList };