-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (59 loc) · 2.23 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
import { DateTime } from './modules/luxon.js';
import dateFormat from './modules/date.js';
import Book, { reload, load } from './modules/books.js';
import { listChange, addBookChange, contactChange } from './modules/nav-change.js';
import { bookAdditionError, bookAdditionSuccess } from './modules/book-validation.js';
const bookForm = document.forms['add-book-form'];
const addBook = document.querySelector('.add-book');
const bookList = document.querySelector('.book-list');
const navBar = document.querySelectorAll('nav a');
const date = document.querySelector('#date');
const list = document.querySelector('#list');
const addNew = document.querySelector('#add-new');
const contact = document.querySelector('#contact');
const bookObj = new Book();
bookList.addEventListener('click', (event) => {
if (event.target.className === 'delete') {
const book = event.target.parentElement;
bookObj.removeBook(book);
bookObj.updateCollection(book);
bookList.removeChild(book);
}
});
addBook.addEventListener('click', (click) => {
click.preventDefault();
const bookTitleValue = bookForm.querySelector('#book-title').value;
const bookAuthorValue = bookForm.querySelector('#book-author').value;
if (bookTitleValue.length > 1 && bookAuthorValue.length > 1) {
bookObj.createBook(bookTitleValue, bookAuthorValue);
bookObj.updateCollection();
bookList.innerHTML += `<li class='book-item'>
<p>
<span class="book-title">"${bookTitleValue}"</span> by <span class='book-author'>${bookAuthorValue}</span>
</p>
<button type='button' class='delete'>Delete</button>
</li>`;
bookAdditionSuccess();
document.querySelectorAll('input[type="text"]').forEach((element) => {
element.value = '';
});
} else {
bookAdditionError();
}
});
reload();
load();
navBar.forEach((nav) => {
nav.addEventListener('click', (event) => {
if (event.target === list) {
listChange();
} else if (event.target === addNew) {
addBookChange();
} else if (event.target === contact) {
contactChange();
}
});
});
setInterval(() => {
date.innerHTML = dateFormat(DateTime.now());
}, 200);