-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkList.js
201 lines (182 loc) · 4.92 KB
/
LinkList.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import React, { Component, Fragment } from 'react'
import Link from './Link'
import { Query } from 'react-apollo'
import gql from 'graphql-tag'
import { LINKS_PER_PAGE } from '../constants'
export const FEED_QUERY = gql`
query FeedQuery($first: Int, $skip: Int, $orderBy: LinkOrderByInput) {
feed(first: $first, skip: $skip, orderBy: $orderBy) {
links {
id
createdAt
url
description
postedBy {
id
name
}
votes {
id
user {
id
}
}
}
count
}
}
`
const NEW_LINKS_SUBSCRIPTION = gql`
subscription {
newLink {
id
url
description
createdAt
postedBy {
id
name
}
votes {
id
user {
id
}
}
}
}
`
const NEW_VOTES_SUBSCRIPTION = gql`
subscription {
newVote {
id
link {
id
url
description
createdAt
postedBy {
id
name
}
votes {
id
user {
id
}
}
}
user {
id
}
}
}
`
class LinkList extends Component {
_updateCacheAfterVote = (store, createVote, linkId) => {
const isNewPage = this.props.location.pathname.includes('new')
const page = parseInt(this.props.match.params.page, 10)
const skip = isNewPage ? (page - 1) * LINKS_PER_PAGE : 0
const first = isNewPage ? LINKS_PER_PAGE : 100
const orderBy = isNewPage ? 'createdAt_DESC' : null
const data = store.readQuery({
query: FEED_QUERY,
variables: { first, skip, orderBy }
})
const votedLink = data.feed.links.find(link => link.id === linkId)
votedLink.votes = createVote.link.votes
store.writeQuery({ query: FEED_QUERY, data })
}
_subscribeToNewLinks = subscribeToMore => {
subscribeToMore({
document: NEW_LINKS_SUBSCRIPTION,
updateQuery: (prev, { subscriptionData }) => {
if (!subscriptionData.data) return prev
const newLink = subscriptionData.data.newLink
return Object.assign({}, prev, {
feed: {
links: [newLink, ...prev.feed.links],
count: prev.feed.links.length + 1,
__typename: prev.feed.__typename
}
})
}
})
}
_subscribeToNewVotes = subscribeToMore => {
subscribeToMore({
document: NEW_VOTES_SUBSCRIPTION
})
}
_getQueryVariables = () => {
const isNewPage = this.props.location.pathname.includes('new')
const page = parseInt(this.props.match.params.page, 10)
const skip = isNewPage ? (page - 1) * LINKS_PER_PAGE : 0
const first = isNewPage ? LINKS_PER_PAGE : 100
const orderBy = isNewPage ? 'createdAt_DESC' : null
return { first, skip, orderBy }
}
_getLinksToRender = data => {
const isNewPage = this.props.location.pathname.includes('new')
if (isNewPage) {
return data.feed.links
}
const rankedLinks = data.feed.links.slice()
rankedLinks.sort((l1, l2) => l2.votes.length - l1.votes.length)
return rankedLinks
}
_nextPage = data => {
const page = parseInt(this.props.match.params.page, 10)
if (page <= data.feed.count / LINKS_PER_PAGE) {
const nextPage = page + 1
this.props.history.push(`/new/${nextPage}`)
}
}
_previousPage = () => {
const page = parseInt(this.props.match.params.page, 10)
if (page > 1) {
const previousPage = page - 1
this.props.history.push(`/new/${previousPage}`)
}
}
render() {
return (
<Query query={FEED_QUERY} variables={this._getQueryVariables()}>
{({ loading, error, data, subscribeToMore }) => {
if (loading) return <div>Fetching</div>
if (error) return <div>Error</div>
this._subscribeToNewLinks(subscribeToMore)
this._subscribeToNewVotes(subscribeToMore)
const linksToRender = this._getLinksToRender(data)
const isNewPage = this.props.location.pathname.includes('new')
const pageIndex = this.props.match.params.page
? (this.props.match.params.page - 1) * LINKS_PER_PAGE
: 0
return (
<Fragment>
{linksToRender.map((link, index) => (
<Link
key={link.id}
link={link}
index={index + pageIndex}
updateStoreAfterVote={this._updateCacheAfterVote}
/>
))}
{isNewPage && (
<div className="flex ml4 mv3 gray">
<div className="pointer mr2" onClick={this._previousPage}>
Previous
</div>
<div className="pointer" onClick={() => this._nextPage(data)}>
Next
</div>
</div>
)}
</Fragment>
)
}}
</Query>
)
}
}
export default LinkList