Skip to content

Commit 7893e3c

Browse files
authored
Update BookFinder.js
1 parent 06ac436 commit 7893e3c

File tree

1 file changed

+96
-34
lines changed

1 file changed

+96
-34
lines changed

7-data-fetching/36-book-finder/BookFinder.js

+96-34
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,119 @@
33

44
import { useState, useEffect } from "react";
55

6-
export default function App() {
7-
const [pokemonList, setPokemonList] = useState([]);
8-
const [filteredPokemonList, setFilteredPokemonList] = useState([]);
9-
10-
useEffect(function () {
11-
async function fetchData() {
12-
try {
13-
const response = await fetch("https://pokeapi.co/api/v2/pokemon?limit=25");
14-
const data = await response.json();
15-
16-
const pokemonDataList = [];
17-
for (const pokemon of data.results) {
18-
const pokemonResponse = await fetch(pokemon.url);
19-
const pokemonData = await pokemonResponse.json();
20-
pokemonDataList.push(pokemonData);
21-
}
22-
23-
setPokemonList(pokemonDataList);
24-
setFilteredPokemonList(pokemonDataList);
25-
} catch (error) {
26-
console.log("Error fetching data: ", error);
6+
export default function BookFinderApp() {
7+
const [query, setQuery] = useState("");
8+
const [books, setBooks] = useState([]);
9+
const [loading, setLoading] = useState(false);
10+
const [error, setError] = useState(null);
11+
12+
async function fetchData(filterValue = "") {
13+
setError(null);
14+
15+
try {
16+
let url = `https://www.googleapis.com/books/v1/volumes?q=${query}`;
17+
18+
const response = await fetch(url);
19+
if (!response.ok) {
20+
throw new Error("Network response was not ok.");
21+
}
22+
const data = await response.json();
23+
console.log(data.items);
24+
let filteredBooks = data.items;
25+
if (filterValue !== "") {
26+
filteredBooks = filteredBooks.filter(function (book) {
27+
return (
28+
book.volumeInfo &&
29+
book.volumeInfo.categories &&
30+
book.volumeInfo.categories.includes(filterValue)
31+
);
32+
});
2733
}
34+
35+
setBooks(filteredBooks);
36+
setLoading(false);
37+
} catch (error) {
38+
setError("Error fetching data. Please try again later.");
2839
}
40+
}
2941

42+
useEffect(
43+
function () {
44+
if (query !== "") {
45+
fetchData();
46+
}
47+
},
48+
[loading],
49+
);
50+
51+
function handleInputChange(event) {
52+
setQuery(event.target.value);
53+
}
54+
55+
function handleSearch(event) {
56+
setLoading(true);
3057
fetchData();
31-
}, []);
58+
}
3259

3360
function handleFilterChange(event) {
34-
const searchTerm = event.target.value.toLowerCase();
35-
const filtered = pokemonList.filter(function(pokemon) {
36-
return pokemon.name.toLowerCase().startsWith(searchTerm);
37-
});
38-
setFilteredPokemonList(filtered);
61+
setLoading(true);
62+
fetchData(event.target.value);
3963
}
4064

4165
return (
4266
<div>
43-
<h1>Pokemon List</h1>
44-
<div>
45-
<input type="text" placeholder="Search Pokemon..." onChange={handleFilterChange} />
67+
<h1>Book Finder</h1>
68+
<div id="input-fields">
69+
<input
70+
type="text"
71+
value={query}
72+
onChange={handleInputChange}
73+
placeholder="Search for books..."
74+
/>
75+
<button type="submit" onClick={handleSearch}>
76+
Search
77+
</button>
78+
<br />
79+
<label htmlFor="filter">Filter by:</label>
80+
<select id="filter" onChange={handleFilterChange}>
81+
<option value="">All</option>
82+
<option value="Paid ebooks">Paid E-books</option>
83+
<option value="free-ebooks">Free E-books</option>
84+
</select>
4685
</div>
86+
{loading && <p>Loading...</p>}
87+
{error && <p>{error}</p>}
4788
<ul>
48-
{filteredPokemonList.map(function(pokemon, index) {
89+
{books.map(function (book) {
4990
return (
50-
<li key={index} className="pokemon">
51-
<p>{pokemon.id}. {pokemon.name[0].toUpperCase() + pokemon.name.slice(1)}</p>
52-
<img src={pokemon.sprites.front_default} alt={pokemon.name} />
91+
<li key={book.id} className="book">
92+
{book.volumeInfo.imageLinks &&
93+
book.volumeInfo.imageLinks.thumbnail && (
94+
<img
95+
src={book.volumeInfo.imageLinks.thumbnail}
96+
alt={book.volumeInfo.title}
97+
className="book-cover"
98+
/>
99+
)}
100+
<div className="book-info">
101+
<h2>{book.volumeInfo.title}</h2>
102+
<p>
103+
{book.volumeInfo.authors
104+
? book.volumeInfo.authors.join(", ")
105+
: "Unknown Author"}
106+
</p>
107+
108+
<p>
109+
{book.volumeInfo.description
110+
? `${book.volumeInfo.description.substring(0, 100)}...`
111+
: "No description available"}
112+
</p>
113+
</div>
53114
</li>
54115
);
55116
})}
56117
</ul>
57118
</div>
58119
);
59120
}
121+

0 commit comments

Comments
 (0)