-
-
Notifications
You must be signed in to change notification settings - Fork 282
/
pager.js
213 lines (191 loc) · 6.59 KB
/
pager.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright (C) 2019 Prabal Singh
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
import * as bootstrap from 'react-bootstrap';
import PropTypes from 'prop-types';
import React from 'react';
import {getNextEnabledAndResultsArray} from '../../../../common/helpers/utils';
import {isFunction} from 'lodash';
import request from 'superagent';
const {Button, ButtonGroup, Col, Dropdown, DropdownButton, Row} = bootstrap;
class PagerElement extends React.Component {
constructor(props) {
super(props);
this.state = {
from: this.props.from,
nextEnabled: this.props.nextEnabled,
results: this.props.results,
size: this.props.size
};
this.handleClickPrevious = this.handleClickPrevious.bind(this);
this.handleClickNext = this.handleClickNext.bind(this);
this.handleResultsPerPageChange = this.handleResultsPerPageChange.bind(this);
this.triggerSearch = this.triggerSearch.bind(this);
}
componentDidMount() {
window.addEventListener('popstate', this.handleURLChange);
// Set initial size and from parameter in browser address bar
const searchParams = new URLSearchParams(window.location.search);
searchParams.set('size', this.state.size);
searchParams.set('from', this.state.from);
const newSearchParamsString = `?${searchParams.toString()}`;
if (newSearchParamsString !== window.location.search) {
window.history.replaceState(null, '', newSearchParamsString);
}
}
componentDidUpdate(prevProps) {
if (prevProps.querySearchParams !== this.props.querySearchParams) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState({from: 0}, this.triggerSearch);
}
}
componentWillUnmount() {
window.removeEventListener('popstate', this.handleURLChange);
}
handleURLChange = () => {
const searchParams = new URLSearchParams(window.location.search);
const {from, size} = this.state;
let newFrom;
let newSize;
if (searchParams.has('from')) {
newFrom = Number(searchParams.get('from'));
}
if (searchParams.has('size')) {
newSize = Number(searchParams.get('size'));
}
if (isFunction(this.props.searchParamsChangeCallback)) {
this.props.searchParamsChangeCallback(searchParams);
}
if (newFrom === from && newSize === size) {
return;
}
this.triggerSearch(newFrom, newSize);
};
triggerSearch(newFrom = this.state.from, newSize = this.state.size) {
const searchParams = new URLSearchParams(this.props.querySearchParams);
searchParams.set('size', newSize);
searchParams.set('from', newFrom);
const newSearchParamsString = `?${searchParams.toString()}`;
// Don't push a new entry when user navigates with browser prev/next buttons,
// which already sets the url search params accordingly.
// Otherwise we rewrite history from this point onwards and prevent from going forward again.
if (newSearchParamsString !== window.location.search) {
window.history.pushState(null, '', newSearchParamsString);
}
// fetch 1 more result than size to check nextEnabled
searchParams.set('size', newSize + 1);
request.get(`${this.props.paginationUrl}?${searchParams.toString()}`)
.then((res) => JSON.parse(res.text))
.then((data) => {
let results;
if (Array.isArray(data)) {
results = data;
}
else {
// Some endpoints (for example /search/search) return an object of type {result: array, total: number}
({results} = data);
}
const {newResultsArray, nextEnabled} = getNextEnabledAndResultsArray(results, newSize);
this.setState({
from: newFrom,
nextEnabled,
results: newResultsArray,
size: newSize
});
this.props.searchResultsCallback(newResultsArray);
});
}
handleClickPrevious() {
this.triggerSearch(Math.max(this.state.from - this.state.size, 0));
}
handleClickNext() {
this.triggerSearch(this.state.from + this.state.size);
}
handleResultsPerPageChange(newSize) {
this.triggerSearch(this.state.from, parseInt(newSize, 10));
}
render() {
return (
this.state.results && this.state.results.length ?
<div id="PagerElement">
<hr className="thin"/>
<Row className="pb-4">
<Col lg={4}>
<Button
className="round-button"
disabled={this.state.from <= 0}
variant="outline-primary"
onClick={this.handleClickPrevious}
>
← Previous Page
</Button>
</Col>
<Col className="text-center" lg={4}>
<ButtonGroup>
<Button disabled variant="secondary">Results {this.state.from + 1} —
{this.state.results.length < this.state.size ?
this.state.from + this.state.results.length :
this.state.from + this.state.size
}
</Button>
<DropdownButton
drop="up" id="bg-nested-dropdown" title={`${this.state.size} per page`}
variant="info"
onSelect={this.handleResultsPerPageChange}
>
<Dropdown.Item eventKey="10">10 per page</Dropdown.Item>
<Dropdown.Item eventKey="20">20 per page</Dropdown.Item>
<Dropdown.Item eventKey="35">35 per page</Dropdown.Item>
<Dropdown.Item eventKey="50">50 per page</Dropdown.Item>
<Dropdown.Item eventKey="100">100 per page</Dropdown.Item>
</DropdownButton>
</ButtonGroup>
</Col>
<Col className="text-right" lg={4}>
<Button
className="round-button"
disabled={!this.state.nextEnabled}
variant="outline-primary"
onClick={this.handleClickNext}
>
Next Page →
</Button>
</Col>
</Row>
</div> :
null
);
}
}
PagerElement.propTypes = {
from: PropTypes.number,
nextEnabled: PropTypes.bool.isRequired,
paginationUrl: PropTypes.string.isRequired,
querySearchParams: PropTypes.string,
results: PropTypes.array,
searchParamsChangeCallback: PropTypes.func,
searchResultsCallback: PropTypes.func.isRequired,
size: PropTypes.number
};
PagerElement.defaultProps = {
from: 0,
querySearchParams: '',
results: [],
searchParamsChangeCallback: null,
size: 20
};
export default PagerElement;