display correct pagination whatever the page

This commit is contained in:
Jonathan Rainville 2018-10-24 10:35:27 -04:00
parent 09e21f3386
commit 6c92e7b24d
3 changed files with 42 additions and 19 deletions

View File

@ -6,7 +6,7 @@ import Pages from './Pagination';
import CardTitleIdenticon from './CardTitleIdenticon'; import CardTitleIdenticon from './CardTitleIdenticon';
const Blocks = ({blocks, changePage, currentPage}) => ( const Blocks = ({blocks, changePage, currentPage, numberOfPages}) => (
<Row> <Row>
<Col> <Col>
<Card> <Card>
@ -37,7 +37,7 @@ const Blocks = ({blocks, changePage, currentPage}) => (
</Row> </Row>
</div> </div>
))} ))}
<Pages changePage={changePage} currentPage={currentPage} numberOfPages={5}/> <Pages changePage={changePage} currentPage={currentPage} numberOfPages={numberOfPages}/>
</CardBody> </CardBody>
</Card> </Card>
</Col> </Col>
@ -47,7 +47,8 @@ const Blocks = ({blocks, changePage, currentPage}) => (
Blocks.propTypes = { Blocks.propTypes = {
blocks: PropTypes.arrayOf(PropTypes.object), blocks: PropTypes.arrayOf(PropTypes.object),
changePage: PropTypes.func, changePage: PropTypes.func,
currentPage: PropTypes.number currentPage: PropTypes.number,
numberOfPages: PropTypes.number
}; };
export default Blocks; export default Blocks;

View File

@ -2,27 +2,43 @@ import React from 'react';
import {Pagination, PaginationItem, PaginationLink} from 'reactstrap'; import {Pagination, PaginationItem, PaginationLink} from 'reactstrap';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
const NB_PAGES_MAX = 14;
const Pages = ({currentPage, numberOfPages, changePage}) => { const Pages = ({currentPage, numberOfPages, changePage}) => {
const paginationItems = []; let i = currentPage - NB_PAGES_MAX / 2;
for (let i = 1; i <= numberOfPages; i++) { if (i < 1) {
paginationItems.push(<PaginationItem active={currentPage === i} key={'page-' + i}> i = 1;
<PaginationLink href="#" onClick={(e) => { }
e.preventDefault(); let max = i + NB_PAGES_MAX;
changePage(i); if (max > numberOfPages) {
}}> max = numberOfPages;
{i} }
</PaginationLink> const pageNumbers = [];
</PaginationItem>); for (i; i <= max; i++) {
pageNumbers.push(i);
} }
return ( return (
<Pagination aria-label="Page navigation example"> <Pagination aria-label="Page navigation example">
<PaginationItem> <PaginationItem disabled={currentPage <= 1}>
<PaginationLink previous href="#"/> <PaginationLink previous href="#" onClick={(e) => {
e.preventDefault();
changePage(currentPage - 1);
}}/>
</PaginationItem> </PaginationItem>
{paginationItems} {pageNumbers.map(number => (<PaginationItem active={currentPage === number} key={'page-' + number}>
<PaginationItem> <PaginationLink href="#" onClick={(e) => {
<PaginationLink next href="#"/> e.preventDefault();
changePage(number);
}}>
{number}
</PaginationLink>
</PaginationItem>))}
<PaginationItem disabled={currentPage >= numberOfPages}>
<PaginationLink next href="#" onClick={(e) => {
e.preventDefault();
changePage(currentPage + 1);
}}/>
</PaginationItem> </PaginationItem>
</Pagination> </Pagination>
); );

View File

@ -7,6 +7,8 @@ import Blocks from '../components/Blocks';
import DataWrapper from "../components/DataWrapper"; import DataWrapper from "../components/DataWrapper";
import {getBlocks} from "../reducers/selectors"; import {getBlocks} from "../reducers/selectors";
const MAX_BLOCKS = 10; // TODO use same constant as API
class BlocksContainer extends Component { class BlocksContainer extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
@ -27,6 +29,10 @@ class BlocksContainer extends Component {
this.props.fetchBlocks(this.loadMoreFrom()); this.props.fetchBlocks(this.loadMoreFrom());
} }
getNumberOfPages() {
return Math.ceil(this.loadMoreFrom() / MAX_BLOCKS);
}
loadMoreFrom() { loadMoreFrom() {
let blocks = this.props.blocks; let blocks = this.props.blocks;
if (blocks.length === 0) { if (blocks.length === 0) {
@ -43,7 +49,7 @@ class BlocksContainer extends Component {
return ( return (
<React.Fragment> <React.Fragment>
<DataWrapper shouldRender={this.props.blocks.length > 0} {...this.props} render={({blocks}) => ( <DataWrapper shouldRender={this.props.blocks.length > 0} {...this.props} render={({blocks}) => (
<Blocks blocks={blocks} showLoadMore={(this.loadMoreFrom() >= 0)} <Blocks blocks={blocks} numberOfPages={this.getNumberOfPages()}
changePage={(newPage) => this.changePage(newPage)} currentPage={this.state.currentPage} /> changePage={(newPage) => this.changePage(newPage)} currentPage={this.state.currentPage} />
)} /> )} />
</React.Fragment> </React.Fragment>