@@ -3,107 +3,92 @@ let myLibrary = [];
33function Book ( title , author , pages , check ) {
44 this . title = title ;
55 this . author = author ;
6- this . pages = pages ;
6+ this . pages = Number ( pages ) ;
77 this . check = check ;
88}
99
10+ const titleInput = document . getElementById ( "title" ) ;
11+ const authorInput = document . getElementById ( "author" ) ;
12+ const pagesInput = document . getElementById ( "pages" ) ;
13+ const checkInput = document . getElementById ( "check" ) ;
14+ const bookForm = document . getElementById ( "demo" ) ;
15+
1016document . addEventListener ( "DOMContentLoaded" , ( ) => {
1117 populateStorage ( ) ;
1218
13- const bookForm = document . getElementById ( "demo" ) ;
14-
15- bookForm . addEventListener ( "submit" , function ( e ) {
16- e . preventDefault ( ) ;
17- submitBook ( ) ;
18- } ) ;
19+ if ( bookForm ) {
20+ bookForm . addEventListener ( "submit" , function ( e ) {
21+ e . preventDefault ( ) ;
22+ processForm ( ) ;
23+ } ) ;
24+ }
1925} ) ;
2026
2127function populateStorage ( ) {
22- if ( myLibrary . length == 0 ) {
23- let book1 = new Book ( "Robinson Crusoe" , "Daniel Defoe" , "252" , true ) ;
24- let book2 = new Book ( "The Old Man and the Sea" , "Ernest Hemingway" , "127" , true ) ;
25- myLibrary . push ( book1 , book2 ) ;
28+ if ( myLibrary . length === 0 ) {
29+
30+ myLibrary . push ( new Book ( "Robinson Crusoe" , "Daniel Defoe" , 252 , true ) ) ;
31+ myLibrary . push (
32+ new Book ( "The Old Man and the Sea" , "Ernest Hemingway" , 127 , true )
33+ ) ;
2634 }
27- render ( ) ;
35+ render ( ) ;
2836}
2937
30- const title = document . getElementById ( "title" ) ;
31- const author = document . getElementById ( "author" ) ;
32- const pages = document . getElementById ( "pages" ) ;
33- const check = document . getElementById ( "check" ) ;
34-
35- //check the right input from forms and if its ok -> add the new book (object in array)
36- //via Book function and start render function
37- function submit ( ) {
38- if (
39- ! title . value ||
40- ! author . value ||
41- ! pages . value ||
42- pages . value <= 0
43- ) {
38+ function processForm ( ) {
39+
40+ const titleVal = titleInput . value . trim ( ) ;
41+ const authorVal = authorInput . value . trim ( ) ;
42+ const pagesVal = pagesInput . value ;
43+
44+ if ( ! titleVal || ! authorVal || pagesVal <= 0 ) {
4445 alert ( "Please fill the field with valid info!" ) ;
45- return false ;
46- } else {
47- let book = new Book ( title . value , author . value , pages . value , check . checked ) ;
48- myLibrary . push ( book ) ;
49- render ( ) ;
46+ return ;
5047 }
51- }
5248
49+ const newBook = new Book ( titleVal , authorVal , pagesVal , checkInput . checked ) ;
50+ myLibrary . push ( newBook ) ;
51+
52+ bookForm . reset ( ) ;
53+ render ( ) ;
54+ $ ( "#demo" ) . collapse ( "hide" ) ;
55+ }
5356
5457function render ( ) {
55- let libraryBody = document . getElementById ( "table-body" ) ;
56-
57- //clean the table before render new one
58- libraryBody . innerHTML = "" ;
59-
60- // //delete old table
61- // for (let n = rowsNumber - 1; n > 0; n--) {
62- // libraryBody.deleteRow(n);
63- // }
64-
65- //insert updated row and cells
66- for ( let i = 0 ; i < myLibrary . length ; i ++ ) {
67- let row = libraryBody . insertRow ( - 1 ) ;
68-
69- let titleCell = row . insertCell ( 0 ) ;
70- let authorCell = row . insertCell ( 1 ) ;
71- let pagesCell = row . insertCell ( 2 ) ;
72- let wasReadCell = row . insertCell ( 3 ) ;
73- let deleteCell = row . insertCell ( 4 ) ;
74-
75- titleCell . textContent = myLibrary [ i ] . title ;
76- authorCell . textContent = myLibrary [ i ] . author ;
77- pagesCell . textContent = myLibrary [ i ] . pages ;
78-
79- //add and wait for action for read/unread button
80- let changeBut = document . createElement ( "button" ) ;
81- changeBut . id = i ;
82- changeBut . className = "btn btn-success" ;
83- wasReadCell . appendChild ( changeBut ) ;
84- let readStatus = "" ;
85- if ( myLibrary [ i ] . check == false ) {
86- readStatus = "No" ;
87- } else {
88- readStatus = "Yes" ;
89- }
90- changeBut . innerText = readStatus ;
91-
92- changeBut . addEventListener ( "click" , function ( ) {
93- myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
58+ const libraryBody = document . getElementById ( "table-body" ) ;
59+ libraryBody . innerHTML = "" ;
60+
61+ myLibrary . forEach ( ( book , index ) => {
62+ const row = libraryBody . insertRow ( - 1 ) ;
63+
64+ row . insertCell ( 0 ) . textContent = book . title ;
65+ row . insertCell ( 1 ) . textContent = book . author ;
66+ row . insertCell ( 2 ) . textContent = book . pages ;
67+
68+ const wasReadCell = row . insertCell ( 3 ) ;
69+ const statusBtn = document . createElement ( "button" ) ;
70+ statusBtn . className = "btn btn-success" ;
71+ statusBtn . textContent = book . check ? "Yes" : "No" ;
72+
73+ statusBtn . addEventListener ( "click" , ( ) => {
74+ book . check = ! book . check ;
9475 render ( ) ;
9576 } ) ;
77+ wasReadCell . appendChild ( statusBtn ) ;
9678
97- //add delete button to every row and render again
98- let deleteBtn = document . createElement ( "button" ) ;
99- deleteBtn . id = i + 5 ;
100- deleteCell . appendChild ( deleteBtn ) ;
79+
80+ const deleteCell = row . insertCell ( 4 ) ;
81+ const deleteBtn = document . createElement ( "button" ) ;
10182 deleteBtn . className = "btn btn-warning" ;
102- deleteBtn . innerHTML = "Delete" ;
103- deleteBtn . addEventListener ( "click" , function ( index ) {
104- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
83+ deleteBtn . textContent = "Delete" ;
84+
85+ deleteBtn . addEventListener ( "click" , ( ) => {
86+ const deletedTitle = book . title ;
10587 myLibrary . splice ( index , 1 ) ;
10688 render ( ) ;
89+
90+ alert ( `You've deleted title: ${ deletedTitle } ` ) ;
10791 } ) ;
108- }
92+ deleteCell . appendChild ( deleteBtn ) ;
93+ } ) ;
10994}
0 commit comments