Skip to content

Commit fefde0a

Browse files
author
Carlos Rufo Jimenez
committed
fix(*): final adjustments 🚀
1 parent 0433ca0 commit fefde0a

File tree

7 files changed

+71
-81
lines changed

7 files changed

+71
-81
lines changed

‎src/components/App.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@ import React, { Component } from 'react'
22
import LinkList from './LinkList'
33
import CreateLink from './CreateLink'
44
import Header from './Header'
5+
import { Switch, Route, Redirect } from 'react-router-dom'
56
import Login from './Login'
67
import Search from './Search'
7-
import { Switch, Route, Redirect } from 'react-router-dom'
88

99
class App extends Component {
1010
render() {
1111
return (
12-
<div className="center w85">
12+
<div className='center w85'>
1313
<Header />
14-
<div className="ph3 pv1 background-gray">
14+
<div className='ph3 pv1 background-gray'>
1515
<Switch>
16-
<Route exact path="/" render={() => <Redirect to="/new/1" />} />
17-
<Route exact path="/create" component={CreateLink} />
18-
<Route exact path="/login" component={Login} />
19-
<Route exact path="/search" component={Search} />
20-
<Route exact path="/top" component={LinkList} />
21-
<Route exact path="/new/:page" component={LinkList} />
16+
<Route exact path='/' render={() => <Redirect to='/new/1' />} />
17+
<Route exact path='/create' component={CreateLink} />
18+
<Route exact path='/login' component={Login} />
19+
<Route exact path='/search' component={Search} />
20+
<Route exact path='/top' component={LinkList} />
21+
<Route exact path='/new/:page' component={LinkList} />
2222
</Switch>
2323
</div>
2424
</div>
2525
)
2626
}
2727
}
2828

29-
export default App
29+
export default App

‎src/components/CreateLink.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ const POST_MUTATION = gql`
1111
createdAt
1212
url
1313
description
14-
postedBy {
15-
id
16-
name
17-
}
18-
votes {
19-
id
20-
user {
21-
id
22-
}
23-
}
2414
}
2515
}
2616
`
@@ -78,4 +68,4 @@ class CreateLink extends Component {
7868
}
7969
}
8070

81-
export default CreateLink
71+
export default CreateLink

‎src/components/Header.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ class Header extends Component {
5252
}
5353
}
5454

55-
export default withRouter(Header)
55+
export default withRouter(Header)

