Skip to content

Commit 9311c60

Browse files
committed
8
1 parent c0da362 commit 9311c60

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

src/App.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ class App extends React.Component {
7474
<div>
7575
<header>
7676
<Link to="/">Adopt Me!</Link>
77+
<Link to="/search-params">
78+
<span aria-label="search" role="img">
79+
🔍
80+
</span>
81+
</Link>
7782
</header>
7883
<Provider value={this.state}>
7984
<Router>

src/Results.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22
import pf from "petfinder-client";
33
import Pet from "./Pet";
44
import SearchBox from "./SearchBox";
5+
import { Consumer } from "./SearchContext";
56

67
const petfinder = pf({
78
key: process.env.API_KEY,
@@ -17,8 +18,16 @@ class Results extends React.Component {
1718
};
1819
}
1920
componentDidMount() {
21+
this.search();
22+
}
23+
search = () => {
2024
petfinder.pet
21-
.find({ location: "Seattle, WA", output: "full" })
25+
.find({
26+
location: this.props.searchParams.location,
27+
animal: this.props.searchParams.animal,
28+
breed: this.props.searchParams.breed,
29+
output: "full"
30+
})
2231
.then(data => {
2332
let pets;
2433
if (data.petfinder.pets && data.petfinder.pets.pet) {
@@ -34,11 +43,11 @@ class Results extends React.Component {
3443
pets: pets
3544
});
3645
});
37-
}
46+
};
3847
render() {
3948
return (
4049
<div className="search">
41-
<SearchBox />
50+
<SearchBox search={this.search} />
4251
{this.state.pets.map(pet => {
4352
let breed;
4453
if (Array.isArray(pet.breeds.breed)) {
@@ -63,4 +72,10 @@ class Results extends React.Component {
6372
}
6473
}
6574

66-
export default Results;
75+
export default function ResultsWithContext(props) {
76+
return (
77+
<Consumer>
78+
{context => <Results {...props} searchParams={context} />}
79+
</Consumer>
80+
);
81+
}

src/SearchBox.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import { ANIMALS } from "petfinder-client";
33
import { Consumer } from "./SearchContext";
44

55
class Search extends React.Component {
6+
handleFormSubmit = event => {
7+
event.preventDefault();
8+
this.props.search();
9+
};
610
render() {
711
return (
812
<Consumer>
913
{context => (
1014
<div className="search-params">
11-
<form>
15+
<form onSubmit={this.handleFormSubmit}>
1216
<label htmlFor="location">
1317
Location
1418
<input

src/SearchParams.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import React from "react";
2+
import { navigate } from "@reach/router";
23
import SearchBox from "./SearchBox";
34

45
class Search extends React.Component {
6+
search() {
7+
navigate("/");
8+
}
59
render() {
610
return (
711
<div className="search-route">
8-
<SearchBox />
12+
<SearchBox search={this.search} />
913
</div>
1014
);
1115
}

0 commit comments

Comments
 (0)