1
1
import React , { Component } from 'react'
2
2
import { gql , graphql } from 'react-apollo'
3
3
import Link from './Link'
4
- import { GC_USER_ID } from '../constants'
4
+ import { GC_USER_ID , GC_AUTH_TOKEN , LINKS_PER_PAGE } from '../constants'
5
5
6
- const LINKS_PER_PAGE = 2
7
6
8
7
class LinkList extends Component {
9
8
@@ -13,33 +12,60 @@ class LinkList extends Component {
13
12
}
14
13
15
14
render ( ) {
16
- if ( this . props . allLinksQuery . loading ) {
15
+
16
+ if ( this . props . allLinksQuery && this . props . allLinksQuery . loading ) {
17
17
return < div > Loading</ div >
18
18
}
19
+
20
+ if ( this . props . allLinksQuery && this . props . allLinksQuery . error ) {
21
+ return < div > Error</ div >
22
+ }
23
+
24
+ const isNewPage = this . props . location . pathname . includes ( 'new' )
25
+ const linksToRender = this . _getLinksToRender ( isNewPage )
19
26
const userId = localStorage . getItem ( GC_USER_ID )
27
+
20
28
return (
21
29
< div >
22
30
{ ! userId ?
23
31
< button onClick = { ( ) => {
24
- const { history } = this . props
25
- history . push ( '/login' )
32
+ this . props . history . push ( '/login' )
26
33
} } > Login</ button > :
27
- < button onClick = { ( ) => {
28
- const { history } = this . props
29
- history . push ( '/create' )
30
- } } > New Post</ button >
34
+ < div >
35
+ < button onClick = { ( ) => {
36
+ this . props . history . push ( '/create' )
37
+ } } > New Post</ button >
38
+ < button onClick = { ( ) => {
39
+ localStorage . removeItem ( GC_USER_ID )
40
+ localStorage . removeItem ( GC_AUTH_TOKEN )
41
+ this . forceUpdate ( )
42
+ { /*this.setState({})*/ }
43
+ } } > Logout</ button >
44
+ </ div >
31
45
}
32
46
< div >
33
- { this . props . allLinksQuery . allLinks . map ( link => (
47
+ { linksToRender . map ( link => (
34
48
< Link key = { link . id } updateStoreAfterVote = { this . _updateCacheAfterVote } link = { link } />
35
49
) ) }
36
50
</ div >
51
+ { isNewPage &&
37
52
< div >
38
- < button onClick = { ( ) => this . _previousPage ( ) } > previous </ button >
39
- < button onClick = { ( ) => this . _nextPage ( ) } > next </ button >
53
+ < button onClick = { ( ) => this . _previousPage ( ) } > Previous </ button >
54
+ < button onClick = { ( ) => this . _nextPage ( ) } > Next </ button >
40
55
</ div >
56
+ }
41
57
</ div >
42
58
)
59
+
60
+ }
61
+
62
+ _getLinksToRender = ( isNewPage ) => {
63
+ if ( isNewPage ) {
64
+ return this . props . allLinksQuery . allLinks
65
+ }
66
+ const rankedLinks = this . props . allLinksQuery . allLinks . slice ( )
67
+ rankedLinks . sort ( ( l1 , l2 ) => l2 . votes . length - l1 . votes . length )
68
+ return rankedLinks
43
69
}
44
70
45
71
_subscribeToNewLinks = ( ) => {
@@ -66,15 +92,23 @@ class LinkList extends Component {
66
92
}
67
93
` ,
68
94
updateQuery : ( previous , { subscriptionData } ) => {
69
- const newAllLinks = [
70
- subscriptionData . data . Link . node ,
71
- ...previous . allLinks
72
- ]
73
- const result = {
74
- ...previous ,
75
- allLinks : newAllLinks
95
+ const isNewPage = this . props . location . pathname . includes ( 'new' )
96
+ const page = parseInt ( this . props . match . params . page , 10 )
97
+ const isFirstPage = page === 1
98
+
99
+ if ( isNewPage && isFirstPage ) {
100
+ const newAllLinks = [
101
+ subscriptionData . data . Link . node ,
102
+ ...previous . allLinks
103
+ ]
104
+ newAllLinks . pop ( )
105
+ const result = {
106
+ ...previous ,
107
+ allLinks : newAllLinks
108
+ }
109
+ return result
76
110
}
77
- return result
111
+ return previous
78
112
}
79
113
} )
80
114
}
@@ -115,49 +149,48 @@ class LinkList extends Component {
115
149
allLinks : newAllLinks
116
150
}
117
151
return result
118
- // return Immutable.setIn(previous, ['allItems', 0], link)
119
152
}
120
153
} )
121
154
}
122
155
123
156
_nextPage = ( ) => {
124
- const { history } = this . props
125
- const page = this . props . match . params . page
157
+ const page = parseInt ( this . props . match . params . page , 10 )
126
158
if ( page <= this . props . allLinksQuery . _allLinksMeta . count / LINKS_PER_PAGE ) {
127
- const nextPage = parseInt ( page ) + 1
128
- history . push ( `/${ nextPage } ` )
159
+ const nextPage = page + 1
160
+ this . props . history . push ( `/new /${ nextPage } ` )
129
161
}
130
162
}
131
163
132
164
_previousPage = ( ) => {
133
- const { history } = this . props
134
- const page = this . props . match . params . page
165
+ const page = parseInt ( this . props . match . params . page , 10 )
135
166
if ( page > 1 ) {
136
- const nextPage = parseInt ( page ) - 1
137
- history . push ( `/${ nextPage } ` )
167
+ const nextPage = page - 1
168
+ this . props . history . push ( `/new /${ nextPage } ` )
138
169
}
139
170
}
140
171
141
172
_updateCacheAfterVote = ( store , createVote , linkId ) => {
142
- const { pathname } = this . props . location
143
- const page = parseInt ( pathname . substring ( 1 , pathname . length ) )
144
- const skip = ( page - 1 ) * LINKS_PER_PAGE
145
- const first = LINKS_PER_PAGE
146
- const data = store . readQuery ( { query : ALL_LINKS_QUERY , variables : { first, skip } } )
173
+ const isNewPage = this . props . location . pathname . includes ( 'new' )
174
+ const page = parseInt ( this . props . match . params . page , 10 )
175
+ const skip = isNewPage ? ( page - 1 ) * LINKS_PER_PAGE : 0
176
+ const first = isNewPage ? LINKS_PER_PAGE : 100
177
+ const orderBy = isNewPage ? "createdAt_DESC" : null
178
+ const data = store . readQuery ( { query : ALL_LINKS_QUERY , variables : { first, skip, orderBy } } )
179
+
147
180
const votedLink = data . allLinks . find ( link => link . id === linkId )
148
181
votedLink . votes = createVote . link . votes
149
182
store . writeQuery ( { query : ALL_LINKS_QUERY , data } )
150
183
}
151
184
152
185
}
153
186
154
- const ALL_LINKS_QUERY = gql `
155
- query AllLinksQuery($first: Int, $skip: Int) {
156
- allLinks(first: $first, skip: $skip) {
187
+ export const ALL_LINKS_QUERY = gql `
188
+ query AllLinksQuery($first: Int, $skip: Int, $orderBy: LinkOrderBy ) {
189
+ allLinks(first: $first, skip: $skip, orderBy: $orderBy ) {
157
190
id
191
+ createdAt
158
192
url
159
193
description
160
- createdAt
161
194
postedBy {
162
195
id
163
196
name
@@ -175,14 +208,14 @@ const ALL_LINKS_QUERY = gql`
175
208
export default graphql ( ALL_LINKS_QUERY , {
176
209
name : 'allLinksQuery' ,
177
210
options : ( ownProps ) => {
178
- const { pathname } = ownProps . location
179
- const page = parseInt ( pathname . substring ( 1 , pathname . length ) )
211
+ const page = parseInt ( ownProps . match . params . page , 10 )
212
+ const isNewPage = ownProps . location . pathname . includes ( 'new' )
213
+ const skip = isNewPage ? ( page - 1 ) * LINKS_PER_PAGE : 0
214
+ const first = isNewPage ? LINKS_PER_PAGE : 100
215
+ const orderBy = isNewPage ? "createdAt_DESC" : null
180
216
return {
181
- variables : {
182
- skip : ( page - 1 ) * LINKS_PER_PAGE ,
183
- first : LINKS_PER_PAGE
184
- } ,
185
- fetchPolicy : 'network-only'
217
+ variables : { first, skip, orderBy } ,
218
+ fetchPolicy : 'cache-and-network'
186
219
}
187
220
}
188
221
} ) ( LinkList )
0 commit comments