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 ) {
18+ const storedLibrary = localStorage . getItem ( "myLibrary" ) ;
19+ if ( storedLibrary ) {
20+ myLibrary = JSON . parse ( storedLibrary ) ;
21+ } else {
22+ // Initialises default books if storage empty
1023 let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , "252" , true ) ;
1124 let book2 = new Book (
1225 "The Old Man and the Sea" ,
@@ -16,30 +29,37 @@ function populateStorage() {
1629 ) ;
1730 myLibrary . push ( book1 ) ;
1831 myLibrary . push ( book2 ) ;
19- render ( ) ;
32+ saveStorage ( ) ;
2033 }
2134}
2235
23- const title = document . getElementById ( "title" ) ;
24- const author = document . getElementById ( "author" ) ;
25- const pages = document . getElementById ( "pages" ) ;
26- const check = document . getElementById ( "check" ) ;
27-
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- ) {
36+ function saveStorage ( ) {
37+ localStorage . setItem ( "myLibrary" , JSON . stringify ( myLibrary ) ) ;
38+ }
39+
40+ // Validates input and adds new book
41+ function addBook ( e ) {
42+ if ( e ) e . preventDefault ( ) ;
43+
44+ if ( ! titleInput . value || ! authorInput . value || ! pagesInput . value ) {
3745 alert ( "Please fill all fields!" ) ;
3846 return false ;
3947 } else {
40- let book = new Book ( title . value , title . value , pages . value , check . checked ) ;
41- library . push ( book ) ;
48+ let book = new Book (
49+ titleInput . value ,
50+ authorInput . value ,
51+ pagesInput . value ,
52+ readCheckbox . checked
53+ ) ;
54+ myLibrary . push ( book ) ;
55+ saveStorage ( ) ;
4256 render ( ) ;
57+
58+ // Clears inputs
59+ titleInput . value = "" ;
60+ authorInput . value = "" ;
61+ pagesInput . value = "" ;
62+ readCheckbox . checked = false ;
4363 }
4464}
4565
@@ -51,53 +71,56 @@ function Book(title, author, pages, check) {
5171}
5272
5373function 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
74+ // Clears table body efficiently
75+ const tbody = table . querySelector ( "tbody" ) ;
76+ tbody . innerHTML = "" ;
77+
78+ // Inserts updated row and cells
6179 let length = myLibrary . length ;
6280 for ( let i = 0 ; i < length ; i ++ ) {
63- let row = table . insertRow ( 1 ) ;
81+ // Insert at the end of the table
82+ let row = tbody . insertRow ( - 1 ) ;
6483 let titleCell = row . insertCell ( 0 ) ;
6584 let authorCell = row . insertCell ( 1 ) ;
6685 let pagesCell = row . insertCell ( 2 ) ;
6786 let wasReadCell = row . insertCell ( 3 ) ;
6887 let deleteCell = row . insertCell ( 4 ) ;
69- titleCell . innerHTML = myLibrary [ i ] . title ;
70- authorCell . innerHTML = myLibrary [ i ] . author ;
71- pagesCell . innerHTML = myLibrary [ i ] . pages ;
72-
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 ) ;
88+
89+ // Uses textContent to prevent XSS
90+ titleCell . textContent = myLibrary [ i ] . title ;
91+ authorCell . textContent = myLibrary [ i ] . author ;
92+ pagesCell . textContent = myLibrary [ i ] . pages ;
93+
94+ // Toggles read status
95+ let readBtn = document . createElement ( "button" ) ;
96+ readBtn . className = "btn btn-success" ;
97+ wasReadCell . appendChild ( readBtn ) ;
98+
7899 let readStatus = "" ;
79100 if ( myLibrary [ i ] . check == false ) {
80- readStatus = "Yes" ;
81- } else {
82101 readStatus = "No" ;
102+ } else {
103+ readStatus = "Yes" ;
83104 }
84- changeBut . innerText = readStatus ;
105+ readBtn . textContent = readStatus ;
85106
86- changeBut . addEventListener ( "click" , function ( ) {
107+ readBtn . addEventListener ( "click" , function ( ) {
87108 myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
109+ saveStorage ( ) ;
88110 render ( ) ;
89111 } ) ;
90112
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 } ` ) ;
99- myLibrary . splice ( i , 1 ) ;
100- render ( ) ;
113+ // Deletes book
114+ let deleteBtn = document . createElement ( "button" ) ;
115+ deleteCell . appendChild ( deleteBtn ) ;
116+ deleteBtn . className = "btn btn-warning" ;
117+ deleteBtn . textContent = "Delete" ;
118+ deleteBtn . addEventListener ( "click" , function ( ) {
119+ if ( confirm ( `Are you sure you want to delete: ${ myLibrary [ i ] . title } ?` ) ) {
120+ myLibrary . splice ( i , 1 ) ;
121+ saveStorage ( ) ;
122+ render ( ) ;
123+ }
101124 } ) ;
102125 }
103126}
0 commit comments