Skip to content

Commit c9bfb66

Browse files
committedJul 8, 2024··
init
0 parents  commit c9bfb66

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed
 

‎assets/js/data/book.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Book {
2+
constructor(title, author) {
3+
this.title = title
4+
this.author = author
5+
this.read = false
6+
}
7+
8+
getTitle() {
9+
return this.title
10+
}
11+
12+
getAuthor() {
13+
return this.author
14+
}
15+
16+
isRead() {
17+
return this.read
18+
}
19+
20+
markAsRead() {
21+
this.read = true
22+
}
23+
}

‎assets/js/data/library.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Library {
2+
constructor() {
3+
this.books = []
4+
}
5+
6+
addBook(book) {
7+
this.books.push(book)
8+
}
9+
10+
getBooks() {
11+
return this.books
12+
}
13+
14+
bookCount() {
15+
return this.books.length
16+
}
17+
18+
removeBook(index) {
19+
if (index >= 0 && index < this.books.length) {
20+
this.books.splice(index, 1)
21+
}
22+
}
23+
}

‎assets/js/main.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const formEl = document.querySelector('form')
2+
3+
const lib = new Library()
4+
5+
formEl.addEventListener('submit', (e) => {
6+
e.preventDefault()
7+
8+
const bookTitle = document.getElementById('bookTitle').value.trim()
9+
const bookAuthor = document.getElementById('bookAuthor').value.trim()
10+
11+
if (bookTitle && bookAuthor) {
12+
const addedBook = new Book(bookTitle, bookAuthor)
13+
14+
// Add book to Library Class
15+
lib.addBook(addedBook)
16+
17+
renderLibrary()
18+
19+
// remove the entered book information
20+
document.getElementById('bookTitle').value = ''
21+
document.getElementById('bookAuthor').value = ''
22+
}
23+
})
24+
25+
function markBookAsRead(index) {
26+
lib.getBooks()[index].markAsRead()
27+
renderLibrary()
28+
}
29+
30+
function removeBook(index) {
31+
lib.removeBook(index)
32+
renderLibrary()
33+
}
34+
35+
function renderLibrary() {
36+
const renderLibEl = document.querySelector('#renderedLibrary')
37+
38+
const bookCountEl = document.querySelector('#bookCount')
39+
40+
bookCountEl.textContent = lib.bookCount()
41+
42+
// resetting previous values
43+
renderLibEl.innerHTML = ''
44+
45+
lib.getBooks().forEach((book, index) => {
46+
renderLibEl.innerHTML += `
47+
<li class="p-3 bg-orange-100 rounded flex justify-between">
48+
<div class="${
49+
book.isRead() ? 'line-through' : ''
50+
}">${book.getTitle()} by ${book.getAuthor()}</div>
51+
<div>
52+
<button class="px-2 py-1 bg-green-600 text-sm rounded text-white" onclick="markBookAsRead(
53+
${index}
54+
)">
55+
Mark as Read
56+
</button>
57+
<button class="px-2 py-1 bg-red-600 text-sm rounded text-white" onclick="removeBook(${index})">
58+
Remove
59+
</button>
60+
</div>
61+
</li>
62+
`
63+
})
64+
}

‎index.html

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Library App</title>
8+
9+
<!-- TailwindCSS -->
10+
<script src="https://cdn.tailwindcss.com"></script>
11+
12+
<style type="text/tailwindcss">
13+
@layer components {
14+
.input {
15+
@apply px-4 py-2 rounded bg-gray-100 outline-none w-full
16+
}
17+
}
18+
19+
</style>
20+
</head>
21+
22+
<body class="bg-indigo-800 p-10">
23+
<div class="bg-white p-5 rounded max-w-xl mx-auto">
24+
<h4 class="font-semibold text-xl">Book Library</h4>
25+
26+
<form name="bookForm" class="space-y-3 mt-3">
27+
<input type="text" id="bookTitle" placeholder="Book Title" class="input" required>
28+
<input type="text" id="bookAuthor" placeholder="Book Author" class="input" required>
29+
<button type="submit"
30+
class="px-3 py-1 bg-orange-500 rounded uppercase font-semibold text-sm text-white">Save </button>
31+
</form>
32+
</div>
33+
34+
<!-- Added books section -->
35+
<section class="bg-white p-5 rounded max-w-xl mx-auto mt-5">
36+
<h4 class="font-semibold text-xl">Added Books (<span id="bookCount">0</span>)</h4>
37+
38+
<ul class="mt-3 space-y-2" id="renderedLibrary">
39+
40+
</ul>
41+
</section>
42+
43+
<script src="./assets/js/data/library.js"></script>
44+
<script src="./assets/js/data/book.js"></script>
45+
<script src="./assets/js/main.js"></script>
46+
47+
</body>
48+
49+
</html>

0 commit comments

Comments
 (0)
Please sign in to comment.