@@ -84,24 +84,48 @@ class LinkList extends Component {
8484 _updateCacheAfterVote = ( store , createVote , linkId ) => {
8585 const isNewPage = this . props . location . pathname . includes ( 'new' )
8686 const page = parseInt ( this . props . match . params . page , 10 )
87-
87+
8888 const skip = isNewPage ? ( page - 1 ) * LINKS_PER_PAGE : 0
8989 const first = isNewPage ? LINKS_PER_PAGE : 100
9090 const orderBy = isNewPage ? 'createdAt_DESC' : null
9191 const data = store . readQuery ( {
9292 query : FEED_QUERY ,
9393 variables : { first, skip, orderBy }
9494 } )
95-
95+
9696 const votedLink = data . feed . links . find ( link => link . id === linkId )
9797 votedLink . votes = createVote . link . votes
9898 store . writeQuery ( { query : FEED_QUERY , data } )
9999 }
100100
101+ _subscribeToNewLinks = subscribeToMore => {
102+ subscribeToMore ( {
103+ document : NEW_LINKS_SUBSCRIPTION ,
104+ updateQuery : ( prev , { subscriptionData } ) => {
105+ if ( ! subscriptionData . data ) return prev
106+ const newLink = subscriptionData . data . newLink . node
107+
108+ return Object . assign ( { } , prev , {
109+ feed : {
110+ links : [ newLink , ...prev . feed . links ] ,
111+ count : prev . feed . links . length + 1 ,
112+ __typename : prev . feed . __typename
113+ }
114+ } )
115+ }
116+ } )
117+ }
118+
119+ _subscribeToNewVotes = subscribeToMore => {
120+ subscribeToMore ( {
121+ document : NEW_VOTES_SUBSCRIPTION
122+ } )
123+ }
124+
101125 _getQueryVariables = ( ) => {
102126 const isNewPage = this . props . location . pathname . includes ( 'new' )
103127 const page = parseInt ( this . props . match . params . page , 10 )
104-
128+
105129 const skip = isNewPage ? ( page - 1 ) * LINKS_PER_PAGE : 0
106130 const first = isNewPage ? LINKS_PER_PAGE : 100
107131 const orderBy = isNewPage ? 'createdAt_DESC' : null
@@ -125,7 +149,7 @@ class LinkList extends Component {
125149 this . props . history . push ( `/new/${ nextPage } ` )
126150 }
127151 }
128-
152+
129153 _previousPage = ( ) => {
130154 const page = parseInt ( this . props . match . params . page , 10 )
131155 if ( page > 1 ) {
@@ -134,46 +158,22 @@ class LinkList extends Component {
134158 }
135159 }
136160
137- _subscribeToNewLinks = subscribeToMore => {
138- subscribeToMore ( {
139- document : NEW_LINKS_SUBSCRIPTION ,
140- updateQuery : ( prev , { subscriptionData } ) => {
141- if ( ! subscriptionData . data ) return prev
142- const newLink = subscriptionData . data . newLink . node
143-
144- return Object . assign ( { } , prev , {
145- feed : {
146- links : [ newLink , ...prev . feed . links ] ,
147- count : prev . feed . links . length + 1 ,
148- __typename : prev . feed . __typename
149- }
150- } )
151- }
152- } )
153- }
154-
155- _subscribeToNewVotes = subscribeToMore => {
156- subscribeToMore ( {
157- document : NEW_VOTES_SUBSCRIPTION
158- } )
159- }
160-
161161 render ( ) {
162162 return (
163163 < Query query = { FEED_QUERY } variables = { this . _getQueryVariables ( ) } >
164164 { ( { loading, error, data, subscribeToMore } ) => {
165165 if ( loading ) return < div > Fetching</ div >
166166 if ( error ) return < div > Error</ div >
167-
167+
168168 this . _subscribeToNewLinks ( subscribeToMore )
169169 this . _subscribeToNewVotes ( subscribeToMore )
170-
170+
171171 const linksToRender = this . _getLinksToRender ( data )
172172 const isNewPage = this . props . location . pathname . includes ( 'new' )
173173 const pageIndex = this . props . match . params . page
174174 ? ( this . props . match . params . page - 1 ) * LINKS_PER_PAGE
175175 : 0
176-
176+
177177 return (
178178 < Fragment >
179179 { linksToRender . map ( ( link , index ) => (
@@ -189,10 +189,7 @@ class LinkList extends Component {
189189 < div className = "pointer mr2" onClick = { this . _previousPage } >
190190 Previous
191191 </ div >
192- < div
193- className = "pointer"
194- onClick = { this . _nextPage . bind ( this , data ) }
195- >
192+ < div className = "pointer" onClick = { ( ) => this . _nextPage ( data ) } >
196193 Next
197194 </ div >
198195 </ div >
@@ -205,4 +202,4 @@ class LinkList extends Component {
205202 }
206203}
207204
208- export default LinkList
205+ export default LinkList
0 commit comments