generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
219 lines (170 loc) · 6.07 KB
/
script.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
function Book(name, author) {
this.name = name;
this.author = author;
}
class Books {
constructor() {
this.storage = [];
}
add(book) {
this.storage.push(book);
}
remove(index) {
this.storage.splice(index, 1);
}
set setBooks(books) {
this.storage = books;
}
}
function saveBooksInLocalStorage(books) {
localStorage.setItem('bookArray', JSON.stringify(books));
}
function getBooksFromLocalStorage() {
return JSON.parse(localStorage.getItem('bookArray'));
}
const bookStor = new Books();
function removeBook(remButt) {
const books = remButt.parentElement.parentElement.children;
for (let i = 0; i < books.length; i += 1) {
if (remButt.parentElement === books[i]) {
bookStor.remove(i);
saveBooksInLocalStorage(bookStor.storage);
break;
}
}
}
function createHTMLBook(bName, bAuthor) {
const bookDiv = document.createElement('div');
bookDiv.classList.add('book', 'd-flex', 'justify-content-between', 'py-2', 'px-3');
const name = document.createElement('p');
name.textContent = `"${bName}" by ${bAuthor}`;
name.classList.add('align-self-center', 'my-1', 'f-20px');
bookDiv.appendChild(name);
const remButt = document.createElement('button');
remButt.textContent = 'Remove';
remButt.classList.add('remButton', 'border', 'border-2', 'border-dark');
bookDiv.appendChild(remButt);
remButt.addEventListener('click', () => {
removeBook(remButt);
remButt.parentElement.remove();
});
return bookDiv;
}
function setLocalStorage() {
const localObj = localStorage.getItem('bookArray');
const bookContainer = document.getElementById('bookContainer');
for (let i = 0; i < JSON.parse(localObj).length; i += 1) {
const { name } = JSON.parse(localObj)[i];
const { author } = JSON.parse(localObj)[i];
bookContainer.appendChild(createHTMLBook(name, author));
}
}
function addBook(newBook) {
bookStor.add(newBook);
saveBooksInLocalStorage(bookStor.storage);
const bookContainer = document.getElementById('bookContainer');
bookContainer.appendChild(createHTMLBook(newBook.name, newBook.author));
}
const addBookbtn = document.getElementById('addBookbtn');
addBookbtn.onclick = function addABook() {
const name = document.getElementById('addName').value;
const author = document.getElementById('addAuthor').value;
if (name !== '' && author !== '') {
const newBook = new Book(name, author);
addBook(newBook);
document.getElementById('addName').value = null;
document.getElementById('addAuthor').value = null;
}
};
function checkTime(i) {
if (i < 10) {
i = `0${i}`;
}
return i;
}
function time() {
const today = new Date();
const day = today.getDate();
const month = today.getMonth();
const year = today.getFullYear();
let tail = '';
if (day === 1) {
tail = 'st';
} else if (day === 2) {
tail = 'nd';
} else if (day === 3) {
tail = 'rd';
} else {
tail = 'th';
}
const monthArray = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const hour = today.getHours();
let minute = today.getMinutes();
let second = today.getSeconds();
minute = checkTime(minute);
second = checkTime(second);
document.getElementById('time').innerHTML = `${monthArray[month]} ${day}${tail} ${year}, ${hour}:${minute}:${second}`;
setTimeout(time, 1000);
}
const bookListBtn = document.getElementById('navBookList');
const bookFormBtn = document.getElementById('navBookForm');
const bookContactBtn = document.getElementById('navBookContact');
bookListBtn.addEventListener('click', () => {
const bookListSection = document.getElementById('bookList');
const bookFormSection = document.getElementById('addBook');
const bookContactSection = document.getElementById('contact');
bookListSection.classList.remove('d-none');
bookListSection.classList.add('d-flex');
bookFormSection.classList.remove('d-flex');
bookFormSection.classList.add('d-none');
bookContactSection.classList.remove('d-flex');
bookContactSection.classList.add('d-none');
bookListBtn.classList.remove('text-black');
bookListBtn.classList.add('text-primary');
bookFormBtn.classList.remove('text-primary');
bookFormBtn.classList.add('text-black');
bookContactBtn.classList.remove('text-primary');
bookContactBtn.classList.add('text-black');
});
bookFormBtn.addEventListener('click', () => {
const bookListSection = document.getElementById('bookList');
const bookFormSection = document.getElementById('addBook');
const bookContactSection = document.getElementById('contact');
bookListSection.classList.remove('d-flex');
bookListSection.classList.add('d-none');
bookFormSection.classList.remove('d-none');
bookFormSection.classList.add('d-flex');
bookContactSection.classList.remove('d-flex');
bookContactSection.classList.add('d-none');
bookListBtn.classList.remove('text-primary');
bookListBtn.classList.add('text-black');
bookFormBtn.classList.remove('text-black');
bookFormBtn.classList.add('text-primary');
bookContactBtn.classList.remove('text-primary');
bookContactBtn.classList.add('text-black');
});
bookContactBtn.addEventListener('click', () => {
const bookListSection = document.getElementById('bookList');
const bookFormSection = document.getElementById('addBook');
const bookContactSection = document.getElementById('contact');
bookListSection.classList.remove('d-flex');
bookListSection.classList.add('d-none');
bookFormSection.classList.remove('d-flex');
bookFormSection.classList.add('d-none');
bookContactSection.classList.remove('d-none');
bookContactSection.classList.add('d-flex');
bookListBtn.classList.remove('text-primary');
bookListBtn.classList.add('text-black');
bookFormBtn.classList.remove('text-primary');
bookFormBtn.classList.add('text-black');
bookContactBtn.classList.remove('text-black');
bookContactBtn.classList.add('text-primary');
});
const localObj = localStorage.getItem('bookArray');
if (localObj != null) {
setLocalStorage();
}
if (localStorage.getItem('bookArray') != null) {
bookStor.setBooks = getBooksFromLocalStorage();
}
time();