forked from btholt/complete-intro-to-react-v5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchParams.js
More file actions
87 lines (79 loc) · 2.35 KB
/
SearchParams.js
File metadata and controls
87 lines (79 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import React, { useState, useEffect, useContext } from "react";
import pf, { ANIMALS } from "petfinder-client";
import useDropdown from "./useDropdown";
import Results from "./Results";
import ThemeContext from "./ThemeContext";
const petfinder = pf({
key: process.env.API_KEY,
secret: process.env.API_SECRET
});
const SearchParams = () => {
const [theme, setTheme] = useContext(ThemeContext);
const [location, updateLocation] = useState("Seattle, WA");
const [breeds, updateBreeds] = useState([]);
const [pets, setPets] = useState([]);
const [animal, AnimalDropdown] = useDropdown("Animal", "dog", ANIMALS);
const [breed, BreedDropdown, updateBreed] = useDropdown("Breed", "", breeds);
async function requestPets() {
const res = await petfinder.pet.find({
location,
breed,
animal,
output: "full"
});
setPets(
Array.isArray(res.petfinder.pets.pet)
? res.petfinder.pets.pet
: [res.petfinder.pets.pet]
);
}
useEffect(() => {
updateBreeds([]);
updateBreed("");
petfinder.breed.list({ animal }).then(data => {
updateBreeds(
Array.isArray(data.petfinder.breeds.breed)
? data.petfinder.breeds.breed
: [data.petfinder.breeds.breed]
);
}, console.error);
}, [animal]);
return (
<div className="search-params">
<form
onSubmit={e => {
e.preventDefault();
requestPets();
}}
>
<label htmlFor="location">
Location
<input
id="location"
value={location}
placeholder="Location"
onChange={e => updateLocation(e.target.value)}
/>
</label>
<AnimalDropdown />
<BreedDropdown />
<label htmlFor="location">
Theme
<select
value={theme}
onChange={e => setTheme(e.target.value)}
onBlur={e => setTheme(e.target.value)}
>
<option value="peru">Peru</option>
<option value="darkblue">Dark Blue</option>
<option value="chartreuse">Chartreuse</option>
<option value="mediumorchid">Medium Orchid</option>
</select>
</label>
<button style={{ backgroundColor: theme }}>Submit</button>
</form>
<Results pets={pets} />
</div>
);
};
export default SearchParams;