Skip to content

Commit 7ca367d

Browse files
author
Peter
committed
Indicate which users has finished voting for a poll #6
1 parent 17bacf4 commit 7ca367d

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

frontend/src/components/Poll.vue

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
<hr v-if="poll.options.length"/>
5353
<poll-option-add :poll="poll"></poll-option-add>
5454
</div>
55+
56+
<div class="voterInfo"
57+
v-if="poll.status >=5">
58+
#voters: {{voterCount}}
59+
</div>
5560
</div>
5661
</template>
5762

@@ -85,6 +90,9 @@
8590
back: faStepBackward
8691
}
8792
},
93+
voterCount () {
94+
return this.$store.state.voters[this.poll.id] && this.$store.state.voters[this.poll.id].length
95+
},
8896
options () {
8997
if (this.poll.status < 10) {
9098
this.poll.options.sort((a, b) => {
@@ -273,6 +281,12 @@
273281
right: 0;
274282
border: 1px solid red;
275283
}
284+
.voterInfo {
285+
margin-top: 2em;
286+
border-top: 1px solid #e9ebee;
287+
text-align: right;
288+
padding: 0.5em;
289+
}
276290
277291
@media only screen
278292
and (max-device-width : 1024px) {

frontend/src/store/store.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const state = {
1010
polls: [],
1111
activeUsers: [],
1212
me: {},
13-
votes: {}
13+
votes: {},
14+
voters: {}
1415
}
1516

1617
const getters = {}
@@ -30,6 +31,9 @@ export const store = new Vuex.Store({
3031
vote (state, vote) {
3132
Vue.set(state.votes, vote.poll_id, vote)
3233
},
34+
setVoters (state, voters) {
35+
state.voters = voters
36+
},
3337
'POLL_CREATED' (state, poll) {
3438
state.polls.push(poll)
3539
},
@@ -75,6 +79,14 @@ export const store = new Vuex.Store({
7579
},
7680
'CONNECTED_USERS' (state, users) {
7781
state.activeUsers = users
82+
},
83+
'USER_VOTED' (state, info) {
84+
if (state.voters[info.poll_id] === undefined) {
85+
Vue.set(state.voters, info.poll_id, [info.user_id])
86+
}
87+
if (!state.voters[info.poll_id].some(user => user === info.user_id)) {
88+
state.voters[info.poll_id].push(info.user_id)
89+
}
7890
}
7991
},
8092
actions: {
@@ -98,6 +110,9 @@ export const store = new Vuex.Store({
98110
commit('INIT_VOTES', map)
99111
})
100112
}
113+
API.get('/api/voters').then(voters => {
114+
commit('setVoters', voters)
115+
})
101116
},
102117
vote ({commit, state}, vote) {
103118
let votes = state.votes[vote.poll_id]
@@ -156,7 +171,8 @@ export const store = new Vuex.Store({
156171
'CONNECTED_USERS' ({commit}, users) {
157172
commit('CONNECTED_USERS', users)
158173
},
159-
'USER_VOTED' (_, info) {
174+
'USER_VOTED' ({commit}, info) {
175+
commit('USER_VOTED', info)
160176
EventBus.$emit('USER_VOTED', info)
161177
}
162178
},

server/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ func main() {
4949

5050
authorized.GET("api/votes", polls.GetVotes)
5151

52+
authorized.GET("api/voters", polls.GetVoters)
53+
5254
authorized.GET("api/users", users.GetUsers)
5355
authorized.PATCH("api/users/:userId", users.SetAdminPermission)
5456

server/polls/voteHandlers.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ func HandleVote(c *gin.Context) {
8585
}{Success: true})
8686
}
8787

88+
func GetVoters(c *gin.Context) {
89+
var votes []Vote
90+
config.DB.Find(&votes)
91+
92+
votersByPoll := map[string][]string{}
93+
for _, vote := range votes {
94+
if _, exists := votersByPoll[vote.PollId]; !exists {
95+
votersByPoll[vote.PollId] = []string{}
96+
}
97+
votersByPoll[vote.PollId] = append(votersByPoll[vote.PollId], vote.UserId)
98+
}
99+
c.JSON(http.StatusOK, votersByPoll)
100+
}
101+
88102
func allVoteOptionsInPoll(v VoteDto, options []Option) bool {
89103
return (len(v.Score1) == 0 || optionExists(options, v.Score1)) &&
90104
(len(v.Score2) == 0 || optionExists(options, v.Score2)) &&

0 commit comments

Comments
 (0)