Skip to content

Commit 83dc82c

Browse files
author
Carlos Rufo
committed
remove node & check prev link exists
1 parent 2c4b6d9 commit 83dc82c

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

src/components/LinkList.js

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,18 @@ export const FEED_QUERY = gql`
3131
const NEW_LINKS_SUBSCRIPTION = gql`
3232
subscription {
3333
newLink {
34-
node {
34+
id
35+
url
36+
description
37+
createdAt
38+
postedBy {
3539
id
36-
url
37-
description
38-
createdAt
39-
postedBy {
40+
name
41+
}
42+
votes {
43+
id
44+
user {
4045
id
41-
name
42-
}
43-
votes {
44-
id
45-
user {
46-
id
47-
}
4846
}
4947
}
5048
}
@@ -54,28 +52,26 @@ const NEW_LINKS_SUBSCRIPTION = gql`
5452
const NEW_VOTES_SUBSCRIPTION = gql`
5553
subscription {
5654
newVote {
57-
node {
55+
id
56+
link {
5857
id
59-
link {
58+
url
59+
description
60+
createdAt
61+
postedBy {
6062
id
61-
url
62-
description
63-
createdAt
64-
postedBy {
65-
id
66-
name
67-
}
68-
votes {
69-
id
70-
user {
71-
id
72-
}
73-
}
63+
name
7464
}
75-
user {
65+
votes {
7666
id
67+
user {
68+
id
69+
}
7770
}
7871
}
72+
user {
73+
id
74+
}
7975
}
8076
}
8177
`
@@ -84,15 +80,15 @@ class LinkList extends Component {
8480
_updateCacheAfterVote = (store, createVote, linkId) => {
8581
const isNewPage = this.props.location.pathname.includes('new')
8682
const page = parseInt(this.props.match.params.page, 10)
87-
83+
8884
const skip = isNewPage ? (page - 1) * LINKS_PER_PAGE : 0
8985
const first = isNewPage ? LINKS_PER_PAGE : 100
9086
const orderBy = isNewPage ? 'createdAt_DESC' : null
9187
const data = store.readQuery({
9288
query: FEED_QUERY,
93-
variables: { first, skip, orderBy }
89+
variables: { first, skip, orderBy },
9490
})
95-
91+
9692
const votedLink = data.feed.links.find(link => link.id === linkId)
9793
votedLink.votes = createVote.link.votes
9894
store.writeQuery({ query: FEED_QUERY, data })
@@ -103,29 +99,29 @@ class LinkList extends Component {
10399
document: NEW_LINKS_SUBSCRIPTION,
104100
updateQuery: (prev, { subscriptionData }) => {
105101
if (!subscriptionData.data) return prev
106-
const newLink = subscriptionData.data.newLink.node
107-
102+
const newLink = subscriptionData.data.newLink
103+
const exists = prev.feed.links.find(({ id }) => id === newLink.id)
104+
if (exists) return prev
108105
return Object.assign({}, prev, {
109106
feed: {
110107
links: [newLink, ...prev.feed.links],
111108
count: prev.feed.links.length + 1,
112-
__typename: prev.feed.__typename
113-
}
109+
__typename: prev.feed.__typename,
110+
},
114111
})
115-
}
112+
},
116113
})
117114
}
118115

119116
_subscribeToNewVotes = subscribeToMore => {
120117
subscribeToMore({
121-
document: NEW_VOTES_SUBSCRIPTION
118+
document: NEW_VOTES_SUBSCRIPTION,
122119
})
123120
}
124-
125121
_getQueryVariables = () => {
126122
const isNewPage = this.props.location.pathname.includes('new')
127123
const page = parseInt(this.props.match.params.page, 10)
128-
124+
129125
const skip = isNewPage ? (page - 1) * LINKS_PER_PAGE : 0
130126
const first = isNewPage ? LINKS_PER_PAGE : 100
131127
const orderBy = isNewPage ? 'createdAt_DESC' : null
@@ -141,15 +137,14 @@ class LinkList extends Component {
141137
rankedLinks.sort((l1, l2) => l2.votes.length - l1.votes.length)
142138
return rankedLinks
143139
}
144-
145140
_nextPage = data => {
146141
const page = parseInt(this.props.match.params.page, 10)
147142
if (page <= data.feed.count / LINKS_PER_PAGE) {
148143
const nextPage = page + 1
149144
this.props.history.push(`/new/${nextPage}`)
150145
}
151146
}
152-
147+
153148
_previousPage = () => {
154149
const page = parseInt(this.props.match.params.page, 10)
155150
if (page > 1) {
@@ -164,16 +159,16 @@ class LinkList extends Component {
164159
{({ loading, error, data, subscribeToMore }) => {
165160
if (loading) return <div>Fetching</div>
166161
if (error) return <div>Error</div>
167-
162+
168163
this._subscribeToNewLinks(subscribeToMore)
169164
this._subscribeToNewVotes(subscribeToMore)
170-
165+
171166
const linksToRender = this._getLinksToRender(data)
172167
const isNewPage = this.props.location.pathname.includes('new')
173168
const pageIndex = this.props.match.params.page
174169
? (this.props.match.params.page - 1) * LINKS_PER_PAGE
175170
: 0
176-
171+
177172
return (
178173
<Fragment>
179174
{linksToRender.map((link, index) => (
@@ -202,4 +197,4 @@ class LinkList extends Component {
202197
}
203198
}
204199

205-
export default LinkList
200+
export default LinkList

0 commit comments

Comments
 (0)