-
Notifications
You must be signed in to change notification settings - Fork 903
/
Copy pathbooks.js
115 lines (90 loc) · 3.61 KB
/
books.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
let myLibrary = [];
function book(title, author, numOfPages, readOrNot) {
this.title = title;
this.author = author;
this.numOfPages = numOfPages;
this.readOrNot = readOrNot;
}
function removeBookFromLibrary(index) {
myLibrary.splice(index, 1);
displayBook();
saveLibrary();
}
function loadLibrary() {
if (localStorage.getItem('myLibrary')) {
myLibrary = JSON.parse(localStorage.getItem('myLibrary'));
}
else {
myLibrary = [];
}
displayBook();
}
function saveLibrary() {
localStorage.setItem('myLibrary', JSON.stringify(myLibrary));
}
function displayBook() {
const libraryContainer = document.getElementById('library-container');
libraryContainer.innerHTML = '';
myLibrary.forEach((element, index) => {
const bookCard = document.createElement('div');
bookCard.classList.add('book-card');
const title = document.createElement('h2');
title.textContent = `Title : ${element.title}`;
const author = document.createElement('p');
author.textContent = `Author: ${element.author}`;
const numOfPages = document.createElement('p');
numOfPages.textContent = `NumOfPages: ${element.numOfPages}`;
const readOrNot = document.createElement('p');
readOrNot.textContent = `Read Or Not: ${element.readOrNot}`;
const removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
removeButton.setAttribute('data-index', index);
removeButton.addEventListener('click', function () {
removeBookFromLibrary(this.getAttribute('data-index'));
});
bookCard.appendChild(title);
bookCard.appendChild(author);
bookCard.appendChild(numOfPages);
bookCard.appendChild(readOrNot);
bookCard.appendChild(removeButton);
libraryContainer.appendChild(bookCard);
});
}
document.addEventListener('DOMContentLoaded', function () {
loadLibrary();
function addBookToTheLibrary(title, author, numOfPages, readOrNot) {
const bookExists = myLibrary.some(book => book.title === title && book.author === author);
if (!bookExists) {
const newBook = new book(title, author, numOfPages, readOrNot)
myLibrary.push(newBook);
saveLibrary();
displayBook();
} else {
const bookAlreadyExistsComponent = document.getElementById('book-exits-component');
bookAlreadyExistsComponent.innerHTML = '';
const message = document.createElement('p');
message.textContent = "Book Already Exist!!";
bookAlreadyExistsComponent.appendChild(message);
setTimeout(() => {
message.remove()
}, 1000);
}
}
const newBookBtn = document.getElementById('new-book-btn');
const bookFormContainer = document.getElementById('book-form-container');
const bookForm = document.getElementById('book-form');
newBookBtn.addEventListener('click', function () {
bookFormContainer.style.display = 'block';
})
bookForm.addEventListener('submit', function (e) {
e.preventDefault();
const title = document.getElementById('title').value;
const author = document.getElementById('author').value;
const numOfPages = document.getElementById('pages').value;
const readOrNot = document.getElementById('readStatus').checked ? 'Read' : 'Not Read';
addBookToTheLibrary(title, author, numOfPages, readOrNot);
displayBook();
bookForm.reset();
bookFormContainer.style.display = 'none';
});
})