Skip to content

Commit a9c2939

Browse files
committed
Library App to add your favorite books
1 parent 6600243 commit a9c2939

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

Library/worm29/books.js

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
let myLibrary = [];
2+
3+
4+
function book(title, author, numOfPages, readOrNot) {
5+
this.title = title;
6+
this.author = author;
7+
this.numOfPages = numOfPages;
8+
this.readOrNot = readOrNot;
9+
}
10+
11+
12+
function removeBookFromLibrary(index) {
13+
myLibrary.splice(index, 1);
14+
displayBook();
15+
saveLibrary();
16+
}
17+
18+
function loadLibrary() {
19+
if (localStorage.getItem('myLibrary')) {
20+
myLibrary = JSON.parse(localStorage.getItem('myLibrary'));
21+
}
22+
else {
23+
myLibrary = [];
24+
}
25+
displayBook();
26+
}
27+
28+
function saveLibrary() {
29+
localStorage.setItem('myLibrary', JSON.stringify(myLibrary));
30+
}
31+
32+
function displayBook() {
33+
const libraryContainer = document.getElementById('library-container');
34+
libraryContainer.innerHTML = '';
35+
myLibrary.forEach((element, index) => {
36+
const bookCard = document.createElement('div');
37+
bookCard.classList.add('book-card');
38+
39+
const title = document.createElement('h2');
40+
title.textContent = `Title : ${element.title}`;
41+
42+
const author = document.createElement('p');
43+
author.textContent = `Author: ${element.author}`;
44+
45+
const numOfPages = document.createElement('p');
46+
numOfPages.textContent = `NumOfPages: ${element.numOfPages}`;
47+
48+
const readOrNot = document.createElement('p');
49+
readOrNot.textContent = `Read Or Not: ${element.readOrNot}`;
50+
51+
const removeButton = document.createElement('button');
52+
removeButton.textContent = 'Remove';
53+
removeButton.setAttribute('data-index', index);
54+
removeButton.addEventListener('click', function () {
55+
removeBookFromLibrary(this.getAttribute('data-index'));
56+
});
57+
58+
59+
bookCard.appendChild(title);
60+
bookCard.appendChild(author);
61+
bookCard.appendChild(numOfPages);
62+
bookCard.appendChild(readOrNot);
63+
bookCard.appendChild(removeButton);
64+
65+
libraryContainer.appendChild(bookCard);
66+
});
67+
}
68+
69+
document.addEventListener('DOMContentLoaded', function () {
70+
loadLibrary();
71+
function addBookToTheLibrary(title, author, numOfPages, readOrNot) {
72+
const bookExists = myLibrary.some(book => book.title === title && book.author === author);
73+
if (!bookExists) {
74+
const newBook = new book(title, author, numOfPages, readOrNot)
75+
myLibrary.push(newBook);
76+
saveLibrary();
77+
displayBook();
78+
} else {
79+
const bookAlreadyExistsComponent = document.getElementById('book-exits-component');
80+
bookAlreadyExistsComponent.innerHTML = '';
81+
82+
const message = document.createElement('p');
83+
message.textContent = "Book Already Exist!!";
84+
bookAlreadyExistsComponent.appendChild(message);
85+
86+
setTimeout(() => {
87+
message.remove()
88+
}, 1000);
89+
}
90+
}
91+
92+
const newBookBtn = document.getElementById('new-book-btn');
93+
const bookFormContainer = document.getElementById('book-form-container');
94+
const bookForm = document.getElementById('book-form');
95+
96+
newBookBtn.addEventListener('click', function () {
97+
bookFormContainer.style.display = 'block';
98+
})
99+
100+
bookForm.addEventListener('submit', function (e) {
101+
e.preventDefault();
102+
103+
const title = document.getElementById('title').value;
104+
const author = document.getElementById('author').value;
105+
const numOfPages = document.getElementById('pages').value;
106+
const readOrNot = document.getElementById('readStatus').checked ? 'Read' : 'Not Read';
107+
108+
addBookToTheLibrary(title, author, numOfPages, readOrNot);
109+
displayBook();
110+
111+
bookForm.reset();
112+
bookFormContainer.style.display = 'none';
113+
});
114+
})
115+

Library/worm29/index.css

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
body {
2+
background-color: white
3+
}
4+
5+
h1 {
6+
text-align: center;
7+
}
8+
9+
.add-button {
10+
white-space: 10;
11+
}

Library/worm29/index.html

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
5+
<head>
6+
<link rel="stylesheet" href="index.css">
7+
</head>
8+
9+
<body>
10+
<h1 id="header">
11+
The Library
12+
</h1>
13+
14+
<div id="book-form-container" style="display: none;">
15+
<form id="book-form">
16+
<input type="text" id="title" placeholder="Title" required>
17+
<input type="text" id="author" placeholder="Author" required>
18+
<input type="number" id="pages" placeholder="Number of Pages" required>
19+
<label>
20+
<input type="checkbox" id="readStatus">
21+
Read?
22+
</label>
23+
<button type="submit">Add Book</button>
24+
</form>
25+
</div>
26+
27+
<div id="library-container"></div>
28+
<div id="book-exists-component"></div>
29+
<div>
30+
<button id="new-book-btn" class="add-button">NEW BOOK</button>
31+
</div>
32+
<script src="books.js"></script>
33+
</body>
34+
35+
</html>

0 commit comments

Comments
 (0)