|
3 | 3 |
|
4 | 4 | import { useState, useEffect } from "react";
|
5 | 5 |
|
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 | + }); |
27 | 33 | }
|
| 34 | + |
| 35 | + setBooks(filteredBooks); |
| 36 | + setLoading(false); |
| 37 | + } catch (error) { |
| 38 | + setError("Error fetching data. Please try again later."); |
28 | 39 | }
|
| 40 | + } |
29 | 41 |
|
| 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); |
30 | 57 | fetchData();
|
31 |
| - }, []); |
| 58 | + } |
32 | 59 |
|
33 | 60 | 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); |
39 | 63 | }
|
40 | 64 |
|
41 | 65 | return (
|
42 | 66 | <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> |
46 | 85 | </div>
|
| 86 | + {loading && <p>Loading...</p>} |
| 87 | + {error && <p>{error}</p>} |
47 | 88 | <ul>
|
48 |
| - {filteredPokemonList.map(function(pokemon, index) { |
| 89 | + {books.map(function (book) { |
49 | 90 | 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> |
53 | 114 | </li>
|
54 | 115 | );
|
55 | 116 | })}
|
56 | 117 | </ul>
|
57 | 118 | </div>
|
58 | 119 | );
|
59 | 120 | }
|
| 121 | + |
0 commit comments