generated from OneGraph/oneblog
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPosts.js
132 lines (124 loc) · 3.55 KB
/
Posts.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// @flow
import React from 'react';
import graphql from 'babel-plugin-relay/macro';
import {createPaginationContainer, type RelayPaginationProp} from 'react-relay';
import Post from './Post';
import PostCard from './PostCard';
import type {Posts_repository} from './__generated__/Posts_repository.graphql';
import LoadingSpinner from './loadingSpinner';
import {Box} from 'grommet/components/Box';
import {useInView} from 'react-intersection-observer';
import config from './config';
import 'intersection-observer';
type Props = {|
relay: RelayPaginationProp,
repository: Posts_repository,
|};
// TODO: pagination. Can do pages or infinite scroll
const Posts = ({relay, repository}: Props) => {
const [isLoading, setIsLoading] = React.useState(false);
const [inViewRef, inView] = useInView({threshold: 0});
React.useEffect(() => {
if (inView && !isLoading && !relay.isLoading() && relay.hasMore()) {
setIsLoading(true);
relay.loadMore(10, (x) => {
setIsLoading(false);
});
}
}, [relay, isLoading, setIsLoading, inView]);
const issues = [];
for (const edge of repository.issues.edges || []) {
if (edge && edge.node) {
issues.push(edge.node);
}
}
return (
<div className="grid gap-8 md:grid-cols-2 lg:grid-cols-3">
{issues.map((node, i) => (
<div key={node.id} className="flex flex-col h-full overflow-hidden border border-gray-200 rounded-lg">
<PostCard
context="list"
post={node}
ref={!isLoading && i === issues.length - 1 ? inViewRef : null}
key={node.id}
/>
</div>
))}
{isLoading ? (
<Box
align="center"
margin="medium"
style={{
maxWidth: 704,
}}>
<LoadingSpinner width="48px" height="48px" />
</Box>
) : null}
</div>
);
};
export default createPaginationContainer(
Posts,
{
repository: graphql`
fragment Posts_repository on Repository
@argumentDefinitions(
count: {type: "Int", defaultValue: 10}
cursor: {type: "String"}
orderBy: {
type: "IssueOrder"
defaultValue: {direction: DESC, field: CREATED_AT}
}
) {
issues(
first: $count
after: $cursor
orderBy: $orderBy
labels: ["publish", "Publish"]
) @connection(key: "PostsCard_posts_issues") {
isClientFetched @__clientField(handle: "isClientFetched")
edges {
node {
id
...PostCard_post
}
}
}
}
`,
},
{
direction: 'forward',
getConnectionFromProps(props) {
return props.repository && props.repository.issues;
},
getVariables(props, {count, cursor}, fragmentVariables) {
return {
count: count,
cursor,
orderBy: fragmentVariables.orderBy,
};
},
query: graphql`
# repoName and repoOwner provided by fixedVariables
query PostsPaginationQuery(
$count: Int!
$cursor: String
$orderBy: IssueOrder
$repoOwner: String!
$repoName: String!
)
@persistedQueryConfiguration(
freeVariables: ["count", "cursor", "orderBy"]
fixedVariables: {environmentVariable: "REPOSITORY_FIXED_VARIABLES"}
cacheSeconds: 300
) {
repository(name: $repoName, owner: $repoOwner) {
__typename
...Posts_repository
@arguments(count: $count, cursor: $cursor, orderBy: $orderBy)
}
}
`,
},
);