1+ const bookList = document . querySelector ( '#table-body' ) ;
12let myLibrary = [ ] ;
23
34function 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+
1039function 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-
8077function 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
9697addBookToLibrary ( 'Sapiens: A Brief History of Humankind' , 'Yuval Noah Harari' , '464' , 'read' ) ;
9798addBookToLibrary ( 'The Lady of the Lake' , 'Andrzej Sapkowski' , '544' , 'unread' ) ;
98- showLibraryInfo ( ) ;
99- showBooksInLibrary ( ) ;
100- deleteAllBooks ( ) ;
99+ deleteBooks ( ) ;
0 commit comments