1- import React , { Fragment } from 'react' ;
1+ import React from 'react' ;
22import { useQuery , useSubscription } from 'urql' ;
33import gql from 'graphql-tag' ;
4- import { LINKS_PER_PAGE } from '../constants' ;
4+
55import Link from './Link' ;
66
77export const FEED_QUERY = gql `
@@ -76,59 +76,54 @@ const NEW_VOTES_SUBSCRIPTION = gql`
7676 }
7777` ;
7878
79- const LinkList = ( { location, match, history } ) => {
80- const isNewPage = location . pathname . includes ( 'new' ) ;
81-
82- const variables = React . useMemo ( ( ) => {
83- const page = parseInt ( match . params . page , 10 ) ;
79+ const LinkList = props => {
80+ const isNewPage = props . location . pathname . includes ( 'new' )
81+ const page = parseInt ( props . match . params . page , 10 )
8482
85- const skip = isNewPage ? ( page - 1 ) * LINKS_PER_PAGE : 0 ;
86- const first = isNewPage ? LINKS_PER_PAGE : 100 ;
87- const orderBy = isNewPage ? 'createdAt_DESC' : null ;
88- return { first , skip , orderBy } ;
89- } , [ match , isNewPage ] ) ;
83+ const variables = React . useMemo ( ( ) => ( {
84+ skip : isNewPage ? ( page - 1 ) * 10 : 0 ,
85+ first : isNewPage ? 10 : 100 ,
86+ orderBy : isNewPage ? 'createdAt_DESC' : null
87+ } ) , [ isNewPage , page ] )
9088
91- const [ { data, error, fetching } ] = useQuery ( {
92- query : FEED_QUERY ,
93- variables,
94- } ) ;
89+ const [ result ] = useQuery ( { query : FEED_QUERY , variables } ) ;
90+ const { data, fetching, error } = result ;
9591
9692 useSubscription ( { query : NEW_VOTES_SUBSCRIPTION } ) ;
9793 useSubscription ( { query : NEW_LINKS_SUBSCRIPTION } ) ;
9894
9995 const linksToRender = React . useMemo ( ( ) => {
100- if ( fetching ) return [ ] ;
101- if ( isNewPage ) return data . feed . links ;
102- const rankedLinks = data . feed . links . slice ( ) ;
103- rankedLinks . sort ( ( l1 , l2 ) => l2 . votes . length - l1 . votes . length ) ;
104- return rankedLinks ;
105- } , [ data , fetching , isNewPage ] ) ;
96+ if ( ! data ) {
97+ return [ ] ;
98+ } else if ( isNewPage ) {
99+ return data . feed . links ;
100+ } else {
101+ const rankedLinks = data . feed . links
102+ . slice ( )
103+ . sort ( ( l1 , l2 ) => l2 . votes . length - l1 . votes . length ) ;
104+ return rankedLinks ;
105+ }
106+ } , [ data , isNewPage ] ) ;
106107
107108 const nextPage = React . useCallback ( ( ) => {
108- const page = parseInt ( match . params . page , 10 ) ;
109- if ( page <= data . feed . count / LINKS_PER_PAGE ) {
110- const nextPage = page + 1 ;
111- history . push ( `/new/${ nextPage } ` ) ;
109+ if ( page <= data . feed . count / 10 ) {
110+ props . history . push ( `/new/${ page + 1 } ` ) ;
112111 }
113- } , [ data , history , match . params ] ) ;
112+ } , [ props . history , data . feed . count , page ] ) ;
114113
115114 const previousPage = React . useCallback ( ( ) => {
116- const page = parseInt ( match . params . page , 10 ) ;
117115 if ( page > 1 ) {
118- const previousPage = page - 1 ;
119- history . push ( `/new/${ previousPage } ` ) ;
116+ props . history . push ( `/new/${ page - 1 } ` ) ;
120117 }
121- } , [ match , history ] ) ;
118+ } , [ props . history , page ] ) ;
122119
123120 if ( fetching ) return < div > Fetching</ div > ;
124121 if ( error ) return < div > Error</ div > ;
125122
126- const pageIndex = match . params . page
127- ? ( match . params . page - 1 ) * LINKS_PER_PAGE
128- : 0 ;
123+ const pageIndex = isNewPage ? ( page - 1 ) * 10 : 0
129124
130125 return (
131- < Fragment >
126+ < React . Fragment >
132127 { linksToRender . map ( ( link , index ) => (
133128 < Link
134129 key = { link . id }
@@ -146,7 +141,7 @@ const LinkList = ({ location, match, history }) => {
146141 </ div >
147142 </ div >
148143 ) }
149- </ Fragment >
144+ </ React . Fragment >
150145 ) ;
151146}
152147
0 commit comments