-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
115 lines (95 loc) · 3.14 KB
/
main.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
const form = document.querySelector('.add-book');
const bookTitle = document.querySelector('.title');
const bookAuthor = document.querySelector('.author');
const bookList = document.querySelector('.book-list');
class Storage {
constructor() {
this.collection = [];
}
static addCollection(newBook) {
this.collection.push(newBook);
localStorage.setItem('collection', JSON.stringify(this.collection));
}
static removeFromCollection(target) {
const removeBook = target.previousElementSibling.firstElementChild.textContent;
this.collection.filter((book, index) => {
if (book.title === removeBook) {
this.collection.splice(index, 1);
}
return this.collection;
});
localStorage.setItem('collection', JSON.stringify(this.collection));
}
static getBooksFromStorage() {
if (localStorage.getItem('collection') === null) {
this.collection = [];
} else {
this.collection = JSON.parse(localStorage.getItem('collection'));
}
return this.collection;
}
}
function Book(title, author) {
this.title = title;
this.author = author;
}
function UI() {}
UI.prototype.addBookToUI = function (newBook) {
Storage.collection.forEach((book, index) => {
if (book.title === newBook.title) {
if (index % 2 === 0) {
bookList.innerHTML += `
<li class='book list-group-item-secondary d-flex flex-row justify-content-between py-3'>
<div class='d-flex flex-row ms-5'>
<p class='book-title margin-sm fs-5'>${newBook.title}</p>
<p class='book-author margin-sm fs-5'>  by ${newBook.author}</p>
</div>
<button class='remove btn btn-danger me-5' type='button'>Remove</button>
</li>
`;
} else {
bookList.innerHTML += `
<li class='book list-group-item-light d-flex flex-row justify-content-between py-3'>
<div class='d-flex flex-row ms-5'>
<p class='book-title margin-sm fs-5'>${newBook.title}</p>
<p class='book-author margin-sm fs-5'>  by ${newBook.author}</p>
</div>
<button class='remove btn btn-danger me-5' type='button'>Remove</button>
</li>
`;
}
}
});
};
UI.prototype.clearInputs = function (element1, element2) {
element1.value = '';
element2.value = '';
};
UI.prototype.removeBookFromUI = function (target) {
target.parentElement.remove();
};
const ui = new UI();
function addBook(e) {
const title = bookTitle.value;
const author = bookAuthor.value;
const newBook = new Book(title, author);
Storage.addCollection(newBook);
ui.addBookToUI(newBook);
ui.clearInputs(bookTitle, bookAuthor);
e.preventDefault();
}
function removeBook(e) {
if (e.target.className === 'remove btn btn-danger me-5') {
ui.removeBookFromUI(e.target);
Storage.removeFromCollection(e.target);
/* eslint-disable */
location.reload();
/* eslint-enable */
}
}
form.addEventListener('submit', addBook);
bookList.addEventListener('click', removeBook);
document.addEventListener('DOMContentLoaded', () => {
const allBooks = Storage.getBooksFromStorage();
allBooks.forEach((book) => ui.addBookToUI(book));
});