Skip to content

Commit 9d0c824

Browse files
author
Carlos Rufo Jimenez
committed
#fix
1 parent f991235 commit 9d0c824

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/components/Link.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { AUTH_TOKEN } from '../constants'
33
import { timeDifferenceForDate } from '../utils'
44
import { Mutation } from 'react-apollo'
55
import gql from 'graphql-tag'
6-
import { FEED_QUERY } from './LinkList'
76

87
const VOTE_MUTATION = gql`
98
mutation VoteMutation($linkId: ID!) {
@@ -36,17 +35,9 @@ class Link extends Component {
3635
<Mutation
3736
mutation={VOTE_MUTATION}
3837
variables={{ linkId }}
39-
update={(cache, { data: { vote: createVote } }) => {
40-
// 1
41-
const data = cache.readQuery({ query: FEED_QUERY })
42-
// 2
43-
const votedLink = data.feed.links.find(
44-
link => link.id === linkId
45-
)
46-
votedLink.votes = createVote.link.votes
47-
// 3
48-
cache.writeQuery({ query: FEED_QUERY, data })
49-
}}
38+
update={(cache, { data: { vote } }) =>
39+
this.props.updateStoreAfterVote(cache, vote, linkId)
40+
}
5041
>
5142
{voteMutation => (
5243
<div className="ml1 gray f11" onClick={voteMutation}>

src/components/LinkList.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,31 @@ class LinkList extends Component {
4242
return (
4343
<div>
4444
{linksToRender.map((link, index) => (
45-
<Link key={link.id} index={index} link={link} />
45+
<Link
46+
key={link.id}
47+
updateStoreAfterVote={this._updateCacheAfterVote}
48+
index={index}
49+
link={link}
50+
/>
4651
))}
4752
</div>
4853
)
4954
}}
5055
</Query>
5156
)
5257
}
58+
59+
_updateCacheAfterVote = (store, createVote, linkId) => {
60+
// 1
61+
const data = store.readQuery({ query: FEED_QUERY })
62+
63+
// 2
64+
const votedLink = data.feed.links.find(link => link.id === linkId)
65+
votedLink.votes = createVote.link.votes
66+
67+
// 3
68+
store.writeQuery({ query: FEED_QUERY, data })
69+
}
5370
}
5471

5572
export default LinkList

0 commit comments

Comments
 (0)