Skip to content

Commit c1bde71

Browse files
committed
Add functions to delete one/all books, improve functions showing books and statistics
1 parent 71fbd26 commit c1bde71

File tree

3 files changed

+58
-61
lines changed

3 files changed

+58
-61
lines changed

css/styles.css

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,10 @@ input[type=number] {
232232
.book-info {
233233
border-bottom: 2px dotted var(--blue);
234234
}
235-
.book-info button {
236-
border: none;
235+
.book-info i {
237236
font-size: 20px;
238-
background-color: transparent;
239-
padding: 0;
240237
}
241-
.book-info button:hover {
238+
.book-info i:hover {
242239
cursor: pointer;
243240
}
244241
.fa-check {

index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ <h1 id="logo"><i class="fad fa-books theme-books"></i> Library</i></h1>
7676

7777
<main id="library-content">
7878
<div id="library-info">
79-
<p>BOOKS READ: <span id="books-read"></span></p>
80-
<p>BOOKS UNREAD: <span id="books-unread"></span></p>
81-
<p>TOTAL BOOKS: <span id="total-books"></span></p>
79+
<p>BOOKS READ: <span id="books-read">0</span></p>
80+
<p>BOOKS UNREAD: <span id="books-unread">0</span></p>
81+
<p>TOTAL BOOKS: <span id="total-books">0</span></p>
8282
<button type="button" id="delete-all-btn">DELETE ALL</button>
8383
</div>
8484

@@ -96,6 +96,7 @@ <h1 id="logo"><i class="fad fa-books theme-books"></i> Library</i></h1>
9696
<!-- HERE IS BOOK LIST GENERATED BY JS -->
9797
</tbody>
9898
</table>
99+
99100
</main>
100101

101102
<script type="text/javascript" src="scripts.js"></script>

scripts.js

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const bookList = document.querySelector('#table-body');
12
let myLibrary = [];
23

34
function Books(title, author, pages, status) {
@@ -7,94 +8,92 @@ function Books(title, author, pages, status) {
78
this.status = status;
89
}
910

11+
function showLibraryInfo() {
12+
const booksRead = document.querySelector('#books-read');
13+
const booksUnread = document.querySelector('#books-unread');
14+
const totalBooks = document.querySelector('#total-books');
15+
let unreadCounter = 0;
16+
let readCounter = 0;
17+
if (myLibrary.length === 0) {
18+
booksUnread.textContent = '0';
19+
booksRead.textContent = '0';
20+
} else {
21+
for (let i = 0; i < myLibrary.length; i += 1) {
22+
if (myLibrary.length === 1 && myLibrary[i].status === 'unread') {
23+
booksRead.textContent = '0';
24+
} else {
25+
booksUnread.textContent = '0';
26+
}
27+
if (myLibrary[i].status === 'unread') {
28+
unreadCounter += 1;
29+
booksUnread.textContent = unreadCounter;
30+
} else {
31+
readCounter += 1;
32+
booksRead.textContent = readCounter;
33+
}
34+
}
35+
}
36+
totalBooks.textContent = myLibrary.length;
37+
}
38+
1039
function showBooksInLibrary() {
11-
const bookList = document.querySelector('#table-body');
40+
showLibraryInfo();
1241
bookList.textContent = '';
1342
for (let i = 0; i < myLibrary.length; i += 1) {
1443
const bookRow = document.createElement('tr');
1544
bookRow.classList.add('book-info');
1645
bookList.appendChild(bookRow);
17-
46+
// BOOK TITLE
1847
const bookTitle = document.createElement('td');
1948
bookTitle.textContent = myLibrary[i].title;
20-
bookTitle.classList.add('book-title');
2149
bookRow.appendChild(bookTitle);
22-
50+
// BOOK AUTHOR
2351
const bookAuthor = document.createElement('td');
2452
bookAuthor.textContent = myLibrary[i].author;
25-
bookAuthor.classList.add('book-author');
2653
bookRow.appendChild(bookAuthor);
27-
54+
// BOOK PAGES
2855
const bookPages = document.createElement('td');
2956
bookPages.textContent = myLibrary[i].pages;
30-
bookPages.classList.add('book-pages');
3157
bookRow.appendChild(bookPages);
32-
58+
// BOOK STATUS BUTTON
3359
const bookStatus = document.createElement('td');
34-
const statusButton = document.createElement('button');
3560
const statusSymbol = document.createElement('i');
36-
if (myLibrary[i].status === 'read') {
37-
statusSymbol.classList.add('fas', 'fa-check');
38-
} else if (myLibrary[i].status === 'unread') {
61+
if (myLibrary[i].status === 'unread') {
3962
statusSymbol.classList.add('fas', 'fa-times');
63+
} else {
64+
statusSymbol.classList.add('fas', 'fa-check');
4065
}
41-
bookStatus.classList.add('book-status');
42-
statusButton.appendChild(statusSymbol);
43-
bookStatus.appendChild(statusButton);
66+
bookStatus.appendChild(statusSymbol);
4467
bookRow.appendChild(bookStatus);
45-
68+
// BOOK REMOVAL BUTTON
4669
const bookDelete = document.createElement('td');
47-
const deleteButton = document.createElement('button');
4870
const deleteSymbol = document.createElement('i');
4971
deleteSymbol.classList.add('fas', 'fa-trash-alt');
50-
deleteButton.appendChild(deleteSymbol);
51-
bookDelete.classList.add('book-delete');
52-
bookDelete.appendChild(deleteButton);
72+
bookDelete.appendChild(deleteSymbol);
5373
bookRow.appendChild(bookDelete);
5474
}
5575
}
5676

57-
function showLibraryInfo() {
58-
const booksRead = document.querySelector('#books-read');
59-
const booksUnread = document.querySelector('#books-unread');
60-
const totalBooks = document.querySelector('#total-books');
61-
let readCounter = 0;
62-
let unreadCounter = 0;
63-
if (myLibrary.length === 0) {
64-
booksRead.textContent = '0';
65-
booksUnread.textContent = '0';
66-
} else {
67-
for (let i = 0; i < myLibrary.length; i += 1) {
68-
if (myLibrary[i].status === 'read') {
69-
readCounter += 1;
70-
booksRead.textContent = readCounter;
71-
} else if (myLibrary[i].status === 'unread') {
72-
unreadCounter += 1;
73-
booksUnread.textContent = unreadCounter;
74-
}
75-
}
76-
}
77-
totalBooks.textContent = myLibrary.length;
78-
}
79-
8077
function addBookToLibrary(title, author, pages, status) {
8178
const book = new Books(title, author, pages, status);
8279
myLibrary.push(book);
83-
showLibraryInfo();
8480
showBooksInLibrary();
8581
}
8682

87-
function deleteAllBooks() {
88-
const deleteAllBtn = document.querySelector('#delete-all-btn');
89-
deleteAllBtn.addEventListener('click', () => {
90-
myLibrary = [];
91-
showBooksInLibrary();
92-
showLibraryInfo();
83+
function deleteBooks() {
84+
document.addEventListener('click', (event) => {
85+
const { target } = event;
86+
if (target.id === 'delete-all-btn') {
87+
myLibrary = [];
88+
showBooksInLibrary();
89+
} else if (target.classList.contains('fa-trash-alt')) {
90+
const tr = target.parentNode.parentNode.rowIndex - 1;
91+
myLibrary.splice(tr, 1);
92+
showBooksInLibrary();
93+
}
9394
});
9495
}
9596

9697
addBookToLibrary('Sapiens: A Brief History of Humankind', 'Yuval Noah Harari', '464', 'read');
9798
addBookToLibrary('The Lady of the Lake', 'Andrzej Sapkowski', '544', 'unread');
98-
showLibraryInfo();
99-
showBooksInLibrary();
100-
deleteAllBooks();
99+
deleteBooks();

0 commit comments

Comments
 (0)