Skip to content

Commit

Permalink
Voting: fix bug with votes not loading initially (#812)
Browse files Browse the repository at this point in the history
* Voting: fix bug with votes not loading initially

* Add comment
  • Loading branch information
2color authored Apr 17, 2019
1 parent 0ddce97 commit 0bf6ff4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 9 additions & 1 deletion apps/voting/app/src/app-state-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ function appStateReducer(state) {
return { ...state, ready }
}

const { pctBase, tokenDecimals, voteTime, votes } = state
const {
pctBase,
tokenDecimals,
voteTime,
votes,
connectedAccountVotes,
} = state

const pctBaseNum = parseInt(pctBase, 10)
const tokenDecimalsNum = parseInt(tokenDecimals, 10)
Expand All @@ -26,6 +32,8 @@ function appStateReducer(state) {
tokenDecimals: tokenDecimalsNum,
},

connectedAccountVotes: connectedAccountVotes || {},

// Transform the vote data for the frontend
votes: votes
? votes.map(vote => {
Expand Down
14 changes: 8 additions & 6 deletions apps/voting/app/src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ async function updateConnectedAccount(state, { account }) {
return {
...state,
// fetch all the votes casted by the connected account
connectedAccountVotes: await getAccountVotes({
connectedAccount: account,
votes: state.votes,
}),
connectedAccountVotes: state.votes
? await getAccountVotes({
connectedAccount: account,
votes: state.votes,
})
: {},
}
}

Expand Down Expand Up @@ -221,8 +223,8 @@ async function startVote(state, { creator, metadata, voteId }) {
* Helpers *
* *
***********************/

async function getAccountVotes({ connectedAccount, votes }) {
// Default votes to an empty array to prevent errors on initial load
async function getAccountVotes({ connectedAccount, votes = [] }) {
const connectedAccountVotes = await Promise.all(
votes.map(({ voteId }) => getVoterState({ connectedAccount, voteId }))
)
Expand Down

0 comments on commit 0bf6ff4

Please sign in to comment.