generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
155 lines (131 loc) · 4.31 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
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
/* eslint max-classes-per-file: ["error", 3] */
// book class
class Books {
constructor(title, author) {
this.title = title;
this.author = author;
}
}
// Local storage
class Store {
static bookLocalStorage() {
return JSON.parse(localStorage.getItem('books'));
}
static getBooks() {
return Store.bookLocalStorage();
}
static addBook(book) {
const books = Store.bookLocalStorage() ? Store.bookLocalStorage() : [];
if (Array.isArray(books)) {
books.push(book);
}
localStorage.setItem('books', JSON.stringify(books));
}
static removeBook(index) {
const books = Store.getBooks();
books.splice(index, 1);
localStorage.setItem('books', JSON.stringify(books));
}
}
// Creating the list
class CreatBook {
// display books
static displayBooks() {
const books = Store.getBooks();
// localStorage.clear();
if (books !== null) {
books.forEach((book) => CreatBook.createBookElements(book));
}
// no books found
}
// creat books and add them in the UI
static createBookElements(book) {
const bookContainer = document.querySelector('.books');
const bookDiv = document.createElement('div');
const bookName = document.createElement('h2');
const authorName = document.createElement('h2');
const btn = document.createElement('button');
btn.classList.add('delete');
bookDiv.classList.add('container');
// Fill the elements
bookName.innerText = book.title;
authorName.innerText = book.author;
btn.innerHTML = 'Delete';
// For showing the above staff in browser we have to append them
bookDiv.append(bookName, authorName, btn);
// This div must also append to some where in HTML so
bookContainer.appendChild(bookDiv);
}
static openListPage() {
const bookList = document.getElementById('list');
const listSection = document.querySelector('.list-section');
const addSection = document.querySelector('.add-section');
const contactSection = document.querySelector('.contact-section');
bookList.addEventListener('click', () => {
addSection.classList.add('hidden');
listSection.classList.remove('hidden');
contactSection.classList.add('hidden');
});
}
static openAddPage() {
const bookList = document.getElementById('add');
const listSection = document.querySelector('.list-section');
const addSection = document.querySelector('.add-section');
const contactSection = document.querySelector('.contact-section');
bookList.addEventListener('click', () => {
addSection.classList.remove('hidden');
listSection.classList.add('hidden');
contactSection.classList.add('hidden');
});
}
static openContactPage() {
const bookList = document.getElementById('contact');
const listSection = document.querySelector('.list-section');
const addSection = document.querySelector('.add-section');
const contactSection = document.querySelector('.contact-section');
bookList.addEventListener('click', () => {
addSection.classList.add('hidden');
listSection.classList.add('hidden');
contactSection.classList.remove('hidden');
});
}
// remove logic
static delete(el) {
if (el.classList.contains('delete')) {
el.parentElement.remove();
}
}
// clear fields
static clearfields() {
document.getElementById('title').value = '';
document.getElementById('author').value = '';
}
}
// display books
document.addEventListener('DOMContentLoaded', CreatBook.displayBooks);
// =============================================================
// Add book
document.getElementById('book-form').addEventListener('submit', (event) => {
// preventDefault
event.preventDefault();
// get values
const title = document.getElementById('title').value;
const author = document.getElementById('author').value;
// create New book
const book = new Books(title, author);
// Add book to the page
CreatBook.createBookElements(book);
// Add book to local storage
Store.addBook(book);
// clear the fields
CreatBook.clearfields();
});
// =============================================================
// Remove book
document.querySelector('.books').addEventListener('click', (e) => {
CreatBook.delete(e.target);
Store.removeBook();
});
CreatBook.openListPage();
CreatBook.openAddPage();
CreatBook.openContactPage();