-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
102 lines (80 loc) · 2.82 KB
/
script.js
File metadata and controls
102 lines (80 loc) · 2.82 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const myLibrary = [
{ title: "The Hobbit", author: "J.R.R. Tolkien", pages: 295, read: true },
{ title: "1984", author: "George Orwell", pages: 328, read: false },
{ title: "To Kill a Mockingbird", author: "Harper Lee", pages: 281, read: true }
];
// Book Object
function Book(title, author, pages, read, id) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
this.id = id;
}
// Add Book Object
function addBookToLibrary(title, author, pages, read) {
const id = crypto.randomUUID();
const newBook = new Book(title, author, pages, read, id);
myLibrary.push(newBook);
}
// Display Books
function displayBooks() {
const bookContainer = document.querySelector(".book-container");
bookContainer.innerHTML = "";
for (let i = 0; i < myLibrary.length; i++) {
const book = myLibrary[i];
const card = document.createElement("div");
card.className = "book-card";
card.innerHTML = `
<h3>${book.title}</h3>
<p>Author: ${book.author}</p>
<p>Pages: ${book.pages}</p>
<p>Read: ${book.read ? 'Read' : 'Not Read Yet'}</p>
<button class="readBtn">Read</button>
<button class="delete">Delete</button>
`;
// Read Button
const readBtn = card.querySelector(".readBtn");
readBtn.addEventListener("click", function() {
book.read = !book.read;
displayBooks();
})
// Delete Button
const deleteBtn = card.querySelector(".delete");
deleteBtn.addEventListener("click", function() {
myLibrary.splice(i, 1);
displayBooks();
})
bookContainer.appendChild(card);
}
}
displayBooks();
// Form Popup
const showFormBtn = document.querySelector(".newBookButton");
const formPopup = document.querySelector("#formPopUp");
const closeBtn = document.querySelector(".close");
showFormBtn.addEventListener("click", function() {
formPopup.style.display = "flex";
})
closeBtn.addEventListener("click", function() {
formPopup.style.display = "none";
})
window.addEventListener("click", function(event) {
if (event.target === formPopup) {
formPopup.style.display = "none";
}
})
// Making new Book card from form
const bookForm = document.querySelector("#bookForm");
bookForm.addEventListener("submit", function(event) {
event.preventDefault();
const title = document.getElementById("title").value;
const author = document.getElementById("author").value;
const pages = document.getElementById("pages").value;
const readStatus = document.getElementById("readStatus").checked;
const newBook = new Book(title, author, pages, readStatus);
myLibrary.push(newBook);
displayBooks();
formPopup.style.display = "none";
bookForm.reset();
})