Skip to content

Commit 71fbd26

Browse files
committed
Add button to delete all books
1 parent 9ffd3dd commit 71fbd26

File tree

3 files changed

+85
-32
lines changed

3 files changed

+85
-32
lines changed

css/styles.css

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
--blue: #1F3E59;
1616
--dark-blue: #1A3246;
1717
--green: #478647;
18+
--dark-green: #337433;
1819
--red: #D05050;
20+
--dark-red: #BE3E3E;
1921
}
2022

2123
#content {
@@ -146,6 +148,7 @@ input[type=number] {
146148
}
147149
#add-book:hover {
148150
cursor: pointer;
151+
border: 1px solid var(--dark-blue);
149152
background-color: var(--dark-blue);
150153
transition: 0.5s;
151154
}
@@ -190,13 +193,30 @@ input[type=number] {
190193
}
191194
#library-info {
192195
display: flex;
193-
justify-content: space-evenly;
196+
justify-content: space-around;
194197
width: 70%;
198+
margin-bottom: 30px;
199+
padding: 0 25px;
200+
font-size: 18px;
195201
}
196-
#library-info p {
197-
font-size: 20px;
202+
#library-info p,
203+
#delete-all-btn {
198204
font-weight: 900;
199205
}
206+
#delete-all-btn {
207+
width: 150px;
208+
border: 1px solid var(--red);
209+
border-radius: 2px;
210+
background-color: var(--red);
211+
color: var(--white);
212+
transition: 0.5s;
213+
}
214+
#delete-all-btn:hover {
215+
border: 1px solid var(--dark-red);
216+
background-color: var(--dark-red);
217+
cursor: pointer;
218+
}
219+
200220
#books-list {
201221
width: 70%;
202222
border-collapse: collapse;
@@ -223,8 +243,17 @@ input[type=number] {
223243
}
224244
.fa-check {
225245
color: var(--green);
246+
transition: 0.5s;
247+
}
248+
.fa-check:hover {
249+
color: var(--dark-green);
226250
}
227251
.fa-times,
228252
.fa-trash-alt {
229253
color: var(--red);
254+
transition: 0.5s;
255+
}
256+
.fa-times:hover,
257+
.fa-trash-alt:hover {
258+
color: var(--dark-red);
230259
}

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ <h1 id="logo"><i class="fad fa-books theme-books"></i> Library</i></h1>
7979
<p>BOOKS READ: <span id="books-read"></span></p>
8080
<p>BOOKS UNREAD: <span id="books-unread"></span></p>
8181
<p>TOTAL BOOKS: <span id="total-books"></span></p>
82+
<button type="button" id="delete-all-btn">DELETE ALL</button>
8283
</div>
8384

8485
<table id="books-list">

scripts.js

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const myLibrary = [];
1+
let myLibrary = [];
22

33
function Books(title, author, pages, status) {
44
this.title = title;
@@ -7,50 +7,27 @@ function Books(title, author, pages, status) {
77
this.status = status;
88
}
99

10-
function addBookToLibrary(title, author, pages, status) {
11-
const book = new Books(title, author, pages, status);
12-
myLibrary.push(book);
13-
return myLibrary;
14-
}
15-
addBookToLibrary('Sapiens: A Brief History of Humankind', 'Yuval Noah Harari', '464', 'read');
16-
addBookToLibrary('The Lady of the Lake', 'Andrzej Sapkowski', '544', 'unread');
17-
18-
function showLibraryInfo() {
19-
let readCounter = 0;
20-
let unreadCounter = 0;
21-
for (let i = 0; i < myLibrary.length; i += 1) {
22-
if (myLibrary[i].status === 'read') {
23-
readCounter += 1;
24-
const booksRead = document.querySelector('#books-read');
25-
booksRead.textContent = readCounter;
26-
} else if (myLibrary[i].status === 'unread') {
27-
unreadCounter += 1;
28-
const booksUnread = document.querySelector('#books-unread');
29-
booksUnread.textContent = unreadCounter;
30-
}
31-
}
32-
const totalBooks = document.querySelector('#total-books');
33-
totalBooks.textContent = myLibrary.length;
34-
}
35-
showLibraryInfo();
36-
3710
function showBooksInLibrary() {
11+
const bookList = document.querySelector('#table-body');
12+
bookList.textContent = '';
3813
for (let i = 0; i < myLibrary.length; i += 1) {
39-
const bookList = document.querySelector('#table-body');
4014
const bookRow = document.createElement('tr');
4115
bookRow.classList.add('book-info');
4216
bookList.appendChild(bookRow);
4317

4418
const bookTitle = document.createElement('td');
4519
bookTitle.textContent = myLibrary[i].title;
20+
bookTitle.classList.add('book-title');
4621
bookRow.appendChild(bookTitle);
4722

4823
const bookAuthor = document.createElement('td');
4924
bookAuthor.textContent = myLibrary[i].author;
25+
bookAuthor.classList.add('book-author');
5026
bookRow.appendChild(bookAuthor);
5127

5228
const bookPages = document.createElement('td');
5329
bookPages.textContent = myLibrary[i].pages;
30+
bookPages.classList.add('book-pages');
5431
bookRow.appendChild(bookPages);
5532

5633
const bookStatus = document.createElement('td');
@@ -61,6 +38,7 @@ function showBooksInLibrary() {
6138
} else if (myLibrary[i].status === 'unread') {
6239
statusSymbol.classList.add('fas', 'fa-times');
6340
}
41+
bookStatus.classList.add('book-status');
6442
statusButton.appendChild(statusSymbol);
6543
bookStatus.appendChild(statusButton);
6644
bookRow.appendChild(bookStatus);
@@ -70,8 +48,53 @@ function showBooksInLibrary() {
7048
const deleteSymbol = document.createElement('i');
7149
deleteSymbol.classList.add('fas', 'fa-trash-alt');
7250
deleteButton.appendChild(deleteSymbol);
51+
bookDelete.classList.add('book-delete');
7352
bookDelete.appendChild(deleteButton);
7453
bookRow.appendChild(bookDelete);
7554
}
7655
}
56+
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+
80+
function addBookToLibrary(title, author, pages, status) {
81+
const book = new Books(title, author, pages, status);
82+
myLibrary.push(book);
83+
showLibraryInfo();
84+
showBooksInLibrary();
85+
}
86+
87+
function deleteAllBooks() {
88+
const deleteAllBtn = document.querySelector('#delete-all-btn');
89+
deleteAllBtn.addEventListener('click', () => {
90+
myLibrary = [];
91+
showBooksInLibrary();
92+
showLibraryInfo();
93+
});
94+
}
95+
96+
addBookToLibrary('Sapiens: A Brief History of Humankind', 'Yuval Noah Harari', '464', 'read');
97+
addBookToLibrary('The Lady of the Lake', 'Andrzej Sapkowski', '544', 'unread');
98+
showLibraryInfo();
7799
showBooksInLibrary();
100+
deleteAllBooks();

0 commit comments

Comments
 (0)