-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (43 loc) · 1.52 KB
/
index.js
File metadata and controls
54 lines (43 loc) · 1.52 KB
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
import switchPage from './modules/pagination.js';
import isValid from './modules/validate.js';
import updateTime from './modules/dateTime.js';
import * as storage from './modules/storage.js';
import * as view from './modules/view.js';
const form = document.querySelector('.form');
const list = document.querySelector('#list');
const addBook = document.querySelector('#addbook');
const contact = document.querySelector('#contact');
const bookList = document.querySelector('.book-list');
setInterval(updateTime, 1000);
window.addEventListener('load', () => {
[list, addBook, contact].forEach((link) => {
link.addEventListener('click', () => {
switchPage(link);
});
});
view.renderBooks();
});
form.addEventListener('submit', (e) => {
e.preventDefault();
const title = document.querySelector('#title').value;
const author = document.querySelector('#author').value;
const id = Math.random() * 2 + title;
if (!isValid(title) && !isValid(author)) {
return view.displayMessage('Please fill all fields!', 'error');
}
const newBook = { id, title, author };
storage.saveBookToStorage(newBook);
view.renderBooks();
switchPage(list);
view.displayMessage('Book added successfully', 'success');
view.clearInputFields();
return null;
});
bookList.addEventListener('click', (e) => {
e.preventDefault();
if (e.target.id !== 'remove') return;
const book = e.target.parentElement;
storage.removeFromStorage(book.id);
view.renderBooks();
view.displayMessage('Book removed successfully', 'success');
});