@@ -31,20 +31,18 @@ export const FEED_QUERY = gql`
31
31
const NEW_LINKS_SUBSCRIPTION = gql `
32
32
subscription {
33
33
newLink {
34
- node {
34
+ id
35
+ url
36
+ description
37
+ createdAt
38
+ postedBy {
35
39
id
36
- url
37
- description
38
- createdAt
39
- postedBy {
40
+ name
41
+ }
42
+ votes {
43
+ id
44
+ user {
40
45
id
41
- name
42
- }
43
- votes {
44
- id
45
- user {
46
- id
47
- }
48
46
}
49
47
}
50
48
}
@@ -54,28 +52,26 @@ const NEW_LINKS_SUBSCRIPTION = gql`
54
52
const NEW_VOTES_SUBSCRIPTION = gql `
55
53
subscription {
56
54
newVote {
57
- node {
55
+ id
56
+ link {
58
57
id
59
- link {
58
+ url
59
+ description
60
+ createdAt
61
+ postedBy {
60
62
id
61
- url
62
- description
63
- createdAt
64
- postedBy {
65
- id
66
- name
67
- }
68
- votes {
69
- id
70
- user {
71
- id
72
- }
73
- }
63
+ name
74
64
}
75
- user {
65
+ votes {
76
66
id
67
+ user {
68
+ id
69
+ }
77
70
}
78
71
}
72
+ user {
73
+ id
74
+ }
79
75
}
80
76
}
81
77
`
@@ -84,15 +80,15 @@ class LinkList extends Component {
84
80
_updateCacheAfterVote = ( store , createVote , linkId ) => {
85
81
const isNewPage = this . props . location . pathname . includes ( 'new' )
86
82
const page = parseInt ( this . props . match . params . page , 10 )
87
-
83
+
88
84
const skip = isNewPage ? ( page - 1 ) * LINKS_PER_PAGE : 0
89
85
const first = isNewPage ? LINKS_PER_PAGE : 100
90
86
const orderBy = isNewPage ? 'createdAt_DESC' : null
91
87
const data = store . readQuery ( {
92
88
query : FEED_QUERY ,
93
- variables : { first, skip, orderBy }
89
+ variables : { first, skip, orderBy } ,
94
90
} )
95
-
91
+
96
92
const votedLink = data . feed . links . find ( link => link . id === linkId )
97
93
votedLink . votes = createVote . link . votes
98
94
store . writeQuery ( { query : FEED_QUERY , data } )
@@ -103,29 +99,29 @@ class LinkList extends Component {
103
99
document : NEW_LINKS_SUBSCRIPTION ,
104
100
updateQuery : ( prev , { subscriptionData } ) => {
105
101
if ( ! subscriptionData . data ) return prev
106
- const newLink = subscriptionData . data . newLink . node
107
-
102
+ const newLink = subscriptionData . data . newLink
103
+ const exists = prev . feed . links . find ( ( { id } ) => id === newLink . id )
104
+ if ( exists ) return prev
108
105
return Object . assign ( { } , prev , {
109
106
feed : {
110
107
links : [ newLink , ...prev . feed . links ] ,
111
108
count : prev . feed . links . length + 1 ,
112
- __typename : prev . feed . __typename
113
- }
109
+ __typename : prev . feed . __typename ,
110
+ } ,
114
111
} )
115
- }
112
+ } ,
116
113
} )
117
114
}
118
115
119
116
_subscribeToNewVotes = subscribeToMore => {
120
117
subscribeToMore ( {
121
- document : NEW_VOTES_SUBSCRIPTION
118
+ document : NEW_VOTES_SUBSCRIPTION ,
122
119
} )
123
120
}
124
-
125
121
_getQueryVariables = ( ) => {
126
122
const isNewPage = this . props . location . pathname . includes ( 'new' )
127
123
const page = parseInt ( this . props . match . params . page , 10 )
128
-
124
+
129
125
const skip = isNewPage ? ( page - 1 ) * LINKS_PER_PAGE : 0
130
126
const first = isNewPage ? LINKS_PER_PAGE : 100
131
127
const orderBy = isNewPage ? 'createdAt_DESC' : null
@@ -141,15 +137,14 @@ class LinkList extends Component {
141
137
rankedLinks . sort ( ( l1 , l2 ) => l2 . votes . length - l1 . votes . length )
142
138
return rankedLinks
143
139
}
144
-
145
140
_nextPage = data => {
146
141
const page = parseInt ( this . props . match . params . page , 10 )
147
142
if ( page <= data . feed . count / LINKS_PER_PAGE ) {
148
143
const nextPage = page + 1
149
144
this . props . history . push ( `/new/${ nextPage } ` )
150
145
}
151
146
}
152
-
147
+
153
148
_previousPage = ( ) => {
154
149
const page = parseInt ( this . props . match . params . page , 10 )
155
150
if ( page > 1 ) {
@@ -164,16 +159,16 @@ class LinkList extends Component {
164
159
{ ( { loading, error, data, subscribeToMore } ) => {
165
160
if ( loading ) return < div > Fetching</ div >
166
161
if ( error ) return < div > Error</ div >
167
-
162
+
168
163
this . _subscribeToNewLinks ( subscribeToMore )
169
164
this . _subscribeToNewVotes ( subscribeToMore )
170
-
165
+
171
166
const linksToRender = this . _getLinksToRender ( data )
172
167
const isNewPage = this . props . location . pathname . includes ( 'new' )
173
168
const pageIndex = this . props . match . params . page
174
169
? ( this . props . match . params . page - 1 ) * LINKS_PER_PAGE
175
170
: 0
176
-
171
+
177
172
return (
178
173
< Fragment >
179
174
{ linksToRender . map ( ( link , index ) => (
@@ -202,4 +197,4 @@ class LinkList extends Component {
202
197
}
203
198
}
204
199
205
- export default LinkList
200
+ export default LinkList
0 commit comments