183 lines
4.3 KiB
JavaScript
Raw Normal View History

2018-02-19 17:41:05 +01:00
/**
*
* GlobalPagination
*
*/
import React from 'react';
import { map } from 'lodash';
import PropTypes from 'prop-types';
import styles from './styles.scss';
class GlobalPagination extends React.Component {
2019-09-27 16:26:09 +02:00
getLastPageNumber = () =>
Math.ceil(this.props.count / this.props.params._limit) || 1;
2018-02-19 17:41:05 +01:00
2019-09-27 16:26:09 +02:00
handleDotsClick = e => e.preventDefault();
2018-02-19 17:41:05 +01:00
2019-09-27 16:26:09 +02:00
handlePreviousPageClick = e => {
2018-02-19 17:41:05 +01:00
e.preventDefault();
if (!this.isFirstPage()) {
const target = {
name: 'params._page',
value: this.props.params._page - 1,
2018-02-19 17:41:05 +01:00
};
this.props.onChangeParams({ target });
}
2019-09-27 16:26:09 +02:00
};
2018-02-19 17:41:05 +01:00
2019-09-27 16:26:09 +02:00
handleNextPageClick = e => {
2018-02-19 17:41:05 +01:00
e.preventDefault();
if (!this.isLastPage()) {
const target = {
name: 'params._page',
value: this.props.params._page + 1,
2018-02-19 17:41:05 +01:00
};
this.props.onChangeParams({ target });
}
2019-09-27 16:26:09 +02:00
};
2018-02-19 17:41:05 +01:00
2019-09-27 16:26:09 +02:00
handleFirstPageClick = e => {
2018-02-19 17:41:05 +01:00
e.preventDefault();
const target = {
name: 'params._page',
2018-02-19 17:41:05 +01:00
value: 1,
};
this.props.onChangeParams({ target });
2019-09-27 16:26:09 +02:00
};
2018-02-19 17:41:05 +01:00
2019-09-27 16:26:09 +02:00
handleLastPageClick = e => {
2018-02-19 17:41:05 +01:00
e.preventDefault();
const target = {
name: 'params._page',
2018-02-19 17:41:05 +01:00
value: this.getLastPageNumber(),
};
this.props.onChangeParams({ target });
2019-09-27 16:26:09 +02:00
};
2018-02-19 17:41:05 +01:00
isFirstPage = () => this.props.params._page === 1;
2018-02-19 17:41:05 +01:00
isLastPage = () => this.props.params._page === this.getLastPageNumber();
2018-02-19 17:41:05 +01:00
2019-09-27 16:26:09 +02:00
needAfterLinksDots = () =>
this.props.params._page < this.getLastPageNumber() - 1;
2018-02-19 17:41:05 +01:00
needPreviousLinksDots = () => this.props.params._page > 3;
2018-02-19 17:41:05 +01:00
renderLinks = () => {
// Init variables
const linksOptions = [];
// Add active page link
linksOptions.push({
value: this.props.params._page,
2018-02-19 17:41:05 +01:00
isActive: true,
handleClick: e => e.preventDefault(),
});
// Add previous page link
if (!this.isFirstPage()) {
linksOptions.unshift({
value: this.props.params._page - 1,
2018-02-19 17:41:05 +01:00
isActive: false,
handleClick: this.handlePreviousPageClick,
});
}
// Add next page link
if (!this.isLastPage() && this.props.count > this.props.params._limit) {
2018-02-19 17:41:05 +01:00
linksOptions.push({
value: this.props.params._page + 1,
2018-02-19 17:41:05 +01:00
isActive: false,
handleClick: this.handleNextPageClick,
});
}
if (this.needPreviousLinksDots()) {
linksOptions.unshift({
value: 1,
isActive: false,
handleClick: this.handleFirstPageClick,
});
}
if (this.needAfterLinksDots()) {
linksOptions.push({
value: this.getLastPageNumber(),
isActive: false,
handleClick: this.handleLastPageClick,
});
}
// Generate links
2019-09-27 16:26:09 +02:00
return map(linksOptions, (linksOption, key) => (
<li className={`${linksOption.isActive && styles.navLiActive}`} key={key}>
<a
href=""
disabled={linksOption.isActive}
onClick={linksOption.handleClick}
2018-02-19 17:41:05 +01:00
>
2019-09-27 16:26:09 +02:00
{linksOption.value}
</a>
</li>
));
};
2018-02-19 17:41:05 +01:00
render() {
return (
<div className={styles.pagination}>
<div>
<a
href=""
className={`
${styles.paginationNavigator}
${this.isFirstPage() && styles.paginationNavigatorDisabled}
`}
onClick={this.handlePreviousPageClick}
disabled={this.isFirstPage()}
>
<i className="fa fa-angle-left" aria-hidden="true"></i>
</a>
<nav className={styles.nav}>
2019-09-27 16:26:09 +02:00
<ul className={styles.navUl}>{this.renderLinks()}</ul>
2018-02-19 17:41:05 +01:00
</nav>
<a
href=""
className={`
${styles.paginationNavigator}
${this.isLastPage() && styles.paginationNavigatorDisabled}
`}
onClick={this.handleNextPageClick}
disabled={this.isLastPage()}
>
<i className="fa fa-angle-right" aria-hidden="true"></i>
</a>
</div>
</div>
);
}
}
GlobalPagination.defaultProps = {
2018-03-14 09:59:42 +01:00
count: 0,
2018-02-19 17:41:05 +01:00
onChangeParams: () => {},
params: {
_page: 1,
_limit: 10,
2018-02-19 17:41:05 +01:00
},
};
GlobalPagination.propTypes = {
2019-09-27 16:26:09 +02:00
count: PropTypes.oneOfType([PropTypes.number, PropTypes.bool]),
2018-02-19 17:41:05 +01:00
onChangeParams: PropTypes.func,
params: PropTypes.shape({
2019-09-27 16:26:09 +02:00
_page: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
_limit: PropTypes.number,
2018-02-19 17:41:05 +01:00
}),
};
export default GlobalPagination;