Skip to content

Commit def3256

Browse files
author
Carlos Rufo Jimenez
committed
fix
1 parent 640a8b3 commit def3256

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/components/LinkList.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ export const FEED_QUERY = gql`
2929
}
3030
`
3131

32-
const updateCacheAfterVote = (page, isNewPage, cache, createVote, linkId) => {
32+
const updateCacheAfterVote = (props, cache, createVote, linkId) => {
33+
const isNewPage = props.location.pathname.includes('new')
34+
const page = parseInt(props.match.params.page, 10)
35+
3336
const skip = isNewPage ? (page - 1) * LINKS_PER_PAGE : 0
3437
const first = isNewPage ? LINKS_PER_PAGE : 100
3538
const orderBy = isNewPage ? 'createdAt_DESC' : null
@@ -43,14 +46,18 @@ const updateCacheAfterVote = (page, isNewPage, cache, createVote, linkId) => {
4346
cache.writeQuery({ query: FEED_QUERY, data })
4447
}
4548

46-
const getQueryVariables = (page, isNewPage) => {
49+
const getQueryVariables = props => {
50+
const isNewPage = props.location.pathname.includes('new')
51+
const page = parseInt(props.match.params.page, 10)
52+
4753
const skip = isNewPage ? (page - 1) * LINKS_PER_PAGE : 0
4854
const first = isNewPage ? LINKS_PER_PAGE : 100
4955
const orderBy = isNewPage ? 'createdAt_DESC' : null
5056
return { first, skip, orderBy }
5157
}
5258

53-
const getLinksToRender = (isNewPage, data) => {
59+
const getLinksToRender = (props, data) => {
60+
const isNewPage = props.location.pathname.includes('new')
5461
if (isNewPage) {
5562
return data.feed.links
5663
}
@@ -59,30 +66,31 @@ const getLinksToRender = (isNewPage, data) => {
5966
return rankedLinks
6067
}
6168

62-
const nextPage = (page, data, history) => {
69+
const nextPage = (props, data) => {
70+
const page = parseInt(props.match.params.page, 10)
6371
if (page <= data.feed.count / LINKS_PER_PAGE) {
6472
const nextPage = page + 1
65-
history.push(`/new/${nextPage}`)
73+
props.history.push(`/new/${nextPage}`)
6674
}
6775
}
6876

69-
const previousPage = (page, history) => {
77+
const previousPage = props => {
78+
const page = parseInt(props.match.params.page, 10)
7079
if (page > 1) {
7180
const previousPage = page - 1
72-
history.push(`/new/${previousPage}`)
81+
props.history.push(`/new/${previousPage}`)
7382
}
7483
}
7584

76-
export default ({ match, location, history }) => {
77-
const isNewPage = location.pathname.includes('new')
78-
const page = parseInt(match.params.page, 10)
85+
export default props => {
7986
return (
80-
<Query query={FEED_QUERY} variables={getQueryVariables(page, isNewPage)}>
87+
<Query query={FEED_QUERY} variables={getQueryVariables(props)}>
8188
{({ loading, error, data, subscribeToMore }) => {
8289
if (loading) return <div>Fetching</div>
8390
if (error) return <div>Error</div>
8491

85-
const linksToRender = getLinksToRender(isNewPage, data)
92+
const linksToRender = getLinksToRender(props, data)
93+
const isNewPage = props.location.pathname.includes('new')
8694

8795
return (
8896
<LinkListSubscriptions subscribeToMore={subscribeToMore}>
@@ -91,24 +99,20 @@ export default ({ match, location, history }) => {
9199
key={link.id}
92100
link={link}
93101
index={index}
94-
updateStoreAfterVote={updateCacheAfterVote.bind(
95-
this,
96-
page,
97-
isNewPage
98-
)}
102+
updateStoreAfterVote={updateCacheAfterVote.bind(this, props)}
99103
/>
100104
))}
101105
{isNewPage && (
102106
<div className="flex ml4 mv3 gray">
103107
<div
104108
className="pointer mr2"
105-
onClick={() => previousPage(page, history)}
109+
onClick={previousPage.bind(this, props)}
106110
>
107111
Previous
108112
</div>
109113
<div
110114
className="pointer"
111-
onClick={() => nextPage(page, data, history)}
115+
onClick={nextPage.bind(this, props, data)}
112116
>
113117
Next
114118
</div>

0 commit comments

Comments
 (0)