Skip to content

Commit

Permalink
Check both ip and user id when viewing poll
Browse files Browse the repository at this point in the history
  • Loading branch information
orellazri committed Feb 13, 2021
1 parent 948e5d2 commit 143fdc2
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/pages/ViewPoll.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default {
};
},
computed: mapState(['error', 'loading']),
computed: mapState(['error', 'loading', 'user']),
async created() {
this.id = this.$route.params.pollId;
Expand All @@ -117,10 +117,17 @@ export default {
return;
}
const vote = await db.collection('polls').doc(this.id).collection('votes').doc(this.ip).get();
this.hasVoted = vote.exists;
// Check if the user has voted on this poll already
let vote = await db.collection('polls').doc(this.id).collection('votes').where('ip', '==', this.ip).get();
this.hasVoted = vote.docs.length > 0;
// Try looking for user id instead of ip, if ip was not found
if (!this.hasVoted && this.user != null) {
vote = await db.collection('polls').doc(this.id).collection('votes').where('user_id', '==', this.user.uid).get();
this.hasVoted = vote.docs.length > 0;
}
if (this.hasVoted) {
this.indexVoted = vote.data().choice;
this.indexVoted = vote.docs[0].data().choice;
}
// Update chart data
Expand Down Expand Up @@ -162,8 +169,10 @@ export default {
return;
}
// Add a vote document with the user's ip
await db.collection('polls').doc(this.id).collection('votes').doc(this.ip).set({
// Add a vote document with the user's details
await db.collection('polls').doc(this.id).collection('votes').doc().set({
ip: this.ip,
user_id: (this.user != null) ? this.user.uid : null,
choice: this.choicePicked,
});
Expand Down

0 comments on commit 143fdc2

Please sign in to comment.