-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
185 lines (143 loc) · 4.9 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// add DOM selectors
const form = document.querySelector('#form');
const titleBook = document.querySelector('#title');
const authorBook = document.querySelector('#author');
const isbnBook = document.querySelector('#isbn');
const buttonSubmit = document.querySelector('#submit');
const tableInsert = document.querySelector('#tbody');
// class build for books
class Book {
constructor(title, author, isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
}
}
// class build for UI
class UI {
constructor() { }
//add a book html insert into table body
addBook(book) {
const html = `
<tr>
<th>${book.title}</th>
<th>${book.author}</th>
<th>${book.isbn}</th>
<th class="btn delete font-weight-bold text-danger">X</th>
</tr>
`;
tableInsert.innerHTML += html;
}
//clear inputs after adding a book
clearInputs() {
titleBook.value = '';
authorBook.value = '';
isbnBook.value = '';
}
//delete book function
deleteBook(target) {
target.remove();
}
//add alert div
addAlert(msg, className) {
// create alert element
const textAlert = document.createElement('div');
textAlert.appendChild(document.createTextNode(msg));
textAlert.classList.add(className);
//get where to append the div
const container = document.querySelector('h3');
container.insertAdjacentElement('afterend', textAlert);
// clean the alert dic after one second
setTimeout(() => {
textAlert.classList.remove(className);
textAlert.textContent = '';
}, 1000)
}
};
/** Class LocalStorage */
class LocalStorage {
constructor() { }
static getBookLocalStore() {
//check if there is an array books in local storage, if is not, set it empty. if it is, get it
let books;
if (localStorage.getItem('books') === null) {
books = [];
} else {
books = JSON.parse(localStorage.getItem('books'));
}
return books;
}
//add book to local storage
static addBookLocalStore(book) {
//call get books from local storage function and pass hatever returns to a const books;
const books = LocalStorage.getBookLocalStore();
//push into array books the new book added
books.push(book);
//set local storage again
localStorage.setItem('books', JSON.stringify(books))
}
//call books array from local storage loop through it and call addBook function with every book from array to persist the books into UI
static displayBookLocalStore() {
//Initialize ui class
const ui = new UI();
//declare books const and call get books from local storage to store whatever returns from that function
const books = LocalStorage.getBookLocalStore();
//loop trough the boks array and then call addBook o UI function
books.forEach(book => {
ui.addBook(book);
});
}
//remove books from local storage
static removeBookLocalStore(target) {
// call books array from local storage to stroe that value into a const books
const books = LocalStorage.getBookLocalStore();
//chech if the element to be removed from local storage is the right one
books.forEach((book, index) => {
if (book.isbn === target) {
console.log(book.isbn)
console.log(target)
books.splice(index, 1);
}
});
//set back local storage after deleting book. is not, non book will be deleted because it just persist in books variable before;
localStorage.setItem('books', JSON.stringify(books));
}
};
window.addEventListener('DOMContentLoaded', LocalStorage.displayBookLocalStore);
form.addEventListener('submit', e => {
//prevent default behivour
e.preventDefault();
//grab inputs values
const title = titleBook.value;
const author = authorBook.value;
const isbn = isbnBook.value;
//initialzie Book class
const book = new Book(title, author, isbn);
//initialize UI class
const ui = new UI();
//display alerts
if (book.title !== '' && book.author !== '' && book.isbn !== '') {
// call addBook function in UI class and pass it te book;
ui.addBook(book);
//add book to Local Storage
LocalStorage.addBookLocalStore(book);
//clear inputs
ui.clearInputs();
//add alert div
ui.addAlert('Book added', 'success')
} else {
// add alert error div
ui.addAlert('Please add a book', 'error')
}
});
tbody.addEventListener('click', e => {
//inizialize ui class
const ui = new UI();
//chech if the target element contains a class delete and then delete parent element
if (e.target.classList.contains('delete')) {
//call delete book function with the parent element to be rempved from UI
ui.deleteBook(e.target.parentElement);
//call remove from local storage function and pass to it the isbn value
LocalStorage.removeBookLocalStore(e.target.previousElementSibling.textContent)
}
});