@@ -2,12 +2,12 @@ import React, { Component } from 'react'
22import Link from './Link'
33import { Query } from 'react-apollo'
44import gql from 'graphql-tag'
5+ import { LINKS_PER_PAGE } from '../constants'
56
6- // 1
77export 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
1111 links {
1212 id
1313 createdAt
@@ -24,6 +24,7 @@ export const FEED_QUERY = gql`
2424 }
2525 }
2626 }
27+ count
2728 }
2829 }
2930`
@@ -109,15 +110,27 @@ class LinkList extends Component {
109110 )
110111 }
111112
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+
112122 render ( ) {
113123 return (
114- < Query query = { FEED_QUERY } >
124+ < Query query = { FEED_QUERY } variables = { this . getQueryVariables ( ) } >
115125 { result => {
116126 if ( result . loading ) return < div > Loading</ div >
117127 if ( result . error ) return < div > Error</ div >
118128
119129 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 )
121134
122135 return (
123136 < div >
@@ -139,6 +152,19 @@ class LinkList extends Component {
139152 link = { link }
140153 />
141154 ) ) }
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+ ) }
142168 </ div >
143169 )
144170 } }
@@ -147,14 +173,18 @@ class LinkList extends Component {
147173 }
148174
149175 _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+ } )
152185
153- // 2
154186 const votedLink = data . feed . links . find ( link => link . id === linkId )
155187 votedLink . votes = createVote . link . votes
156-
157- // 3
158188 store . writeQuery ( { query : FEED_QUERY , data } )
159189 }
160190
@@ -186,6 +216,31 @@ class LinkList extends Component {
186216 document : NEW_VOTES_SUBSCRIPTION ,
187217 } )
188218 }
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+ }
189244}
190245
191246export default LinkList
0 commit comments