summaryrefslogtreecommitdiffstats
path: root/frontend/components/Pagination.js
blob: abc22d0be7ba156217beffbea7b780948a26b427 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import React from 'react';
import { compose } from 'react-apollo';
import styled from 'styled-components';
import Link from 'next/link';
import PropTypes from 'prop-types';
import { itemEnhancer } from '../enhancers/enhancers';
import { perPage } from '../config';

const PaginationStyles = styled.div`
  text-align: center;
  display: inline-grid;
  grid-template-columns: repeat(4, auto);
  align-items: stretch;
  justify-content: center;
  align-content: center;
  margin: 2rem 0;
  border: 1px solid ${props => props.theme.lightgrey};
  border-radius: 10px;
  & > * {
    margin: 0;
    padding: 15px 30px;
    border-right: 1px solid ${props => props.theme.lightgrey};
    &:last-child {
      border-right: 0;
    }
  }
  a[aria-disabled='true'] {
    color: grey;
    pointer-events: none;
  }
`;

const Pagination = props => {
  if (props.loading) return <p>Loading...</p>;
  const { aggregate } = props.itemsQuery.itemsConnection;
  const { page } = props;
  const pages = Math.ceil(aggregate.count / perPage);

  return (
    <PaginationStyles>
      <Link
        prefetch
        href={{
          pathname: 'items',
          query: { page: page - 1 },
        }}
      >
        <a className="prev" aria-disabled={page <= 1}>
          Prev
        </a>
      </Link>
      <p>
        Page <strong>{page} </strong> of <strong className="totalPages">{pages}</strong>
      </p>
      <p>
        <strong>{aggregate.count}</strong> Items Total
      </p>
      <Link
        prefetch
        href={{
          pathname: 'items',
          query: { page: page + 1 },
        }}
      >
        <a className="next" aria-disabled={page >= pages}>
          Next 
        </a>
      </Link>
    </PaginationStyles>
  );
};

Pagination.propTypes = {
  itemsQuery: PropTypes.shape({
    itemsConnection: PropTypes.shape({
      aggregate: PropTypes.shape({
        count: PropTypes.number.isRequired,
      }),
    }),
  }).isRequired,
  page: PropTypes.number.isRequired,
};

export default compose(itemEnhancer)(Pagination);
export { Pagination };