@@ -12,6 +12,7 @@ export const FEED_QUERY = gql`
1212 $orderBy: LinkOrderByInput
1313 ) {
1414 feed(take: $take, skip: $skip, orderBy: $orderBy) {
15+ id
1516 links {
1617 id
1718 createdAt
@@ -92,13 +93,36 @@ const getLinksToRender = (isNewPage, data) => {
9293 return rankedLinks ;
9394} ;
9495
96+ const getQueryVariables = ( isNewPage , page ) => {
97+ const skip = isNewPage ? ( page - 1 ) * LINKS_PER_PAGE : 0 ;
98+ const take = isNewPage ? LINKS_PER_PAGE : 100 ;
99+ const orderBy = isNewPage ? 'createdAt_DESC' : null ;
100+ return { take, skip, orderBy } ;
101+ } ;
102+
95103const LinkList = ( ) => {
96- const { data, loading, error } = useQuery ( FEED_QUERY ) ;
97104 const history = useHistory ( ) ;
98105 const isNewPage = history . location . pathname . includes (
99106 'new'
100107 ) ;
101- const pageIndex = 1 ;
108+ const pageIndexParams = history . location . pathname . split (
109+ '/'
110+ ) ;
111+ const page = parseInt (
112+ pageIndexParams [ pageIndexParams . length - 1 ]
113+ ) ;
114+
115+ const pageIndex = page ? ( page - 1 ) * LINKS_PER_PAGE : 0 ;
116+
117+ const {
118+ data,
119+ loading,
120+ error,
121+ subscribeToMore
122+ } = useQuery ( FEED_QUERY , {
123+ variables : getQueryVariables ( isNewPage , page )
124+ } ) ;
125+
102126 return (
103127 < >
104128 { loading && < p > Loading...</ p > }
@@ -111,23 +135,32 @@ const LinkList = () => {
111135 key = { link . id }
112136 link = { link }
113137 index = { index + pageIndex }
114- updateStoreAfterVote = { ( ) =>
115- console . log ( 'update' )
116- }
117138 />
118139 )
119140 ) }
120141 { isNewPage && (
121142 < div className = "flex ml4 mv3 gray" >
122143 < div
123144 className = "pointer mr2"
124- onClick = { ( ) => console . log ( 'prev page' ) }
145+ onClick = { ( ) => {
146+ if ( page > 1 ) {
147+ history . push ( `/new/${ page - 1 } ` ) ;
148+ }
149+ } }
125150 >
126151 Previous
127152 </ div >
128153 < div
129154 className = "pointer"
130- onClick = { ( ) => console . log ( 'next page' ) }
155+ onClick = { ( ) => {
156+ if (
157+ page <=
158+ data . feed . count / LINKS_PER_PAGE
159+ ) {
160+ const nextPage = page + 1 ;
161+ history . push ( `/new/${ nextPage } ` ) ;
162+ }
163+ } }
131164 >
132165 Next
133166 </ div >
0 commit comments