generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (110 loc) · 3.48 KB
/
index.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
/* eslint-disable no-multi-assign */
// eslint-disable-next-line max-classes-per-file
import currentDate from './modules/date.js';
import Book from './modules/books.js';
class UI {
static displayBooks() {
// eslint-disable-next-line no-use-before-define
const books = Store.getBooks();
books.forEach((book) => UI.addBookToList(book));
}
static addBookToList(book) {
const list = document.querySelector('#book-list');
const row = document.createElement('div');
row.setAttribute('id', 'row');
row.innerHTML = `
<p class="row-btn">${book.title} by ${book.author}</p>
<input class ="button delete" type="submit" value="Remove">
`;
list.appendChild(row);
}
static deleteBook(el) {
if (el.classList.contains('delete')) {
el.parentElement.remove();
}
}
static clearFields() {
document.querySelector('#btitle').value = '';
document.querySelector('#author').value = '';
}
}
class Store {
static getBooks() {
let books;
if (localStorage.getItem('books') === null) {
books = [];
} else {
books = JSON.parse(localStorage.getItem('books'));
}
return books;
}
static addBook(book) {
const books = Store.getBooks();
books.push(book);
localStorage.setItem('books', JSON.stringify(books));
}
static removeBook(isbn) {
const books = Store.getBooks();
books.forEach((book, index) => {
if (book.isbn === isbn) {
books.splice(index, 1);
}
});
localStorage.setItem('books', JSON.stringify(books));
}
}
document.addEventListener('DOMContentLoaded', UI.displayBooks);
document.querySelector('#form').addEventListener('submit', (e) => {
e.preventDefault();
const title = document.querySelector('#btitle').value;
const author = document.querySelector('#author').value;
document.querySelector('.form-inputs').value = '';
document.querySelector('.form-inputs1').value = '';
const book = new Book(title, author);
UI.addBookToList(book);
Store.addBook(book);
UI.clearFields();
});
document.querySelector('#book-list').addEventListener('click', (e) => {
UI.deleteBook(e.target);
Store.removeBook(e.target.previousElementSibling.textContent);
});
const bookList = document.querySelector('#book-list');
const listHead = document.querySelector('.list-head');
const bookForm = document.querySelector('#form');
const addBookTitle = document.querySelector('.add-book-title');
const contactInfo = document.querySelector('.contact-info');
const Toogle = () => {
bookList.style.display = 'block';
bookForm.style.display = 'none';
addBookTitle.style.display = 'none';
listHead.style.display = 'block';
contactInfo.style.display = 'none';
};
document.querySelector('#List').addEventListener('click', () => {
Toogle();
});
const addToogle = () => {
bookList.style.display = 'none';
bookForm.style.display = 'block';
addBookTitle.style.display = 'block';
listHead.style.display = 'none';
contactInfo.style.display = 'none';
};
document.querySelector('#add-new').addEventListener('click', () => {
addToogle();
});
const contactToogle = () => {
bookList.style.display = 'none';
bookForm.style.display = 'none';
addBookTitle.style.display = 'none';
contactInfo.style.display = 'block';
listHead.style.display = 'none';
};
document.querySelector('#contact').addEventListener('click', () => {
contactToogle();
});
const websiteDate = document.querySelector('.date');
setInterval(() => {
websiteDate.innerHTML = `${currentDate()}`;
}, 1000);