-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
121 lines (102 loc) · 3.51 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
/* eslint-disable */
class Book{
constructor(title,author) {
this.title = title;
this.author = author;
}
}
//UI handler
class UI{
static displayBook() {
const books = Store.getBooks();
books.forEach(book =>{
UI.addBookToUI(book.title,book.author)
})
}
static addBookToUI(title,author) {
const bookList = document.getElementById('book-list');
const bookListItem = document.createElement('li');
bookListItem.className = 'book-list-item';
bookListItem.innerHTML = `"<span class='span-book' >${title}</span>"
<span class="span-book">by</span>
<span class="span-book"> ${author}</span>
<button class="remove">Remove</button>`
bookList.appendChild(bookListItem)
}
static removeBookFromUI(e) {
e.target.parentElement.remove();
}
}
//local storage
class Store{
static getBooks() {
const book = JSON.parse(localStorage.getItem('books'));
const books = book || [];
return books;
}
static addBookToLS(title, author){
const books = Store.getBooks();
const book = new Book(title, author);
books.push(book);
localStorage.setItem('books', JSON.stringify(books));
}
static removeBookFromLS(title,author) {
const books = Store.getBooks();
const book = books.filter(book => book.title !== title.innerHTML && book.author !== author.innerHTML);
localStorage.setItem('books', JSON.stringify(book));
}
}
//Event: Display Books
document.addEventListener('DOMContentLoaded', UI.displayBooks)
//add event listeners
const btn = document.getElementById('btn');
const form = document.querySelector('.form-div');
btn.addEventListener('click', (e) => {
if (!form.checkValidity()) {
return;
}
e.preventDefault();
const title = document.getElementById('title');
const author = document.getElementById('author');
UI.addBookToUI(title.value, author.value);
Store.addBookToLS(title.value, author.value);
title.value = '';
author.value = '';
});
//remove book
document.getElementById('book-list').addEventListener('click', e =>{
if(e.target.classList.contains('remove')){
UI.removeBookFromUI(e);
Store.removeBookFromLS(
e.target.parentElement.children[0],
e.target.parentElement.children[2]
)
}
})
// Create a new Date object
const currentDate = new Date();
// Display the current date in the browser
const dateString =currentDate.toLocaleString();
document.getElementById("date").innerHTML = dateString;
const showBooksSec = document.getElementById('book-list-section');
const addBooksSec = document.getElementById('add-book-section');
const contactSec = document.getElementById('contact-section');
const showBooks = document.getElementById('show-list');
const showAdd = document.getElementById('show-add');
const showContact = document.getElementById('show-contact');
showBooks.addEventListener('click',()=>{
showBooksSec.style.display = 'flex';
addBooksSec.style.display = 'none';
contactSec.style.display = 'none';
})
showAdd.addEventListener('click',()=>{
addBooksSec.style.display = 'flex';
showBooksSec.style.display = 'none';
contactSec.style.display = 'none';
})
showContact.addEventListener('click',()=>{
contactSec.style.display = 'flex';
addBooksSec.style.display = 'none';
showBooksSec.style.display = 'none';
})
UI.displayBook()