generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
130 lines (118 loc) · 3.69 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
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
/* eslint-disable no-use-before-define */
/* eslint-disable no-unused-vars */
/* eslint-disable max-classes-per-file */
class Book {
constructor(title, author) {
this.title = title;
this.author = author;
this.id = Math.random();
}
}
// UI class to handle all UI events
class UI {
static showBooks(collection) {
const table = document.querySelector('.table');
table.innerHTML = '';
collection.forEach((book, index) => {
const row = document.createElement('tr');
const td1 = document.createElement('p');
td1.innerHTML = `<strong>"${book.title}" by ${book.author}</strong>`;
const td2 = document.createElement('div');
const button = document.createElement('button');
button.className = 'remove';
button.innerHTML = 'Remove';
button.addEventListener('click', () => {
library.removeBook(book.id);
});
td2.appendChild(button);
row.append(td1, td2);
table.appendChild(row);
});
document.querySelector('.form').reset();
}
}
class Library {
constructor() {
this.collection = JSON.parse(localStorage.getItem('books')) || [];
this.checkLength();
}
checkLength() {
const empty = document.querySelector('.empty');
if (this.collection.length === 0) {
empty.innerHTML = 'The library is empty';
} else {
empty.innerHTML = '';
}
}
addBook(book) {
this.collection.push(book);
localStorage.setItem('books', JSON.stringify(this.collection));
UI.showBooks(this.collection);
this.checkLength();
}
removeBook(id) {
this.collection = this.collection.filter((book) => book.id !== id);
localStorage.setItem('books', JSON.stringify(this.collection));
UI.showBooks(this.collection);
this.checkLength();
}
}
const library = new Library();
UI.showBooks(library.collection);
const title = document.getElementById('book-title');
const author = document.getElementById('author');
const form = document.querySelector('.form');
const error = document.querySelector('.error');
form.addEventListener('submit', (e) => {
e.preventDefault();
if (title.value === '' && author.value === '') {
error.innerHTML = 'Please fill in all the fields';
setTimeout(() => {
error.textContent = '';
}, 3000);
} else {
const newBook = new Book(title.value, author.value);
library.addBook(newBook);
}
});
// Show date and time
window.onload = () => {
const date = new Date();
date.setDate(date.getDate());
document.querySelector('.date').innerHTML = date;
function updateTime() {
const time = document.getElementsByClassName('date');
const datestring = new Date().toLocaleString();
const newString = datestring.replace(', ', ' -');
time.textContent = newString;
}
setInterval(updateTime, 1000);
};
const homepage = document.querySelector('.home');
const addNew = document.querySelector('.add');
const contactpage = document.querySelector('.contact');
const homeSection = document.querySelector('#book-list');
const bookSection = document.querySelector('#Add-book');
const contactSection = document.querySelector('#contactUs');
const Clicked = false;
homepage.addEventListener('click', () => {
if (Clicked === false) {
homeSection.style.display = 'block';
bookSection.style.display = 'none';
contactSection.style.display = 'none';
}
});
addNew.addEventListener('click', () => {
if (Clicked === false) {
homeSection.style.display = 'none';
bookSection.style.display = 'block';
contactSection.style.display = 'none';
}
});
contactpage.addEventListener('click', () => {
if (Clicked === false) {
homeSection.style.display = 'none';
bookSection.style.display = 'none';
contactSection.style.display = 'block';
}
});