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

Voting: fix bug with votes not loading initially #812

Merged
merged 2 commits into from
Apr 17, 2019
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
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 || {},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to ensure there's an object in

open: isVoteOpen(vote, now),
},
connectedAccountVote: connectedAccountVotes[vote.voteId] || VOTE_ABSENT,
}))


// 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 = [] }) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we leave a comment for why we need to default this?

const connectedAccountVotes = await Promise.all(
votes.map(({ voteId }) => getVoterState({ connectedAccount, voteId }))
)
Expand Down