191 lines
4.4 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';
2018-03-14 09:59:42 +01:00
/* eslint-disable jsx-a11y/anchor-is-valid */
2018-02-19 17:41:05 +01:00
class GlobalPagination extends React.Component {
getLastPageNumber = () => Math.ceil(this.props.count / this.props.params.limit);
handleDotsClick = (e) => e.preventDefault();
handlePreviousPageClick = (e) => {
e.preventDefault();
if (!this.isFirstPage()) {
const target = {
2018-02-20 19:20:31 +01:00
name: 'params.page',
value: this.props.params.page - 1,
2018-02-19 17:41:05 +01:00
};
this.props.onChangeParams({ target });
}
}
handleNextPageClick = (e) => {
e.preventDefault();
if (!this.isLastPage()) {
const target = {
2018-02-20 19:20:31 +01:00
name: 'params.page',
value: this.props.params.page + 1,
2018-02-19 17:41:05 +01:00
};
this.props.onChangeParams({ target });
}
}
handleFirstPageClick = (e) => {
e.preventDefault();
const target = {
2018-02-20 19:20:31 +01:00
name: 'params.page',
2018-02-19 17:41:05 +01:00
value: 1,
};
this.props.onChangeParams({ target });
}
handleLastPageClick = (e) => {
e.preventDefault();
const target = {
2018-02-20 19:20:31 +01:00
name: 'params.page',
2018-02-19 17:41:05 +01:00
value: this.getLastPageNumber(),
};
this.props.onChangeParams({ target });
}
2018-02-20 19:20:31 +01:00
isFirstPage = () => this.props.params.page === 1;
2018-02-19 17:41:05 +01:00
2018-02-20 19:20:31 +01:00
isLastPage = () => this.props.params.page === this.getLastPageNumber();
2018-02-19 17:41:05 +01:00
2018-02-20 19:20:31 +01:00
needAfterLinksDots = () => this.props.params.page < this.getLastPageNumber() - 1;
2018-02-19 17:41:05 +01:00
2018-02-20 19:20:31 +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({
2018-02-20 19:20:31 +01:00
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({
2018-02-20 19:20:31 +01:00
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) {
linksOptions.push({
2018-02-20 19:20:31 +01:00
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
return (
map(linksOptions, (linksOption, key) => (
<li
className={`${linksOption.isActive && styles.navLiActive}`}
key={key}
>
<a href="" disabled={linksOption.isActive} onClick={linksOption.handleClick}>
{linksOption.value}
</a>
</li>
))
);
}
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}>
<ul className={styles.navUl}>
{this.renderLinks()}
</ul>
</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: {
2018-02-20 19:20:31 +01:00
page: 1,
2018-02-19 17:41:05 +01:00
limit: 10,
},
};
GlobalPagination.propTypes = {
count: PropTypes.oneOfType([
PropTypes.number,
PropTypes.bool,
]),
onChangeParams: PropTypes.func,
params: PropTypes.shape({
2018-02-20 19:20:31 +01:00
page: PropTypes.oneOfType([
2018-02-19 17:41:05 +01:00
PropTypes.string,
PropTypes.number,
]),
limit: PropTypes.number,
}),
};
export default GlobalPagination;