generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
53 lines (48 loc) · 1.57 KB
/
lib.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
import Book from './book.js';
import {
storedBooks, empty, title, author, tableBody,
} from './variables.js';
const BookActions = class AwesomeLibrary {
constructor() {
this.storedBooks = storedBooks;
}
addBookDetails() {
const booksData = new Book(title.value, author.value);
if (!this.storedBooks) {
this.storedBooks = [];
}
this.storedBooks.push(booksData);
localStorage.setItem('bookstored', JSON.stringify(this.storedBooks));
this.displayBooks();
}
displayBooks() {
if (!this.storedBooks || !this.storedBooks === 0 || this.storedBooks === []) {
empty.innerHTML = 'No books in the library';
} else {
empty.style.display = 'none';
let eachTableRow = '';
for (let i = 0; i < this.storedBooks.length; i += 1) {
eachTableRow
+= `<tr>
<td>"${this.storedBooks[i].title}" by ${this.storedBooks[i].author}</td>
<td><button type="button" id=${i} class="remove_btn">Remove</button></td>
</tr>
`;
}
tableBody.innerHTML = eachTableRow;
const deleteBookBtn = document.querySelectorAll('.remove_btn');
this.removeBooks(deleteBookBtn);
}
}
removeBooks(deleteBookBtn) {
deleteBookBtn.forEach((elementBtn) => {
elementBtn.addEventListener('click', () => {
const index = elementBtn.getAttribute('id');
this.storedBooks.splice(index, 1);
localStorage.setItem('bookstored', JSON.stringify(this.storedBooks));
this.displayBooks();
});
});
}
};
export default BookActions;