blob: 620659209031642635a437f87bbab9d6b1c231af (
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
|
import { Component } from 'react';
import { withApollo, graphql, compose } from 'react-apollo';
import styled from 'styled-components';
import Pagination from './Pagination';
import Item from './Item';
import { itemEnhancer } from '../enhancers/enhancers';
import { ALL_ITEMS_QUERY, DELETE_ITEM_MUTATION } from '../queries';
const Title = styled.h1`
font-size: 10px;
`;
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;
`;
class ItemList extends Component {
componentDidMount() {
this.prefetchNextItems(this.props.page);
}
componentWillReceiveProps(nextProps) {
// update the next items if the page prop changed
if (this.props.page !== nextProps.page) {
this.prefetchNextItems(nextProps.page);
}
}
prefetchNextItems = currentPage => {
const page = currentPage + 1;
console.log(`Prefetching Next items! Page ${page}`);
this.props.client.query({
query: ALL_ITEMS_QUERY,
variables: {
skip: page * 3 - 3,
},
});
};
render() {
console.log(this.props);
// 1
if (this.props.itemsQuery && this.props.itemsQuery.loading) {
return <div>Loading</div>;
}
// 2
if (this.props.itemsQuery && this.props.itemsQuery.error) {
console.log(this.props.itemsQuery.error);
return <div>Error</div>;
}
console.log(this.props);
// 3
const itemsToRender = this.props.itemsQuery.items;
return (
<div>
<Pagination page={this.props.page} />
<Title>Items For Sale</Title>
<Items key={this.props.page}>{itemsToRender.map((item, i) => <Item key={item.id} item={item} />)}</Items>
</div>
);
}
}
export default withApollo(compose(itemEnhancer)(ItemList));
|