summaryrefslogtreecommitdiffstats
path: root/frontend/components/Pagination.js
blob: d00778bdfd9c47e6f3fd22b079cd953c348db171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import React from 'react';
import { compose } from 'react-apollo';
import { Link } from '../routes';
import { itemEnhancer } from '../enhancers/enhancers';

const Pagination = props => {
  if (props.loading) return <p>Loading Item...</p>;
  const { aggregate, pageInfo } = props.itemsQuery.itemsConnection;

  const { page } = props;
  const pages = Math.floor(aggregate.count / 3);
  return (
    <div>
      <p>
        Page <strong>{page} </strong>
        of
        <strong>{pages} </strong>
        -
        <strong>{aggregate.count} </strong>
        total
      </p>
      {pageInfo.hasPreviousPage ? (
        <Link prefetch route="items" params={{ page: page - 1 }}>
          <a>←Prev</a>
        </Link>
      ) : null}

      {pageInfo.hasNextPage ? (
        <Link prefetch route="items" params={{ page: page + 1 }}>
          <a>Next →</a>
        </Link>
      ) : null}
    </div>
  );
};

const ComponentWithMutations = compose(itemEnhancer)(Pagination);

export default ComponentWithMutations;