-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
72 lines (60 loc) · 2.3 KB
/
script.js
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
import { Library } from "./module/libraryscript.js";
let myLibrary = new Library();
//hardcoding initial 3 entries.
myLibrary.addBookToLibrary("Harry Potter1", "J.K.Rowling", 400, false);
myLibrary.addBookToLibrary("Harry Potter2", "J.K.Rowling", 450, true);
myLibrary.addBookToLibrary("Harry Potter3", "J.K.Rowling", 500, false);
const librarySection = document.querySelector(".myBooks");
const addButton = document.querySelector(".add-book-btn");
const dialog = document.getElementById("bookDialog");
const closeDialogButton = document.getElementById("closeDialog");
const newBookForm = document.getElementById("newBookForm");
addButton.addEventListener("click", () => {
dialog.showModal();
});
closeDialogButton.addEventListener("click", () => {
dialog.close();
});
newBookForm.addEventListener("submit", (event) => {
event.preventDefault(); //to not send data to the server
const title = document.getElementById("title").value;
const author = document.getElementById("author").value;
const pageNo = document.getElementById("pageNo").value;
const isRead = document.getElementById("isRead").checked;
myLibrary.addBookToLibrary(title, author, parseInt(pageNo), isRead);
renderLibrary();
dialog.close();
newBookForm.reset();
});
function renderLibrary() {
librarySection.innerHTML = "";
myLibrary.books.forEach((book, index) => {
let newBookElement = document.createElement("div");
newBookElement.classList.add("book");
newBookElement.innerHTML = `
<h2>${book.title}</h2>
<p>Author: ${book.author}</p>
<p>Pages: ${book.pageNo}</p>
<p>Status: <button class="toggle-read-btn">${
book.isRead ? "Read" : "Not read yet"
}</button></p>
<button class="remove-book-btn">Remove</button>
`;
// Event listener to remove the book
newBookElement
.querySelector(".remove-book-btn")
.addEventListener("click", () => {
myLibrary.removeBookFromLibrary(index);
renderLibrary();
});
// Event listener to toggle the read status
newBookElement
.querySelector(".toggle-read-btn")
.addEventListener("click", () => {
myLibrary.toggleBookReadStatus(index);
renderLibrary();
});
librarySection.appendChild(newBookElement);
});
}
renderLibrary();