-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
146 lines (133 loc) · 4.18 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
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
141
142
143
144
145
146
class BookList {
constructor(bookName, bookAuthor, bookId, books, submit, bookList) {
this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookId = bookId;
this.books = books;
this.submit = submit;
this.bookList = bookList;
}
book() {
this.bookId = Math.random()
.toString(36)
.replace(/[^a-z]+/g, '')
.substr(0, 5);
const variable = {
name: this.bookName.value,
author: this.bookAuthor.value,
id: this.bookId,
};
return variable;
}
addBook(bookName, bookAuthor, bookId) {
const list = document.createElement('li');
const nameInput = document.createElement('a');
nameInput.classList.add('book-data');
list.appendChild(nameInput);
const removeButton = document.createElement('button');
removeButton.classList.add('button');
removeButton.classList.add('is-danger');
removeButton.innerText = 'Remove';
if (bookName !== undefined && bookAuthor !== undefined) {
removeButton.setAttribute('id', bookId);
nameInput.append(bookName + ' by ' + bookAuthor + ' ');
} else {
removeButton.setAttribute('id', this.bookId);
nameInput.append(this.bookName.value + ' by ' + this.bookAuthor.value + ' ');
}
nameInput.appendChild(removeButton);
this.books.appendChild(nameInput);
removeButton.addEventListener('click', (e) => {
e.preventDefault();
removeButton.parentElement.remove();
const books = this.bookList;
const index = books.findIndex((book) => book.id === bookId);
books.splice(index, 1);
localStorage.setItem('books', JSON.stringify(books));
});
}
submitBook() {
this.submit.addEventListener('click', (e) => {
e.preventDefault();
this.addBook(undefined, undefined);
this.bookList.push(this.book());
localStorage.setItem('books', JSON.stringify(this.bookList));
this.bookName.value = '';
this.bookAuthor.value = '';
});
}
localStorage() {
window.addEventListener('load', () => {
if (localStorage.getItem('books') !== null) {
this.bookList = JSON.parse(localStorage.getItem('books'));
this.bookList.forEach((book) => {
const newBookName = book.name;
const newBookAuthor = book.author;
const newBookId = book.id;
this.addBook(newBookName, newBookAuthor, newBookId);
});
}
});
}
}
const bookName = document.querySelector('.name');
const bookAuthor = document.querySelector('.author');
const books = document.querySelector('.book-list');
const submit = document.querySelector('.add');
const bookList = [];
const booksLists = new BookList(bookName, bookAuthor, null, books, submit, bookList);
booksLists.submitBook();
booksLists.localStorage();
// Navigation
const allBooks = document.querySelector('#allbooks');
const addBook = document.querySelector('#addbook');
const contact = document.querySelector('#contact');
const indexPage = document.querySelector('#index');
const allBooksLink = document.querySelector('#all-books');
const addBookLink = document.querySelector('#add-book');
const contactUsLink = document.querySelector('#contact-us');
function mainNav(page) {
page.addEventListener('click', () => {
allBooks.classList.add('show');
allBooks.classList.remove('hidden');
addBook.classList.add('hidden');
contact.classList.add('hidden');
});
}
mainNav(indexPage);
mainNav(allBooksLink);
addBookLink.addEventListener('click', () => {
addBook.classList.add('show');
addBook.classList.remove('hidden');
allBooks.classList.add('hidden');
contact.classList.add('hidden');
});
contactUsLink.addEventListener('click', () => {
contact.classList.add('show');
contact.classList.remove('hidden');
allBooks.classList.add('hidden');
addBook.classList.add('hidden');
});
// Date & Time
const d = new Date();
const day = d.getDate();
const year = d.getFullYear();
const months = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'June',
'July',
'Aug',
'Sept',
'Oct',
'Nov',
'Dec',
];
const month = months[d.getMonth()];
const date = `${month} ${day} ${year}`;
const time = new Date().toLocaleTimeString();
const timeBox = document.querySelector('#time');
timeBox.innerHTML = `${date}, ${time}`;