Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to using react-paginate so we dont need to manage that code ourselves #588

Merged
merged 2 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"postcss-url": "^9.0.0",
"react-chartjs-2": "^2.11.1",
"react-helmet": "^6.1.0",
"react-paginate": "^7.1.0",
"react-radio-group": "^3.0.3",
"react-timeago": "^5.2.0",
"reactstrap": "^8.9.0"
Expand Down
66 changes: 24 additions & 42 deletions src/components/Pages.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PropTypes from 'prop-types';
import React from 'react';
import {Pagination, PaginationItem, PaginationLink} from 'reactstrap';
import ReactPaginate from 'react-paginate';

export default class Pages extends React.PureComponent {

Expand All @@ -11,51 +11,33 @@ export default class Pages extends React.PureComponent {
updatePage: PropTypes.func.isRequired
};

pageRange() {
const currentPosition = Math.ceil(this.props.pagesToDisplay / 2);
const start = this.props.current < currentPosition ? 1
: this.props.current - currentPosition + 1;
const len = this.props.pages < start + this.props.pagesToDisplay - 1
? this.props.pages - start + 1 : this.props.pagesToDisplay;
return Array
.apply(null, Array(len))
.map((u, i) => start + i);
}

render() {
const {updatePage, current, pages} = this.props;
const updatePageWrapper = (num) => {
return (e) => {
e.preventDefault();
updatePage(num);
};
};
const handlePageClick = (data) => updatePage(data.selected);

return (
<Pagination aria-label="Page navigation example">
{current !== 1 && <PaginationItem>
<PaginationLink first onClick={updatePageWrapper(1)} />
</PaginationItem>}
{current > 1 && <PaginationItem>
<PaginationLink previous onClick={updatePageWrapper(current-1)} />
</PaginationItem>}
{this.pageRange().map((page) => {
const isCurrent = current == page;
return (
<PaginationItem key={page} active={isCurrent}>
<PaginationLink onClick={updatePageWrapper(page)}>
{page}
</PaginationLink>
</PaginationItem>
);
})}
{current !== pages && <PaginationItem>
<PaginationLink next onClick={updatePageWrapper(current + 1)} />
</PaginationItem>}
{current !== pages && <PaginationItem>
<PaginationLink last onClick={updatePageWrapper(pages)} />
</PaginationItem>}
</Pagination>
<ReactPaginate
pageCount={pages}
marginPagesDisplayed={2}
pageRangeDisplayed={this.props.pagesToDisplay}
onPageChange={handlePageClick}
forcePage={current}

breakLabel={'...'}
breakClassName={'page-item disabled'}
breakLinkClassName={'page-link'}

pageClassName={'page-item'}
pageLinkClassName={'page-link'}
nextClassName={'page-item'}
nextLinkClassName={'page-link'}
previousClassName={'page-item'}
previousLinkClassName={'page-link'}

containerClassName={'pagination'}
subContainerClassName={'pages pagination'}
activeClassName={'active'}
/>
);
}
}
2 changes: 1 addition & 1 deletion src/templates/search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const doSearch = (data, setResults) => {
index.search(query, {page: page-1, filters: filters.join(' AND ')}).then(({nbHits, page, nbPages, hits, hitsPerPage}) => {
setResults({
total: nbHits,
pages: nbPages,
pages: nbPages + 1,
page: page + 1,
limit: hitsPerPage,
plugins: hits
Expand Down
1 change: 1 addition & 0 deletions src/utils/algolia-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function pluginQueries() {
transformer: ({data}) => data.pages.edges.map(pageToAlgoliaRecord),
indexName: 'Plugins',
settings: {
paginationLimitedTo: 2000, // they recommend 1000, to keep speed up and prevent people from scraping, but both are fine to us
attributesToSnippet: ['content:20'],
ranking: [
'typo',
Expand Down