generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
143 lines (127 loc) · 4.27 KB
/
app.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
const dynamicBooksDiv = document.getElementById('books-wrapper');
const bookTitle = document.getElementById('title');
const bookAuthor = document.getElementById('author');
class Book {
constructor(title, author) {
this.title = title;
this.author = author;
this.id = Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
static getBooks() {
let booksList;
if (localStorage.getItem('booksDetails') === null) {
booksList = [];
} else {
booksList = JSON.parse(localStorage.getItem('booksDetails'));
}
return booksList;
}
static addBooks(book) {
const newbooksList = Book.getBooks();
newbooksList.push(book);
localStorage.setItem('booksDetails', JSON.stringify(newbooksList));
}
static removeBooks(bookID) {
const books = Book.getBooks();
books.forEach((book, index) => {
if (book.id === bookID) {
books.splice(index, 1);
}
});
localStorage.setItem('booksDetails', JSON.stringify(books));
}
static addBooksToList(book) {
const div = document.createElement('div');
const h3 = document.createElement('h3');
const h4 = document.createElement('h4');
const button = document.createElement('button');
div.className = 'book-div';
h3.innerText = `Title : ${book.title}`;
h4.innerText = `Author : ${book.author}`;
button.innerHTML = 'remove';
button.id = book.id;
button.type = 'button';
button.className = 'rmv';
div.append(h3, h4, button);
dynamicBooksDiv.appendChild(div);
}
static showBooks() {
const booksCreated = Book.getBooks();
booksCreated.forEach((book) => Book.addBooksToList(book));
}
static clearInputs() {
bookTitle.value = '';
bookAuthor.value = '';
}
static deleteBook(book) {
if (book.classList.contains('rmv')) {
book.parentElement.remove();
}
}
}
document.addEventListener('DOMContentLoaded', Book.showBooks);
document.querySelector('#formContainer').addEventListener('submit', (e) => {
e.preventDefault();
if (bookTitle.value === '' || bookAuthor.value === '') {
Book.clearInputs();
} else {
const book = new Book(bookTitle.value, bookAuthor.value);
Book.addBooksToList(book);
Book.addBooks(book);
Book.clearInputs();
}
});
document.querySelector('.books-wrapper').addEventListener('click', (e) => {
Book.deleteBook(e.target);
Book.removeBooks(e.target.id);
});
const booklistBtn = document.getElementById('booklist');
const addbookBtn = document.getElementById('addbook');
const contactinfoBtn = document.getElementById('contactinfo');
const booklistSection = document.getElementById('booklistsection');
const inputSection = document.querySelector('.input-wrapper');
const contactinfoSection = document.querySelector('.contactinfo-section');
const booklistOpen = () => {
booklistSection.classList.remove('dis-none');
inputSection.classList.add('dis-none');
booklistBtn.style.backgroundColor = 'white';
contactinfoBtn.style.backgroundColor = 'unset';
addbookBtn.style.backgroundColor = 'unset';
contactinfoSection.classList.add('dis-none');
};
const addbookOpen = () => {
booklistSection.classList.add('dis-none');
inputSection.classList.remove('dis-none');
booklistBtn.style.backgroundColor = 'unset';
contactinfoBtn.style.backgroundColor = 'unset';
addbookBtn.style.backgroundColor = 'white';
contactinfoSection.classList.add('dis-none');
};
const contactinfoOpen = () => {
booklistSection.classList.add('dis-none');
inputSection.classList.add('dis-none');
booklistBtn.style.backgroundColor = 'unset';
contactinfoBtn.style.backgroundColor = 'white';
addbookBtn.style.backgroundColor = 'unset';
contactinfoSection.classList.remove('dis-none');
};
booklistBtn.addEventListener('click', booklistOpen);
addbookBtn.addEventListener('click', addbookOpen);
contactinfoBtn.addEventListener('click', contactinfoOpen);
/* global luxon, luxon */
const displayTime = () => {
const currentDate = luxon.DateTime.fromJSDate(new Date());
const date = currentDate.toLocaleString(
luxon.DateTime.DATETIME_MED_WITH_SECONDS,
);
document.querySelector('.time').innerHTML = date;
};
document.addEventListener('DOMContentLoaded', () => {
luxon.Settings.defaultLocale = 'en';
displayTime();
setInterval(() => {
displayTime();
}, 1000);
});