@@ -5,54 +5,74 @@ const titleInput = document.getElementById("title");
55const authorInput = document . getElementById ( "author" ) ;
66const pagesInput = document . getElementById ( "pages" ) ;
77const checkInput = document . getElementById ( "check" ) ;
8+ const addBookForm = document . getElementById ( "addBookForm" ) ;
9+
810// populate the library with some default books if it's empty
911function populateStorage ( ) {
1012 if ( myLibrary . length === 0 ) {
1113 let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , 252 , true ) ;
12- let book2 = new Book ( "The Old Man and the Sea" , "Ernest Hemingway" , 127 , true ) ;
14+ let book2 = new Book ( "The Old Man and the Sea" , "Ernest Hemingway" , 127 , true ) ;
1315 myLibrary . push ( book1 ) ;
1416 myLibrary . push ( book2 ) ;
15- }
16- }
17- //check the right input from forms and if its ok -> add the new book (object in array)
18- //via Book function and start render function
19- function submit ( ) {
20- if (
21- titleInput . value . trim == "" || // cant be null so removed all that
22- authorInput . value . trim == "" ||
23- pagesInput . value . trim == ""
24- ) {
25- alert ( "Please fill all fields!" ) ;
26- return false ;
27- } else {
28- let newBook = new Book ( titleInput . value , authorInput . value , parseInt ( pagesInput . value ) , checkInput . checked ) ;
29- myLibrary . push ( newBook ) ;
30- //clear form after adding newBook
31- titleInput . value = '' ;
32- authorInput . value = '' ;
33- pagesInput . value = '' ;
34- checkInput . value = false ;
35- render ( ) ;
3617 }
3718}
19+
20+ // this function is now exclusively for adding a new book to the array and rendering, replaced the old submit()
21+ function addBookToLibrary ( ) {
22+ let newBook = new Book (
23+ titleInput . value ,
24+ authorInput . value ,
25+ parseInt ( pagesInput . value ) ,
26+ checkInput . checked
27+ ) ;
28+ myLibrary . push ( newBook ) ;
29+ addBookForm . reset ( ) ;
30+ render ( ) ;
31+ }
32+
33+ addBookForm . addEventListener ( 'submit' , function ( event ) {
34+ event . preventDefault ( ) ; // prevents the default page reload
35+ addBookToLibrary ( ) ;
36+ } ) ;
37+
3838// book constructor
3939function Book ( title , author , pages , check ) {
4040 this . title = title ;
4141 this . author = author ;
4242 this . pages = pages ;
4343 this . check = check ;
4444}
45- // renders lib data into HTML
45+
46+ function displayNotification ( message ) {
47+ const container = document . getElementById ( "notification-container" ) ;
48+ container . innerHTML = `<div class="alert alert-success alert-dismissible fade show" role="alert">
49+ ${ message }
50+ <button type="button" class="close" data-dismiss="alert" aria-label="Close">
51+ <span aria-hidden="true">×</span>
52+ </button>
53+ </div>` ;
54+ setTimeout ( ( ) => {
55+ const alert = container . querySelector ( '.alert' ) ;
56+ if ( alert ) {
57+ alert . classList . remove ( 'show' ) ;
58+ alert . classList . add ( 'fade' ) ;
59+ setTimeout ( ( ) =>
60+ alert . remove ( ) , 150 ) ;
61+ }
62+ } , 3000 ) ; // 3 secs
63+ }
64+
65+ // renders library data into HTML
4666function render ( ) {
4767 let tableBody = document . querySelector ( "#display tbody" ) ;
48- tableBody . innerHTML = '' ; // get rid of all instead of one by one row deletion
49-
68+ tableBody . innerHTML = '' ;
69+
5070 myLibrary . forEach ( ( book , index ) => {
5171 let row = tableBody . insertRow ( ) ;
52- // post debate lets use textContent instead of innerHTML
5372 row . insertCell ( 0 ) . textContent = book . title ;
5473 row . insertCell ( 1 ) . textContent = book . author ;
5574 row . insertCell ( 2 ) . textContent = book . pages ;
75+
5676 // 'read' status
5777 const wasReadCell = row . insertCell ( 3 ) ;
5878 const readStatusBtn = document . createElement ( "button" ) ;
@@ -68,17 +88,19 @@ function render() {
6888 const deleteCell = row . insertCell ( 4 ) ;
6989 const deleteBtn = document . createElement ( "button" ) ;
7090 deleteBtn . className = "btn btn-warning" ;
71- deleteBtn . innerHTML = "Delete" ;
91+ deleteBtn . textContent = "Delete" ;
7292 deleteCell . appendChild ( deleteBtn ) ;
93+
7394 deleteBtn . addEventListener ( "click" , function ( ) {
7495 const deletedTitle = myLibrary [ index ] . title ;
7596 myLibrary . splice ( index , 1 ) ;
7697 render ( ) ;
77- alert ( `You've deleted title: ${ deletedTitle } ` ) ;
98+ displayNotification ( `You've deleted " ${ deletedTitle } " ` ) ;
7899 } ) ;
79100 } ) ;
80101}
81- // moved to end to make sure all is defined before load
102+
103+ // ensures functions are defined before being called on page load
82104window . addEventListener ( "load" , function ( ) {
83105 populateStorage ( ) ;
84106 render ( ) ;
0 commit comments