-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.html
60 lines (56 loc) · 1.98 KB
/
search.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Book Search</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>Search for Books</h1>
<a href="./home.html" class="home-link">Home</a>
</header>
<main>
<div class="search-container">
<input type="text" id="searchInput" placeholder="Enter book title..." />
<button id="searchButton">Search</button>
</div>
<div id="resultsContainer"></div>
</main>
<script>
const searchButton = document.getElementById('searchButton');
const searchInput = document.getElementById('searchInput');
const resultsContainer = document.getElementById('resultsContainer');
searchButton.addEventListener('click', async () => {
const query = searchInput.value.trim();
if (!query) {
resultsContainer.innerHTML = "<p>Please enter a search term.</p>";
return;
}
try {
const response = await fetch(`http://localhost:3000/api/search?q=${encodeURIComponent(query)}`);
const data = await response.json();
if (data.results.length === 0) {
resultsContainer.innerHTML = "<p>No books found.</p>";
return;
}
resultsContainer.innerHTML = `
<ul>
${data.results.map(book => `
<li>
<h3>${book.book_title}</h3>
<p>Chapter: ${book.chapter_title}</p>
<a href="${book.pastebin_url}" target="_blank">Read Chapter</a>
</li>
`).join('')}
</ul>
`;
} catch (error) {
resultsContainer.innerHTML = "<p>An error occurred. Please try again.</p>";
console.error(error);
}
});
</script>
</body>
</html>