1+ const titleInput = document . getElementById ( "title" ) ;
2+ const authorInput = document . getElementById ( "author" ) ;
3+ const pagesInput = document . getElementById ( "pages" ) ;
4+ const readCheckbox = document . getElementById ( "check" ) ;
5+ const table = document . getElementById ( "display" ) ;
6+ const submitBtn = document . getElementById ( "submit-book-btn" ) ;
7+
18let myLibrary = [ ] ;
29
3- window . addEventListener ( "load" , function ( e ) {
10+ window . addEventListener ( "load" , function ( ) {
411 populateStorage ( ) ;
512 render ( ) ;
613} ) ;
714
15+ submitBtn . addEventListener ( "click" , addBook ) ;
16+
817function populateStorage ( ) {
9- if ( myLibrary . length == 0 ) {
10- let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , "252" , true ) ;
11- let book2 = new Book (
12- "The Old Man and the Sea" ,
13- "Ernest Hemingway" ,
14- "127" ,
15- true
16- ) ;
17- myLibrary . push ( book1 ) ;
18- myLibrary . push ( book2 ) ;
19- render ( ) ;
18+ const storedLibrary = localStorage . getItem ( "myLibrary" ) ;
19+ if ( storedLibrary ) {
20+ myLibrary = JSON . parse ( storedLibrary ) ;
2021 }
2122}
2223
23- const title = document . getElementById ( "title" ) ;
24- const author = document . getElementById ( "author" ) ;
25- const pages = document . getElementById ( "pages" ) ;
26- const check = document . getElementById ( "check" ) ;
24+ function saveStorage ( ) {
25+ localStorage . setItem ( "myLibrary" , JSON . stringify ( myLibrary ) ) ;
26+ }
27+
28+ // Validates input and adds new book
29+ function addBook ( e ) {
30+ if ( e ) e . preventDefault ( ) ;
2731
28- //check the right input from forms and if its ok -> add the new book (object in array)
29- //via Book function and start render function
30- function submit ( ) {
31- if (
32- title . value == null ||
33- title . value == "" ||
34- pages . value == null ||
35- pages . value == ""
36- ) {
32+ if ( ! titleInput . value || ! authorInput . value || ! pagesInput . value ) {
3733 alert ( "Please fill all fields!" ) ;
3834 return false ;
39- } else {
40- let book = new Book ( title . value , title . value , pages . value , check . checked ) ;
41- library . push ( book ) ;
42- render ( ) ;
4335 }
36+ let book = new Book (
37+ titleInput . value ,
38+ authorInput . value ,
39+ pagesInput . value ,
40+ readCheckbox . checked
41+ ) ;
42+ myLibrary . push ( book ) ;
43+ saveStorage ( ) ;
44+ render ( ) ;
4445}
4546
4647function Book ( title , author , pages , check ) {
@@ -51,52 +52,51 @@ function Book(title, author, pages, check) {
5152}
5253
5354function render ( ) {
54- let table = document . getElementById ( "display" ) ;
55- let rowsNumber = table . rows . length ;
56- //delete old table
57- for ( let n = rowsNumber - 1 ; n > 0 ; n -- {
58- table . deleteRow ( n ) ;
59- }
60- //insert updated row and cells
55+ // Clears table body efficiently
56+ const tbody = table . querySelector ( "tbody" ) ;
57+ tbody . innerHTML = "" ;
58+
59+ // Inserts updated row and cells
6160 let length = myLibrary . length ;
6261 for ( let i = 0 ; i < length ; i ++ ) {
63- let row = table . insertRow ( 1 ) ;
62+ // Insert at the end of the table
63+ let row = tbody . insertRow ( - 1 ) ;
6464 let titleCell = row . insertCell ( 0 ) ;
6565 let authorCell = row . insertCell ( 1 ) ;
6666 let pagesCell = row . insertCell ( 2 ) ;
6767 let wasReadCell = row . insertCell ( 3 ) ;
6868 let deleteCell = row . insertCell ( 4 ) ;
69- titleCell . innerHTML = myLibrary [ i ] . title ;
70- authorCell . innerHTML = myLibrary [ i ] . author ;
71- pagesCell . innerHTML = myLibrary [ i ] . pages ;
7269
73- //add and wait for action for read/unread button
74- let changeBut = document . createElement ( "button" ) ;
75- changeBut . id = i ;
76- changeBut . className = "btn btn-success" ;
77- wasReadCell . appendChild ( changeBut ) ;
70+ // Uses textContent to prevent XSS
71+ titleCell . textContent = myLibrary [ i ] . title ;
72+ authorCell . textContent = myLibrary [ i ] . author ;
73+ pagesCell . textContent = myLibrary [ i ] . pages ;
74+
75+ // Toggles read status
76+ let readBtn = document . createElement ( "button" ) ;
77+ wasReadCell . appendChild ( readBtn ) ;
78+
7879 let readStatus = "" ;
79- if ( myLibrary [ i ] . check == false ) {
80- readStatus = "Yes" ;
81- } else {
80+ if ( myLibrary [ i ] . check === false ) {
8281 readStatus = "No" ;
82+ } else {
83+ readStatus = "Yes" ;
8384 }
84- changeBut . innerText = readStatus ;
85+ readBtn . textContent = readStatus ;
8586
86- changeBut . addEventListener ( "click" , function ( ) {
87+ readBtn . addEventListener ( "click" , function ( ) {
8788 myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
89+ saveStorage ( ) ;
8890 render ( ) ;
8991 } ) ;
9092
91- //add delete button to every row and render again
92- let delButton = document . createElement ( "button" ) ;
93- delBut . id = i + 5 ;
94- deleteCell . appendChild ( delBut ) ;
95- delBut . className = "btn btn-warning" ;
96- delBut . innerHTML = "Delete" ;
97- delBut . addEventListener ( "clicks" , function ( ) {
98- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
93+ // Deletes book
94+ let deleteBtn = document . createElement ( "button" ) ;
95+ deleteCell . appendChild ( deleteBtn ) ;
96+ deleteBtn . textContent = "Delete" ;
97+ deleteBtn . addEventListener ( "click" , function ( ) {
9998 myLibrary . splice ( i , 1 ) ;
99+ saveStorage ( ) ;
100100 render ( ) ;
101101 } ) ;
102102 }
0 commit comments