-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
156 lines (131 loc) · 3.66 KB
/
main.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const showModal = document.getElementById("showModal");
const modal = document.getElementById("modal");
const submitButton = document.getElementById("submit");
const formBook = document.getElementById("form-book");
const overlay = document.getElementById("overlay");
const booksGrid = document.querySelector(".book-cards");
const myLibrary = [];
class Book {
constructor(
title = "Unknown",
author = "Unknown",
pages = "0",
isRead = false
) {
this.title = title;
this.author = author;
this.pages = pages;
this.isRead = isRead;
}
}
class Library {
constructor() {
this.books = [];
}
addBook(newBook) {
if (!this.isInLibrary(newBook)) {
this.books.push(newBook);
}
}
removeBook(title) {
this.books = this.books.filter((book) => book.title !== title);
}
getBook(title) {
return this.books.find((book) => book.title === title);
}
isInLibrary(newBook) {
return this.books.some((book) => book.title === newBook.title);
}
}
const library = new Library();
const createBookCard = (book) => {
const bookCard = document.createElement("div");
const title = document.createElement("p");
const author = document.createElement("p");
const pages = document.createElement("p");
const buttonGroup = document.createElement("div");
const readBtn = document.createElement("button");
const removeBtn = document.createElement("button");
bookCard.classList.add("book-card");
buttonGroup.classList.add("button-group");
readBtn.classList.add("btn");
removeBtn.classList.add("btn");
readBtn.onclick = toggleRead;
removeBtn.onclick = removeBook;
title.textContent = `"${book.title}"`;
author.textContent = book.author;
pages.textContent = `${book.pages} pages`;
removeBtn.textContent = "Remove";
if (book.isRead) {
readBtn.textContent = "Read";
readBtn.classList.add("btn-light-green");
} else {
readBtn.textContent = "Not read";
readBtn.classList.add("btn-light-red");
}
bookCard.appendChild(title);
bookCard.appendChild(author);
bookCard.appendChild(pages);
buttonGroup.appendChild(readBtn);
buttonGroup.appendChild(removeBtn);
bookCard.appendChild(buttonGroup);
booksGrid.appendChild(bookCard);
};
const toggleRead = (e) => {
const title = e.target.parentNode.parentNode.firstChild.innerHTML.replaceAll(
'"',
""
);
const book = library.getBook(title);
book.isRead = !book.isRead;
// saveLocal();
updateBooksGrid();
};
const removeBook = (e) => {
const title = e.target.parentNode.parentNode.firstChild.innerHTML.replaceAll(
'"',
""
);
library.removeBook(title);
// saveLocal();
updateBooksGrid();
};
const openAddBookModal = () => {
modal.classList.add("active");
overlay.classList.add("active");
};
const closeAddBookModal = () => {
modal.classList.remove("active");
overlay.classList.remove("active");
};
const resetBooksGrid = () => {
booksGrid.innerHTML = "";
};
const updateBooksGrid = () => {
resetBooksGrid();
for (let book of library.books) {
createBookCard(book);
}
};
const addBook = (e) => {
e.preventDefault();
const newBook = getBookFromInput();
library.addBook(newBook);
// saveLocal()
updateBooksGrid();
closeAddBookModal();
};
const getBookFromInput = () => {
const title = document.getElementById("title").value;
const author = document.getElementById("author").value;
const pages = document.getElementById("pages").value;
const isRead = document.getElementById("isRead").checked;
return new Book(title, author, pages, isRead);
};
showModal.addEventListener("click", () => {
openAddBookModal();
});
submitButton.addEventListener("click", (e) => {
addBook(e);
closeAddBookModal();
});