@@ -2,12 +2,12 @@ import React, { Component } from 'react'
2
2
import Link from './Link'
3
3
import { Query } from 'react-apollo'
4
4
import gql from 'graphql-tag'
5
+ import { LINKS_PER_PAGE } from '../constants'
5
6
6
- // 1
7
7
export const FEED_QUERY = gql `
8
- # 2
9
- query FeedQuery {
10
- feed {
8
+ query FeedQuery($first: Int, $skip: Int, $orderBy: LinkOrderByInput) {
9
+ feed(first: $first, skip: $skip, orderBy: $orderBy) {
10
+ count
11
11
links {
12
12
id
13
13
createdAt
@@ -24,6 +24,7 @@ export const FEED_QUERY = gql`
24
24
}
25
25
}
26
26
}
27
+ count
27
28
}
28
29
}
29
30
`
@@ -109,15 +110,27 @@ class LinkList extends Component {
109
110
)
110
111
}
111
112
113
+ getQueryVariables = ( ) => {
114
+ const page = parseInt ( this . props . match . params . page , 10 )
115
+ const isNewPage = this . props . location . pathname . includes ( 'new' )
116
+ const skip = isNewPage ? ( page - 1 ) * LINKS_PER_PAGE : 0
117
+ const first = isNewPage ? LINKS_PER_PAGE : 100
118
+ const orderBy = isNewPage ? 'createdAt_DESC' : null
119
+ return { first, skip, orderBy }
120
+ }
121
+
112
122
render ( ) {
113
123
return (
114
- < Query query = { FEED_QUERY } >
124
+ < Query query = { FEED_QUERY } variables = { this . getQueryVariables ( ) } >
115
125
{ result => {
116
126
if ( result . loading ) return < div > Loading</ div >
117
127
if ( result . error ) return < div > Error</ div >
118
128
119
129
const { data, subscribeToMore } = result
120
- const linksToRender = data . feed . links
130
+
131
+ const isNewPage = this . props . location . pathname . includes ( 'new' )
132
+ const linksToRender = this . _getLinksToRender ( isNewPage , data )
133
+ const page = parseInt ( this . props . match . params . page , 10 )
121
134
122
135
return (
123
136
< div >
@@ -139,6 +152,19 @@ class LinkList extends Component {
139
152
link = { link }
140
153
/>
141
154
) ) }
155
+ { isNewPage && (
156
+ < div className = "flex ml4 mv3 gray" >
157
+ < div
158
+ className = "pointer mr2"
159
+ onClick = { ( ) => this . _previousPage ( ) }
160
+ >
161
+ Previous
162
+ </ div >
163
+ < div className = "pointer" onClick = { ( ) => this . _nextPage ( data ) } >
164
+ Next
165
+ </ div >
166
+ </ div >
167
+ ) }
142
168
</ div >
143
169
)
144
170
} }
@@ -147,14 +173,18 @@ class LinkList extends Component {
147
173
}
148
174
149
175
_updateCacheAfterVote = ( store , createVote , linkId ) => {
150
- // 1
151
- const data = store . readQuery ( { query : FEED_QUERY } )
176
+ const isNewPage = this . props . location . pathname . includes ( 'new' )
177
+ const page = parseInt ( this . props . match . params . page , 10 )
178
+ const skip = isNewPage ? ( page - 1 ) * LINKS_PER_PAGE : 0
179
+ const first = isNewPage ? LINKS_PER_PAGE : 100
180
+ const orderBy = isNewPage ? 'createdAt_DESC' : null
181
+ const data = store . readQuery ( {
182
+ query : FEED_QUERY ,
183
+ variables : { first, skip, orderBy } ,
184
+ } )
152
185
153
- // 2
154
186
const votedLink = data . feed . links . find ( link => link . id === linkId )
155
187
votedLink . votes = createVote . link . votes
156
-
157
- // 3
158
188
store . writeQuery ( { query : FEED_QUERY , data } )
159
189
}
160
190
@@ -186,6 +216,31 @@ class LinkList extends Component {
186
216
document : NEW_VOTES_SUBSCRIPTION ,
187
217
} )
188
218
}
219
+
220
+ _getLinksToRender = ( isNewPage , data ) => {
221
+ if ( isNewPage ) {
222
+ return data . feed . links
223
+ }
224
+ const rankedLinks = data . feed . links . slice ( )
225
+ rankedLinks . sort ( ( l1 , l2 ) => l2 . votes . length - l1 . votes . length )
226
+ return rankedLinks
227
+ }
228
+
229
+ _nextPage = data => {
230
+ const page = parseInt ( this . props . match . params . page , 10 )
231
+ if ( page <= data . feed . count / LINKS_PER_PAGE ) {
232
+ const nextPage = page + 1
233
+ this . props . history . push ( `/new/${ nextPage } ` )
234
+ }
235
+ }
236
+
237
+ _previousPage = ( ) => {
238
+ const page = parseInt ( this . props . match . params . page , 10 )
239
+ if ( page > 1 ) {
240
+ const previousPage = page - 1
241
+ this . props . history . push ( `/new/${ previousPage } ` )
242
+ }
243
+ }
189
244
}
190
245
191
246
export default LinkList
0 commit comments