blob: d0e39e9eab4f1dd674418b67aef90fb4db86385c (
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
|
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: repeat(auto-fit, minmax(300px, 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,
};
componentDidUpdate(lastProps) {
if (lastProps.page === this.props.page) return;
// update the query
console.log('This fires, and should update the query');
}
render() {
return (
<Center key={this.props.page}>
<Pagination page={this.props.page} />
<Query
query={ALL_ITEMS_QUERY}
fetchPolicy="cache-and-network"
ssr={false}
variables={{
skip: this.props.page * perPage - perPage,
first: perPage,
}}
>
{({ data, error, loading, variables, refetch }) => {
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} />)}
<button
onClick={() => {
console.log('Refetching..');
console.log(refetch);
refetch();
}}
>
refetch
</button>
</Items>
);
}}
</Query>
<Pagination page={this.props.page} />
</Center>
);
}
}
export default ItemList;
|