1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
// @flow
import CircularProgress from '@material-ui/core/CircularProgress'
import { withStyles } from '@material-ui/core/styles'
import Table from '@material-ui/core/Table/Table'
import TableBody from '@material-ui/core/TableBody/TableBody'
import TablePagination from '@material-ui/core/TablePagination/TablePagination'
import gql from 'graphql-tag'
import React from 'react'
import { Query } from 'react-apollo'
import BugSummary from './BugSummary'
const QUERY = gql`
query($first: Int = 10, $last: Int, $after: String, $before: String) {
defaultRepository {
bugs: allBugs(first: $first, last: $last, after: $after, before: $before) {
totalCount
edges {
cursor
node {
...BugSummary
}
}
pageInfo{
hasNextPage
hasPreviousPage
startCursor
endCursor
}
}
}
}
${BugSummary.fragment}
`
const styles = theme => ({
main: {
maxWidth: 600,
margin: 'auto',
marginTop: theme.spacing.unit * 4
}
})
const List = withStyles(styles)(class List extends React.Component {
props: {
bugs: Array,
fetchMore: (any) => any,
classes: any,
}
state = {
page: 0,
rowsPerPage: 10,
lastQuery: {}
}
handleChangePage = (event, page) => {
const {bugs, fetchMore} = this.props
const {rowsPerPage} = this.state
const pageInfo = bugs.pageInfo
if (page === this.state.page + 1) {
if (!pageInfo.hasNextPage) {
return
}
const variables = {
after: pageInfo.endCursor,
first: rowsPerPage
}
fetchMore({
variables,
updateQuery: this.updateQuery,
})
this.setState({page, lastQuery: variables})
return
}
if (page === this.state.page - 1) {
if (!pageInfo.hasPreviousPage) {
return
}
const variables = {
before: pageInfo.startCursor,
last: rowsPerPage
}
fetchMore({
variables,
updateQuery: this.updateQuery,
})
this.setState({page, lastQuery: variables})
return
}
throw 'non neighbour page pagination is not supported'
}
handleChangeRowsPerPage = event => {
const {fetchMore} = this.props
const {lastQuery} = this.state
const rowsPerPage = event.target.value
const variables = lastQuery
if (lastQuery.first) {
variables.first = rowsPerPage
} else if (lastQuery.last) {
variables.last = rowsPerPage
} else {
variables.first = rowsPerPage
}
fetchMore({
variables,
updateQuery: this.updateQuery,
})
this.setState({rowsPerPage, lastQuery: variables})
}
updateQuery = (previousResult, {fetchMoreResult}) => {
console.log(fetchMoreResult)
return fetchMoreResult ? fetchMoreResult : previousResult
}
render() {
const {classes, bugs} = this.props
const {page, rowsPerPage} = this.state
return (
<main className={classes.main}>
<Table className={classes.table}>
<TableBody>
{bugs.edges.map(({cursor, node}) => (
<BugSummary bug={node} key={cursor}/>
))}
</TableBody>
</Table>
<TablePagination
component="div"
count={bugs.totalCount}
rowsPerPage={rowsPerPage}
page={page}
backIconButtonProps={{
'aria-label': 'Previous Page'
}}
nextIconButtonProps={{
'aria-label': 'Next Page'
}}
onChangePage={this.handleChangePage}
onChangeRowsPerPage={this.handleChangeRowsPerPage}
/>
</main>
)
}
})
const ListPage = () => (
<Query query={QUERY}>
{({loading, error, data, fetchMore}) => {
if (loading) return <CircularProgress/>
if (error) return <p>Error.</p>
return <List bugs={data.defaultRepository.bugs} fetchMore={fetchMore}/>
}}
</Query>
)
export default ListPage
|