summaryrefslogtreecommitdiffstats
path: root/frontend/components/Pagination.js
blob: 2cb5080226e2ff04eda4f3fa44dbb69426b0b445 (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
40
41
42
43
44
import React, { Component } from 'react';
import { graphql, gql, compose } from 'react-apollo';
import { ALL_ITEMS_QUERY } from '../queries';
import withData from '../lib/withData';
import { Link } from '../routes';
import { itemEnhancer } from '../enhancers/enhancers';

const Pagination = props => {
  const { loading, error } = props.itemsQuery;

  if (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;