-
Notifications
You must be signed in to change notification settings - Fork 2
/
UserPosts.js
107 lines (95 loc) · 2.19 KB
/
UserPosts.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
import React from 'react'
import PropTypes from 'prop-types'
import { routerShape } from 'found/lib/PropTypes'
import { graphql } from 'react-relay'
import { paginationContainer } from 'relay-compose-test'
import { compose, flattenProp, withHandlers, withProps } from 'recompose'
import PostList from '../../components/PostList'
export const POST_COUNT = 6
const UserPosts = ({ permission, posts, hasMore, loadMore, router }) => {
if (!permission.isLoggedIn) {
router.push('/login')
return <div />
}
if (!permission.canPublish) {
router.push('/')
return <div />
}
return (
<div>
<PostList
posts={posts.edges}
hasMore={hasMore}
onMore={loadMore}
/>
</div>
)
}
UserPosts.propTypes = {
router: routerShape.isRequired,
permission: PropTypes.shape({
isLoggedIn: PropTypes.bool,
canPublish: PropTypes.bool,
}).isRequired,
posts: PropTypes.shape({
edges: PropTypes.array,
}).isRequired,
hasMore: PropTypes.bool.isRequired,
loadMore: PropTypes.func.isRequired,
}
const fragments = graphql`
fragment UserPosts on Query {
permission {
isLoggedIn
canPublish
}
user {
posts (after: $afterCursor first: $count) @connection(key: "UserPosts_posts") {
pageInfo {
hasNextPage
endCursor
},
edges {
node {
id
...PostTeaser_post
}
}
}
}
}
`
const connectionConfig = {
direction: 'forward',
getFragmentVariables(prevVars, totalCount) {
return {
...prevVars,
count: totalCount,
}
},
getVariables(_props, { count, cursor }) {
return {
afterCursor: cursor,
count,
}
},
query: graphql`
query UserPostsPaginationQuery($afterCursor: String, $count: Int!) {
...UserPosts
}
`,
}
const props = withProps(({ relay }) => ({
hasMore: relay.hasMore(),
}))
const handlers = withHandlers({
loadMore: ({ relay }) => () => relay.isLoading() || relay.loadMore(POST_COUNT),
})
const enhance = compose(
paginationContainer(fragments, connectionConfig),
props,
handlers,
flattenProp('data'),
flattenProp('user'),
)
export default enhance(UserPosts)