‎src/components/Link.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ const VOTE_MUTATION = gql`
2222
}
2323
}
2424
`
25+
2526
class Link extends Component {
2627
render() {
27-
const { link, index, updateStoreAfterVote } = this.props
2828
const authToken = localStorage.getItem(AUTH_TOKEN)
29-
29+
3030
return (
3131
<div className="flex mt2 items-start">
3232
<div className="flex items-center">
33-
<span className="gray">{index + 1}.</span>
33+
<span className="gray">{this.props.index + 1}.</span>
3434
{authToken && (
3535
<Mutation
3636
mutation={VOTE_MUTATION}
37-
variables={{ linkId: link.id }}
37+
variables={{ linkId: this.props.link.id }}
3838
update={(store, { data: { vote } }) =>
39-
updateStoreAfterVote(store, vote, link.id)
39+
this.props.updateStoreAfterVote(store, vote, this.props.link.id)
4040
}
4141
>
4242
{voteMutation => (
@@ -49,17 +49,19 @@ class Link extends Component {
4949
</div>
5050
<div className="ml1">
5151
<div>
52-
{link.description} ({link.url})
52+
{this.props.link.description} ({this.props.link.url})
5353
</div>
5454
<div className="f6 lh-copy gray">
55-
{`${link.votes.length} votes | by
56-
${link.postedBy ? link.postedBy.name : 'Unknown'}
57-
${timeDifferenceForDate(link.createdAt)}`}
55+
{this.props.link.votes.length} votes | by{' '}
56+
{this.props.link.postedBy
57+
? this.props.link.postedBy.name
58+
: 'Unknown'}
59+
{timeDifferenceForDate(this.props.link.createdAt)}
5860
</div>
5961
</div>
6062
</div>
6163
)
6264
}
6365
}
6466

65-
export default Link
67+
export default Link

‎src/components/LinkList.js

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,48 @@ class LinkList extends Component {
8484
_updateCacheAfterVote = (store, createVote, linkId) => {
8585
const isNewPage = this.props.location.pathname.includes('new')
8686
const page = parseInt(this.props.match.params.page, 10)
87-
87+
8888
const skip = isNewPage ? (page - 1) * LINKS_PER_PAGE : 0
8989
const first = isNewPage ? LINKS_PER_PAGE : 100
9090
const orderBy = isNewPage ? 'createdAt_DESC' : null
9191
const data = store.readQuery({
9292
query: FEED_QUERY,
9393
variables: { first, skip, orderBy }
9494
})
95-
95+
9696
const votedLink = data.feed.links.find(link => link.id === linkId)
9797
votedLink.votes = createVote.link.votes
9898
store.writeQuery({ query: FEED_QUERY, data })
9999
}
100100

101+
_subscribeToNewLinks = subscribeToMore => {
102+
subscribeToMore({
103+
document: NEW_LINKS_SUBSCRIPTION,
104+
updateQuery: (prev, { subscriptionData }) => {
105+
if (!subscriptionData.data) return prev
106+
const newLink = subscriptionData.data.newLink.node
107+
108+
return Object.assign({}, prev, {
109+
feed: {
110+
links: [newLink, ...prev.feed.links],
111+
count: prev.feed.links.length + 1,
112+
__typename: prev.feed.__typename
113+
}
114+
})
115+
}
116+
})
117+
}
118+
119+
_subscribeToNewVotes = subscribeToMore => {
120+
subscribeToMore({
121+
document: NEW_VOTES_SUBSCRIPTION
122+
})
123+
}
124+
101125
_getQueryVariables = () => {
102126
const isNewPage = this.props.location.pathname.includes('new')
103127
const page = parseInt(this.props.match.params.page, 10)
104-
128+
105129
const skip = isNewPage ? (page - 1) * LINKS_PER_PAGE : 0
106130
const first = isNewPage ? LINKS_PER_PAGE : 100
107131
const orderBy = isNewPage ? 'createdAt_DESC' : null
@@ -125,7 +149,7 @@ class LinkList extends Component {
125149
this.props.history.push(`/new/${nextPage}`)
126150
}
127151
}
128-
152+
129153
_previousPage = () => {
130154
const page = parseInt(this.props.match.params.page, 10)
131155
if (page > 1) {
@@ -134,46 +158,22 @@ class LinkList extends Component {
134158
}
135159
}
136160

137-
_subscribeToNewLinks = subscribeToMore => {
138-
subscribeToMore({
139-
document: NEW_LINKS_SUBSCRIPTION,
140-
updateQuery: (prev, { subscriptionData }) => {
141-
if (!subscriptionData.data) return prev
142-
const newLink = subscriptionData.data.newLink.node
143-
144-
return Object.assign({}, prev, {
145-
feed: {
146-
links: [newLink, ...prev.feed.links],
147-
count: prev.feed.links.length + 1,
148-
__typename: prev.feed.__typename
149-
}
150-
})
151-
}
152-
})
153-
}
154-
155-
_subscribeToNewVotes = subscribeToMore => {
156-
subscribeToMore({
157-
document: NEW_VOTES_SUBSCRIPTION
158-
})
159-
}
160-
161161
render() {
162162
return (
163163
<Query query={FEED_QUERY} variables={this._getQueryVariables()}>
164164
{({ loading, error, data, subscribeToMore }) => {
165165
if (loading) return <div>Fetching</div>
166166
if (error) return <div>Error</div>
167-
167+
168168
this._subscribeToNewLinks(subscribeToMore)
169169
this._subscribeToNewVotes(subscribeToMore)
170-
170+
171171
const linksToRender = this._getLinksToRender(data)
172172
const isNewPage = this.props.location.pathname.includes('new')
173173
const pageIndex = this.props.match.params.page
174174
? (this.props.match.params.page - 1) * LINKS_PER_PAGE
175175
: 0
176-
176+
177177
return (
178178
<Fragment>
179179
{linksToRender.map((link, index) => (
@@ -189,10 +189,7 @@ class LinkList extends Component {
189189
<div className="pointer mr2" onClick={this._previousPage}>
190190
Previous
191191
</div>
192-
<div
193-
className="pointer"
194-
onClick={this._nextPage.bind(this, data)}
195-
>
192+
<div className="pointer" onClick={() => this._nextPage(data)}>
196193
Next
197194
</div>
198195
</div>
@@ -205,4 +202,4 @@ class LinkList extends Component {
205202
}
206203
}
207204

208-
export default LinkList
205+
export default LinkList

‎src/components/Login.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,4 @@ class Login extends Component {
8888
}
8989
}
9090

91-
export default Login
91+
export default Login

‎src/components/Search.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,19 @@ const FEED_SEARCH_QUERY = gql`
2727
`
2828

2929
class Search extends Component {
30+
3031
state = {
3132
links: [],
3233
filter: ''
3334
}
3435

35-
_executeSearch = async () => {
36-
const { filter } = this.state
37-
const result = await this.props.client.query({
38-
query: FEED_SEARCH_QUERY,
39-
variables: { filter }
40-
})
41-
const links = result.data.feed.links
42-
this.setState({ links })
43-
}
44-
4536
render() {
4637
return (
4738
<div>
4839
<div>
4940
Search
5041
<input
51-
type="text"
42+
type='text'
5243
onChange={e => this.setState({ filter: e.target.value })}
5344
/>
5445
<button onClick={() => this._executeSearch()}>OK</button>
@@ -59,6 +50,16 @@ class Search extends Component {
5950
</div>
6051
)
6152
}
53+
54+
_executeSearch = async () => {
55+
const { filter } = this.state
56+
const result = await this.props.client.query({
57+
query: FEED_SEARCH_QUERY,
58+
variables: { filter },
59+
})
60+
const links = result.data.feed.links
61+
this.setState({ links })
62+
}
6263
}
6364

64-
export default withApollo(Search)
65+
export default withApollo(Search)

0 commit comments

Comments
 (0)