Skip to content

Commit 21e624a

Browse files
committed
limit votes to 1 per user
1 parent 8eb5ae9 commit 21e624a

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/components/CreateLink.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@ class CreateLink extends Component {
5252
postedById
5353
},
5454
update: (store, { data: { createLink } }) => {
55-
5655
const first = LINKS_PER_PAGE
5756
const skip = 0
58-
const orderBy = "createdAt_DESC"
59-
57+
const orderBy = 'createdAt_DESC'
6058
const data = store.readQuery({
6159
query: ALL_LINKS_QUERY,
6260
variables: { first, skip, orderBy }
@@ -92,6 +90,9 @@ const CREATE_LINK_MUTATION = gql`
9290
}
9391
votes {
9492
id
93+
user {
94+
id
95+
}
9596
}
9697
}
9798
}

src/components/Link.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class Link extends Component {
77
render() {
88

99
const userId = localStorage.getItem(GC_USER_ID)
10-
console.log(`Link - userId:`, userId)
1110
return (
1211
<div>
1312
{userId && <div onClick={() => this._voteForLink()}></div>}
@@ -18,8 +17,14 @@ class Link extends Component {
1817
}
1918

2019
_voteForLink = async () => {
21-
const linkId = this.props.link.id
2220
const userId = localStorage.getItem(GC_USER_ID)
21+
const voterIds = this.props.link.votes.map(vote => vote.user.id)
22+
if (voterIds.includes(userId)) {
23+
console.log(`User (${userId}) already voted for this link.`)
24+
return
25+
}
26+
27+
const linkId = this.props.link.id
2328
await this.props.createVoteMutation({
2429
variables: {
2530
userId,
@@ -40,8 +45,14 @@ const CREATE_VOTE_MUTATION = gql`
4045
link {
4146
votes {
4247
id
48+
user {
49+
id
50+
}
4351
}
4452
}
53+
user {
54+
id
55+
}
4556
}
4657
}
4758
`

src/components/LinkList.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ class LinkList extends Component {
3838
<button onClick={() => {
3939
localStorage.removeItem(GC_USER_ID)
4040
localStorage.removeItem(GC_AUTH_TOKEN)
41-
this.forceUpdate()
42-
{/*this.setState({})*/}
41+
this.forceUpdate() // doesn't work as it should :(
4342
}}>Logout</button>
4443
</div>
4544
}
@@ -133,8 +132,14 @@ class LinkList extends Component {
133132
}
134133
votes {
135134
id
135+
user {
136+
id
137+
}
136138
}
137139
}
140+
user {
141+
id
142+
}
138143
}
139144
}
140145
}
@@ -197,6 +202,9 @@ export const ALL_LINKS_QUERY = gql`
197202
}
198203
votes {
199204
id
205+
user {
206+
id
207+
}
200208
}
201209
}
202210
_allLinksMeta {

0 commit comments

Comments
 (0)