blob: 3c28de27d5a9c33157b4c70fbeab6854b5e6219d (
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
|
import React from 'react';
import { Query } from 'react-apollo';
import styled from 'styled-components';
import PropTypes from 'prop-types';
import Pagination from './Pagination';
import Item from './Item';
import { perPage } from '../config';
import { ALL_ITEMS_QUERY } from '../queries/index';
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,
};
static getDerivedStateFromProps(nextProps, state) {
console.log(nextProps);
return { refetch: state.page !== nextProps.page };
}
state = {
refetch: false,
};
render() {
const fetchPolicy = this.state.refetch ? 'network-only' : 'cache-first';
console.log(this.state.refetch, this.props.page);
console.log(fetchPolicy);
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={fetchPolicy}
>
{({ data, error, loading }) => {
if (loading) return <div>Loading</div>;
if (error) return <div>Error</div>;
return (
<Items key={this.props.page}>
{data.items.map(item => <Item key={item.id} item={item} />)}
</Items>
);
}}
</Query>
<Pagination page={this.props.page} />
</Center>
);
}
}
export default ItemList;
|