@@ -20,26 +20,36 @@ function populateStorage() {
2020 }
2121}
2222
23- const title = document . getElementById ( "title" ) ;
24- const author = document . getElementById ( "author" ) ;
25- const pages = document . getElementById ( "pages" ) ;
26- const check = document . getElementById ( "check" ) ;
23+ const bookTitle = document . getElementById ( "title" ) ;
24+ const authorOfBook = document . getElementById ( "author" ) ;
25+ const NoOfpages = document . getElementById ( "pages" ) ;
26+ const readBook = document . getElementById ( "check" ) ;
2727
2828//check the right input from forms and if its ok -> add the new book (object in array)
2929//via Book function and start render function
3030function submit ( ) {
3131 if (
32- title . value == null ||
33- title . value == "" ||
34- pages . value == null ||
35- pages . value == "" ||
36- author . value == null ||
37- author . value == ""
32+ bookTitle . value . trim ( ) === "" ||
33+ NoOfpages . value . trim ( ) === "" ||
34+ authorOfBook . value . trim ( ) === ""
3835 ) {
3936 alert ( "Please fill all fields!" ) ;
4037 return false ;
38+ }
39+
40+ if (
41+ ! / ^ [ A - Z a - z \s ] + $ / . test ( authorOfBook . value ) ||
42+ ! / ^ [ A - Z a - z \s ] + $ / . test ( bookTitle . value )
43+ ) {
44+ alert ( "Author and book title must contain only letters" ) ;
45+ return false ;
4146 } else {
42- let book = new Book ( title . value , author . value , pages . value , check . checked ) ;
47+ let book = new Book (
48+ bookTitle . value ,
49+ authorOfBook . value ,
50+ NoOfpages . value ,
51+ readBook . checked
52+ ) ;
4353 myLibrary . push ( book ) ;
4454 render ( ) ;
4555 }
@@ -48,11 +58,11 @@ function submit() {
4858const submitInput = document . getElementById ( "submit" ) ;
4959submitInput . addEventListener ( "click" , submit ) ;
5060
51- function Book ( title , author , pages , check ) {
52- this . title = title ;
53- this . author = author ;
54- this . pages = pages ;
55- this . check = check ;
61+ function Book ( bookTitle , authorOfBook , NoOfpages , readBook ) {
62+ this . bookTitle = bookTitle ;
63+ this . authorOfBook = authorOfBook ;
64+ this . NoOfpages = NoOfpages ;
65+ this . readBook = readBook ;
5666}
5767
5868function render ( ) {
@@ -71,39 +81,34 @@ function render() {
7181 let pagesCell = row . insertCell ( 2 ) ;
7282 let wasReadCell = row . insertCell ( 3 ) ;
7383 let deleteCell = row . insertCell ( 4 ) ;
74- titleCell . innerHTML = myLibrary [ i ] . title ;
75- authorCell . innerHTML = myLibrary [ i ] . author ;
76- pagesCell . innerHTML = myLibrary [ i ] . pages ;
77-
78- //add and wait for action for read/unread button
79- let changeBut = document . createElement ( "button" ) ;
80- changeBut . id = i ;
81- changeBut . className = "btn btn-success" ;
82- wasReadCell . appendChild ( changeBut ) ;
83- let readStatus = "" ;
84- if ( myLibrary [ i ] . check == true ) {
85- readStatus = "Yes" ;
86- } else {
87- readStatus = "No" ;
88- }
84+ titleCell . innerHTML = myLibrary [ i ] . bookTitle ;
85+ authorCell . innerHTML = myLibrary [ i ] . authorOfBook ;
86+ pagesCell . innerHTML = myLibrary [ i ] . NoOfpages ;
8987
90- changeBut . innerText = readStatus ;
88+ let changeBtn = document . createElement ( "button" ) ;
89+ changeBtn . id = i ;
90+ changeBtn . className = "btn btn-success" ;
91+ wasReadCell . appendChild ( changeBtn ) ;
92+ changeBtn . textContent = myLibrary [ i ] . readBook ? "Yes" : "No" ;
9193
92- changeBut . addEventListener ( "click" , function ( ) {
93- myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
94+ changeBtn . addEventListener ( "click" , function ( ) {
95+ myLibrary [ i ] . readBook = ! myLibrary [ i ] . readBook ;
9496 render ( ) ;
9597 } ) ;
9698
9799 //add delete button to every row and render again
98100
99101 let delButton = document . createElement ( "button" ) ;
100- delButton . id = i ;
102+ // delButton.id = i;
101103
102104 deleteCell . appendChild ( delButton ) ;
103105 delButton . className = "btn btn-warning" ;
104106 delButton . innerHTML = "Delete" ;
105107 delButton . addEventListener ( "click" , function ( ) {
106- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
108+ const toast = document . createElement ( "div" ) ;
109+ toast . textContent = `You've deleted title: ${ myLibrary [ i ] . bookTitle } ` ;
110+ table . appendChild ( toast ) ;
111+ setTimeout ( ( ) => toast . remove ( ) , 2000 ) ;
107112 myLibrary . splice ( i , 1 ) ;
108113 render ( ) ;
109114 } ) ;
0 commit comments