@@ -29,7 +29,10 @@ export const FEED_QUERY = gql`
29
29
}
30
30
`
31
31
32
- const updateCacheAfterVote = ( page , isNewPage , cache , createVote , linkId ) => {
32
+ const updateCacheAfterVote = ( props , cache , createVote , linkId ) => {
33
+ const isNewPage = props . location . pathname . includes ( 'new' )
34
+ const page = parseInt ( props . match . params . page , 10 )
35
+
33
36
const skip = isNewPage ? ( page - 1 ) * LINKS_PER_PAGE : 0
34
37
const first = isNewPage ? LINKS_PER_PAGE : 100
35
38
const orderBy = isNewPage ? 'createdAt_DESC' : null
@@ -43,14 +46,18 @@ const updateCacheAfterVote = (page, isNewPage, cache, createVote, linkId) => {
43
46
cache . writeQuery ( { query : FEED_QUERY , data } )
44
47
}
45
48
46
- const getQueryVariables = ( page , isNewPage ) => {
49
+ const getQueryVariables = props => {
50
+ const isNewPage = props . location . pathname . includes ( 'new' )
51
+ const page = parseInt ( props . match . params . page , 10 )
52
+
47
53
const skip = isNewPage ? ( page - 1 ) * LINKS_PER_PAGE : 0
48
54
const first = isNewPage ? LINKS_PER_PAGE : 100
49
55
const orderBy = isNewPage ? 'createdAt_DESC' : null
50
56
return { first, skip, orderBy }
51
57
}
52
58
53
- const getLinksToRender = ( isNewPage , data ) => {
59
+ const getLinksToRender = ( props , data ) => {
60
+ const isNewPage = props . location . pathname . includes ( 'new' )
54
61
if ( isNewPage ) {
55
62
return data . feed . links
56
63
}
@@ -59,30 +66,31 @@ const getLinksToRender = (isNewPage, data) => {
59
66
return rankedLinks
60
67
}
61
68
62
- const nextPage = ( page , data , history ) => {
69
+ const nextPage = ( props , data ) => {
70
+ const page = parseInt ( props . match . params . page , 10 )
63
71
if ( page <= data . feed . count / LINKS_PER_PAGE ) {
64
72
const nextPage = page + 1
65
- history . push ( `/new/${ nextPage } ` )
73
+ props . history . push ( `/new/${ nextPage } ` )
66
74
}
67
75
}
68
76
69
- const previousPage = ( page , history ) => {
77
+ const previousPage = props => {
78
+ const page = parseInt ( props . match . params . page , 10 )
70
79
if ( page > 1 ) {
71
80
const previousPage = page - 1
72
- history . push ( `/new/${ previousPage } ` )
81
+ props . history . push ( `/new/${ previousPage } ` )
73
82
}
74
83
}
75
84
76
- export default ( { match, location, history } ) => {
77
- const isNewPage = location . pathname . includes ( 'new' )
78
- const page = parseInt ( match . params . page , 10 )
85
+ export default props => {
79
86
return (
80
- < Query query = { FEED_QUERY } variables = { getQueryVariables ( page , isNewPage ) } >
87
+ < Query query = { FEED_QUERY } variables = { getQueryVariables ( props ) } >
81
88
{ ( { loading, error, data, subscribeToMore } ) => {
82
89
if ( loading ) return < div > Fetching</ div >
83
90
if ( error ) return < div > Error</ div >
84
91
85
- const linksToRender = getLinksToRender ( isNewPage , data )
92
+ const linksToRender = getLinksToRender ( props , data )
93
+ const isNewPage = props . location . pathname . includes ( 'new' )
86
94
87
95
return (
88
96
< LinkListSubscriptions subscribeToMore = { subscribeToMore } >
@@ -91,24 +99,20 @@ export default ({ match, location, history }) => {
91
99
key = { link . id }
92
100
link = { link }
93
101
index = { index }
94
- updateStoreAfterVote = { updateCacheAfterVote . bind (
95
- this ,
96
- page ,
97
- isNewPage
98
- ) }
102
+ updateStoreAfterVote = { updateCacheAfterVote . bind ( this , props ) }
99
103
/>
100
104
) ) }
101
105
{ isNewPage && (
102
106
< div className = "flex ml4 mv3 gray" >
103
107
< div
104
108
className = "pointer mr2"
105
- onClick = { ( ) => previousPage ( page , history ) }
109
+ onClick = { previousPage . bind ( this , props ) }
106
110
>
107
111
Previous
108
112
</ div >
109
113
< div
110
114
className = "pointer"
111
- onClick = { ( ) => nextPage ( page , data , history ) }
115
+ onClick = { nextPage . bind ( this , props , data ) }
112
116
>
113
117
Next
114
118
</ div >
0 commit comments