import React, { Component } from 'react'; import PropTypes from 'prop-types'; import className from 'classnames'; import { navigate } from '@gatsbyjs/reach-router'; import { gettext } from '../utils/constants'; import { DropdownMenu, Dropdown, DropdownToggle, DropdownItem } from 'reactstrap'; import '../css/pagination.css'; const propTypes = { currentPage: PropTypes.number.isRequired, gotoPreviousPage: PropTypes.func.isRequired, gotoNextPage: PropTypes.func.isRequired, hasNextPage: PropTypes.bool.isRequired, resetPerPage: PropTypes.func.isRequired, curPerPage: PropTypes.number.isRequired, noURLUpdate: PropTypes.bool }; const PER_PAGES = [25, 50, 100]; class Paginator extends Component { constructor(props) { super(props); this.state = { isMenuShow: false }; } resetPerPage = (perPage) => { this.updateURL(1, perPage); this.props.resetPerPage(perPage); }; goToPrevious = () => { const { currentPage, curPerPage } = this.props; this.updateURL(currentPage - 1, curPerPage); this.props.gotoPreviousPage(); }; goToNext = () => { const { currentPage, curPerPage } = this.props; this.updateURL(currentPage + 1, curPerPage); this.props.gotoNextPage(); }; updateURL = (page, perPage) => { const { noURLUpdate = false } = this.props; if (noURLUpdate) { return; } let url = new URL(location.href); let searchParams = new URLSearchParams(url.search); searchParams.set('page', page); searchParams.set('per_page', perPage); url.search = searchParams.toString(); navigate(url.toString()); }; getPerPageText = (perPage) => { return gettext('{number_placeholder} / Page').replace('{number_placeholder}', perPage); }; toggleOperationMenu = (e) => { e.stopPropagation(); this.setState({ isMenuShow: !this.state.isMenuShow }); }; renderDropdownItem = (curPerPage, perPage) => { return ( {this.resetPerPage(perPage);}} key={perPage}> {curPerPage === perPage && } {this.getPerPageText(perPage)} ); }; render() { const { curPerPage, currentPage } = this.props; return (
{currentPage} {this.getPerPageText(curPerPage)} {PER_PAGES.map(perPage => { return this.renderDropdownItem(curPerPage, perPage); })}
); } } Paginator.propTypes = propTypes; export default Paginator;