summaryrefslogtreecommitdiffstats
path: root/frontend/components/Items.js
blob: 394f16d4f6b32359fc323107752e151eac2a5337 (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
import React from 'react';
import { Query } from 'react-apollo';
import styled from 'styled-components';
import PropTypes from 'prop-types';
import gql from 'graphql-tag';
import Pagination from './Pagination';
import Item from './Item';
import LoadingItem from './LoadingItem';
import { perPage } from '../config';

const ALL_ITEMS_QUERY = gql`
  query ALL_ITEMS_QUERY($skip: Int = 0, $first: Int = 4) {
    items(orderBy: createdAt_DESC, first: $first, skip: $skip) {
      __typename
      id
      title
      price
      description
      image
      largeImage
    }
  }
`;

const Items = styled.div`
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 60px;
  max-width: ${props => props.theme.maxWidth};
  margin: 0 auto;
`;

const Center = styled.div`
  text-align: center;
`;

class ItemList extends React.Component {
  static propTypes = {
    page: PropTypes.number.isRequired,
  };
  render() {
    return (
      <Center key={this.props.page}>
        <Pagination page={this.props.page} />
        <Query
          query={ALL_ITEMS_QUERY}
          variables={{
            skip: this.props.page * perPage - perPage,
            first: perPage,
          }}
          // fetchPolicy="network-only"
        >
          {({ data, error, loading }) => {
            if (loading) {
              return (
                <Items>
                  {Array.from({ length: 4 })
                    .map((x, id) => ({ id }))
                    .map(x => <LoadingItem key={x.id} />)}
                </Items>
              );
            }
            if (error) return <div>Error</div>;
            return <Items>{data.items.map(item => <Item key={item.id} item={item} />)}</Items>;
          }}
        </Query>
        <Pagination page={this.props.page} />
      </Center>
    );
  }
}

export default ItemList;
export { ALL_ITEMS_QUERY };