-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (67 loc) · 2.78 KB
/
Copy pathscript.js
File metadata and controls
82 lines (67 loc) · 2.78 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
const searchBar = document.getElementById('search-bar');
const searchBtn = document.getElementById('search-btn');
const booksContainer = document.getElementById('books-container');
const libraryContainer = document.getElementById('library-container');
let myLibrary = JSON.parse(localStorage.getItem('library')) || [];
async function fetchBooks(query) {
const response = await fetch(`https://www.googleapis.com/books/v1/volumes?q=${query}`);
const data = await response.json();
displayBooks(data.items);
}
function displayBooks(books) {
booksContainer.innerHTML = "";
books.forEach(book => {
const bookInfo = book.volumeInfo;
const bookCard = document.createElement('div');
bookCard.className = "book-card";
bookCard.innerHTML = `
<img src="${bookInfo.imageLinks?.thumbnail || 'https://via.placeholder.com/50'}" alt="Book Cover">
<div>
<h3>${bookInfo.title}</h3>
<p>${bookInfo.authors ? bookInfo.authors.join(', ') : 'Unknown Author'}</p>
</div>
<button onclick="addToLibrary('${book.id}', '${bookInfo.title}', '${bookInfo.authors ? bookInfo.authors.join(', ') : ''}', '${bookInfo.imageLinks?.thumbnail || ''}')">Add</button>
`;
booksContainer.appendChild(bookCard);
});
}
function addToLibrary(id, title, author, image) {
if (!myLibrary.some(book => book.id === id)) {
myLibrary.push({ id, title, author, image, progress: 0 });
localStorage.setItem('library', JSON.stringify(myLibrary));
displayLibrary();
}
}
function displayLibrary() {
libraryContainer.innerHTML = "";
myLibrary.forEach((book, index) => {
const bookCard = document.createElement('div');
bookCard.className = "book-card";
bookCard.innerHTML = `
<img src="${book.image || 'https://via.placeholder.com/50'}" alt="Book Cover">
<div>
<h3>${book.title}</h3>
<p>${book.author}</p>
<input type="range" min="0" max="100" value="${book.progress}" onchange="updateProgress(${index}, this.value)">
<span>${book.progress}% Read</span>
</div>
<button onclick="removeFromLibrary(${index})">Remove</button>
`;
libraryContainer.appendChild(bookCard);
});
}
function updateProgress(index, progress) {
myLibrary[index].progress = progress;
localStorage.setItem('library', JSON.stringify(myLibrary));
displayLibrary();
}
function removeFromLibrary(index) {
myLibrary.splice(index, 1);
localStorage.setItem('library', JSON.stringify(myLibrary));
displayLibrary();
}
searchBtn.addEventListener('click', () => {
const query = searchBar.value.trim();
if (query) fetchBooks(query);
});
displayLibrary();