@@ -2,16 +2,15 @@ let myLibrary = [];
22
33window . addEventListener ( "load" , function ( e ) {
44 populateStorage ( ) ;
5- render ( ) ;
65} ) ;
76
87function populateStorage ( ) {
98 if ( myLibrary . length == 0 ) {
10- let book1 = new Book ( "Robinson Crusoe" , "Daniel Defoe" , " 252" , true ) ;
9+ let book1 = new Book ( "Robinson Crusoe" , "Daniel Defoe" , 252 , true ) ;
1110 let book2 = new Book (
1211 "The Old Man and the Sea" ,
1312 "Ernest Hemingway" ,
14- " 127" ,
13+ 127 ,
1514 true
1615 ) ;
1716 myLibrary . push ( book1 ) ;
@@ -20,91 +19,87 @@ function populateStorage() {
2019 }
2120}
2221
23- const title = document . getElementById ( "title" ) ;
24- const author = document . getElementById ( "author" ) ;
25- const pages = document . getElementById ( "pages" ) ;
26- const check = document . getElementById ( "check" ) ;
22+ const titleInput = document . getElementById ( "title" ) ;
23+ const authorInput = document . getElementById ( "author" ) ;
24+ const pagesInput = document . getElementById ( "pages" ) ;
25+ const checkInput = document . getElementById ( "check" ) ;
2726
2827//check the right input from forms and if its ok -> add the new book (object in array)
2928//via Book function and start render function
30- function submit ( ) {
29+ function handleSubmit ( event ) {
30+ event . preventDefault ( ) ;
3131 if (
32- ! title . value . trim ( ) ||
33- ! author . value . trim ( ) ||
34- ! pages . value
32+ ! titleInput . value . trim ( ) ||
33+ ! authorInput . value . trim ( ) ||
34+ pagesInput . value <= 0
3535 ) {
3636 alert ( "Please fill all fields!" ) ;
3737 return false ;
3838 } else {
39- let book = new Book ( title . value , author . value , pages . value , check . checked ) ;
39+ let book = new Book ( String ( titleInput . value ) , String ( authorInput . value ) , Number ( pagesInput . value ) , checkInput . checked ) ;
4040 myLibrary . push ( book ) ;
4141 resetAddNewBook ( )
4242 render ( ) ;
4343 }
4444}
4545
4646function resetAddNewBook ( ) {
47- title . value = '' ;
48- author . value = '' ;
49- pages . value = '' ;
50- check . checked = false ;
47+ // titleInput.value = '';
48+ // authorInput.value = '';
49+ // pagesInput.value = 0;
50+ // checkInput.checked = false;
51+ document . getElementById ( "book-form" ) . reset ( )
5152}
5253
53- function Book ( title , author , pages , check ) {
54+ class Book {
55+ constructor ( title , author , pages , check ) {
5456 this . title = title ;
5557 this . author = author ;
5658 this . pages = pages ;
5759 this . check = check ;
60+ }
5861}
5962
6063function render ( ) {
61- let table = document . getElementById ( "display" ) ;
62- let rowsNumber = table . rows . length ;
6364 //delete old table
64- for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) {
65- table . deleteRow ( n ) ;
66- }
65+ let tableBody = document . getElementById ( "display-body" )
66+ tableBody . innerHTML = '' ;
6767 //insert updated row and cells
68- let length = myLibrary . length ;
69- for ( let libraryBookIndex = 0 ; libraryBookIndex < length ; libraryBookIndex ++ ) {
70- let row = table . insertRow ( - 1 ) ;
68+ myLibrary . forEach ( ( book , index ) => {
69+ let row = tableBody . insertRow ( - 1 ) ;
7170 let titleCell = row . insertCell ( 0 ) ;
7271 let authorCell = row . insertCell ( 1 ) ;
7372 let pagesCell = row . insertCell ( 2 ) ;
7473 let wasReadCell = row . insertCell ( 3 ) ;
7574 let deleteCell = row . insertCell ( 4 ) ;
76- titleCell . innerHTML = myLibrary [ libraryBookIndex ] . title ;
77- authorCell . innerHTML = myLibrary [ libraryBookIndex ] . author ;
78- pagesCell . innerHTML = myLibrary [ libraryBookIndex ] . pages ;
75+ titleCell . innerText = book . title ;
76+ authorCell . innerText = book . author ;
77+ pagesCell . innerText = book . pages ;
7978
80- //add and wait for action for read/unread button
81- let readOrUnreadButton = document . createElement ( "button" ) ;
82- readOrUnreadButton . id = `readOrUnreadBtn${ libraryBookIndex } ` ;
83- readOrUnreadButton . className = "btn btn-success" ;
84- wasReadCell . appendChild ( readOrUnreadButton ) ;
85- let readStatus = "" ;
86- if ( myLibrary [ libraryBookIndex ] . check ) {
87- readStatus = "Yes" ;
88- } else {
89- readStatus = "No" ;
90- }
91- readOrUnreadButton . innerText = readStatus ;
79+ //add and wait for action for readToggle button
80+ let readToggleButton = document . createElement ( "button" ) ;
81+ readToggleButton . id = `readOrUnreadBtn${ index } ` ;
82+ readToggleButton . className = "btn btn-success" ;
83+ wasReadCell . appendChild ( readToggleButton ) ;
84+
85+ readToggleButton . innerText = book . check ? "Yes" : "No" ;
9286
93- readOrUnreadButton . addEventListener ( "click" , function ( ) {
94- myLibrary [ libraryBookIndex ] . check = ! myLibrary [ libraryBookIndex ] . check ;
87+ readToggleButton . addEventListener ( "click" , function ( ) {
88+ book . check = ! book . check ;
9589 render ( ) ;
9690 } ) ;
9791
9892 //add delete button to every row and render again
99- let delButton = document . createElement ( "button" ) ;
100- delButton . id = `delBtn${ libraryBookIndex } ` ;
101- deleteCell . appendChild ( delButton ) ;
102- delButton . className = "btn btn-warning" ;
103- delButton . innerHTML = "Delete" ;
104- delButton . addEventListener ( "click" , ( ) => {
105- alert ( `You've deleted title: ${ myLibrary [ libraryBookIndex ] . title } ` ) ;
106- myLibrary . splice ( libraryBookIndex , 1 ) ;
107- render ( ) ;
93+ let deleteButton = document . createElement ( "button" ) ;
94+ deleteButton . id = `delBtn${ index } ` ;
95+ deleteCell . appendChild ( deleteButton ) ;
96+ deleteButton . className = "btn btn-warning" ;
97+ deleteButton . innerText = "Delete" ;
98+ deleteButton . addEventListener ( "click" , ( ) => {
99+ if ( confirm ( `You've deleted title: ${ book . title } ` ) ) {
100+ myLibrary . splice ( index , 1 ) ;
101+ render ( ) ;
102+ }
108103 } ) ;
109- }
104+ } )
110105}
0 commit comments