Skip to content

Commit 295218c

Browse files
author
Carlos Rufo Jimenez
committed
7.4-end-filtering-v2.1
1 parent 9d0c824 commit 295218c

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

src/components/App.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import LinkList from './LinkList'
33
import CreateLink from './CreateLink'
44
import Header from './Header'
55
import Login from './Login'
6+
import Search from './Search'
67
import { Switch, Route } from 'react-router-dom'
78

89
class App extends Component {
@@ -12,9 +13,10 @@ class App extends Component {
1213
<Header />
1314
<div className="ph3 pv1 background-gray">
1415
<Switch>
15-
<Route exact path="/login" component={Login} />
1616
<Route exact path="/" component={LinkList} />
1717
<Route exact path="/create" component={CreateLink} />
18+
<Route exact path="/login" component={Login} />
19+
<Route exact path="/search" component={Search} />
1820
</Switch>
1921
</div>
2022
</div>

src/components/Header.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class Header extends Component {
1313
<Link to="/" className="ml1 no-underline black">
1414
new
1515
</Link>
16+
<div className="ml1">|</div>
17+
<Link to="/search" className="ml1 no-underline black">
18+
search
19+
</Link>
1620
{authToken && (
1721
<div className="flex">
1822
<div className="ml1">|</div>

src/components/Search.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React, { Component } from 'react'
2+
import { withApollo } from 'react-apollo'
3+
import gql from 'graphql-tag'
4+
import Link from './Link'
5+
6+
const FEED_SEARCH_QUERY = gql`
7+
query FeedSearchQuery($filter: String!) {
8+
feed(filter: $filter) {
9+
links {
10+
id
11+
url
12+
description
13+
createdAt
14+
postedBy {
15+
id
16+
name
17+
}
18+
votes {
19+
id
20+
user {
21+
id
22+
}
23+
}
24+
}
25+
}
26+
}
27+
`
28+
29+
class Search extends Component {
30+
state = {
31+
links: [],
32+
filter: '',
33+
}
34+
35+
render() {
36+
return (
37+
<div>
38+
<div>
39+
Search
40+
<input
41+
type="text"
42+
onChange={e => this.setState({ filter: e.target.value })}
43+
/>
44+
<button onClick={() => this._executeSearch()}>OK</button>
45+
</div>
46+
{this.state.links.map((link, index) => (
47+
<Link key={link.id} link={link} index={index} />
48+
))}
49+
</div>
50+
)
51+
}
52+
53+
_executeSearch = async () => {
54+
const { filter } = this.state
55+
const result = await this.props.client.query({
56+
query: FEED_SEARCH_QUERY,
57+
variables: { filter },
58+
})
59+
const links = result.data.feed.links
60+
this.setState({ links })
61+
}
62+
}
63+
64+
export default withApollo(Search)

0 commit comments

Comments
 (0)