File tree Expand file tree Collapse file tree 2 files changed +21
-13
lines changed Expand file tree Collapse file tree 2 files changed +21
-13
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,6 @@ import { AUTH_TOKEN } from '../constants'
3
3
import { timeDifferenceForDate } from '../utils'
4
4
import { Mutation } from 'react-apollo'
5
5
import gql from 'graphql-tag'
6
- import { FEED_QUERY } from './LinkList'
7
6
8
7
const VOTE_MUTATION = gql `
9
8
mutation VoteMutation($linkId: ID!) {
@@ -36,17 +35,9 @@ class Link extends Component {
36
35
< Mutation
37
36
mutation = { VOTE_MUTATION }
38
37
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
+ }
50
41
>
51
42
{ voteMutation => (
52
43
< div className = "ml1 gray f11" onClick = { voteMutation } >
Original file line number Diff line number Diff line change @@ -42,14 +42,31 @@ class LinkList extends Component {
42
42
return (
43
43
< div >
44
44
{ 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
+ />
46
51
) ) }
47
52
</ div >
48
53
)
49
54
} }
50
55
</ Query >
51
56
)
52
57
}
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
+ }
53
70
}
54
71
55
72
export default LinkList
You can’t perform that action at this time.
0 commit comments