Completed
Pull Request — master (#158)
by Alejandro
07:36
created

src/common/SimplePaginator.js   A

Complexity

Total Complexity 4
Complexity/F 0

Size

Lines of Code 66
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 54
mnd 4
bc 4
fnc 0
dl 0
loc 66
ccs 19
cts 19
cp 1
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React from 'react';
2
import PropTypes from 'prop-types';
3
import { Pagination, PaginationItem, PaginationLink } from 'reactstrap';
4
import { range, max, min } from 'ramda';
5
import './SimplePaginator.scss';
6
7 3
const propTypes = {
8
  pagesCount: PropTypes.number.isRequired,
9
  currentPage: PropTypes.number.isRequired,
10
  setCurrentPage: PropTypes.func.isRequired,
11
};
12
13 3
export const ellipsis = '...';
14
15 3
const pagination = (currentPage, pageCount) => {
16 3
  const delta = 2;
17 3
  const pages = range(
18
    max(delta, currentPage - delta),
19
    min(pageCount - 1, currentPage + delta) + 1
20
  );
21
22 3
  if (currentPage - delta > delta) {
23 2
    pages.unshift(ellipsis);
24
  }
25 3
  if (currentPage + delta < pageCount - 1) {
26 2
    pages.push(ellipsis);
27
  }
28
29 3
  pages.unshift(1);
30 3
  pages.push(pageCount);
31
32 3
  return pages;
33
};
34
35 3
const SimplePaginator = ({ pagesCount, currentPage, setCurrentPage }) => {
36 7
  if (pagesCount < 2) {
37 4
    return null;
38
  }
39
40 28
  const onClick = (page) => () => setCurrentPage(page);
41
42 3
  return (
43
    <Pagination listClassName="flex-wrap justify-content-center mb-0 simple-paginator">
44
      <PaginationItem disabled={currentPage <= 1}>
45
        <PaginationLink previous tag="span" onClick={onClick(currentPage - 1)} />
46
      </PaginationItem>
47
      {pagination(currentPage, pagesCount).map((page, index) => (
48 22
        <PaginationItem
49
          key={page !== ellipsis ? page : `${page}_${index}`}
50
          active={page === currentPage}
51
          disabled={page === ellipsis}
52
        >
53
          <PaginationLink tag="span" onClick={onClick(page)}>{page}</PaginationLink>
54
        </PaginationItem>
55
      ))}
56
      <PaginationItem disabled={currentPage >= pagesCount}>
57
        <PaginationLink next tag="span" onClick={onClick(currentPage + 1)} />
58
      </PaginationItem>
59
    </Pagination>
60
  );
61
};
62
63 3
SimplePaginator.propTypes = propTypes;
64
65
export default SimplePaginator;
66