Skip to content

Commit 6508e7a

Browse files
feat: add back UI and handler functions
1 parent 00f2edd commit 6508e7a

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

src/script.js

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,165 @@ class Library {
103103
}
104104
}
105105

106+
/*==========
107+
** Application State and Functions
108+
**/
109+
const myLibrary = new Library();
110+
111+
// Add new book to library
112+
function addBookToLibrary(title, author, pages, genre, read) {
113+
const newBook = new Book(title, author, pages, genre, read);
114+
myLibrary.addBook(newBook);
115+
return newBook;
116+
}
117+
118+
// Display all books
119+
function displayBooks() {
120+
const booksContainer = document.getElementById("books-container");
121+
122+
// Clear container
123+
booksContainer.innerHTML = "";
124+
125+
// Check if library empty
126+
if (myLibrary.isEmpty) {
127+
booksContainer.innerHTML = `
128+
<div class="empty-library">
129+
<i class="fas fa-book-open"></i>
130+
<h3>Your library is empty</h3>
131+
<p>Click "Add New Book" to start adding books to your collection!</p>
132+
</div>
133+
`;
134+
return;
135+
}
136+
137+
// Create and append cards to library
138+
myLibrary.books.forEach((book) => {
139+
const bookCard = createBookCard(book);
140+
booksContainer.appendChild(bookCard);
141+
});
142+
}
143+
144+
// Create book card element
145+
function createBookCard(book) {
146+
const bookCard = document.createElement("div");
147+
bookCard.className = "book-card";
148+
bookCard.dataset.bookId = book.id;
149+
150+
bookCard.innerHTML = `
151+
<div class="book-header">
152+
<h3 class="book-title">${book.title}</h3>
153+
<span class="book-status ${book.statusClass}">${
154+
book.statusText
155+
}</span>
156+
</div>
157+
<p class="book-author">${book.formattedAuthor}</p>
158+
<div class="book-details">
159+
<div class="book-detail">
160+
<span class="book-detail-label">Pages:</span>
161+
<span class="book-detail-value">${book.pages}</span>
162+
</div>
163+
${
164+
book.genre
165+
? `
166+
<div class="book-detail">
167+
<span class="book-detail-label">Genre:</span>
168+
<span class="book-detail-value">${book.genre}</span>
169+
</div>
170+
`
171+
: ""
172+
}
173+
</div>
174+
<div class="book-actions">
175+
<button class="btn-success toggle-read-btn" data-book-id="${
176+
book.id
177+
}">
178+
<i class="fas fa-book-reader"></i> ${
179+
book.read ? "Mark Unread" : "Mark Read"
180+
}
181+
</button>
182+
<button class="btn-danger remove-btn" data-book-id="${book.id}">
183+
<i class="fas fa-trash"></i> Remove
184+
</button>
185+
</div>
186+
`;
187+
188+
return bookCard;
189+
}
190+
191+
// Handle form submission
192+
function handleFormSubmit(event) {
193+
event.preventDefault();
194+
195+
// Get form values
196+
const title = document.getElementById("title").value.trim();
197+
const author = document.getElementById("author").value.trim();
198+
const pages = parseInt(document.getElementById("pages").value);
199+
const genre = document.getElementById("genre").value.trim();
200+
const read = document.getElementById("read").checked;
201+
202+
// Validate fields
203+
if (!title || !author || !pages) {
204+
alert("Please fill in all required fields (Title, Author, and Pages)");
205+
return;
206+
}
207+
208+
// Add book to library
209+
addBookToLibrary(title, author, pages, genre, read);
210+
211+
// Reset form
212+
document.getElementById("book-form").reset();
213+
214+
// Close modal
215+
document.getElementById("book-form-modal").close();
216+
217+
// Update display
218+
displayBooks();
219+
}
220+
221+
// Remove book
222+
function handleRemoveBook(event) {
223+
if (!event.target.closest(".remove-btn")) return;
224+
225+
const bookId = event.target.closest(".remove-btn").dataset.bookId;
226+
227+
// Confirm deletion
228+
if (confirm("Are you sure you want to remove this book from your library?")) {
229+
// Remove book using Library class method
230+
const removed = myLibrary.removeBook(bookId);
231+
232+
if (removed) {
233+
// Update display
234+
displayBooks();
235+
}
236+
}
237+
}
238+
239+
// Handle toggle read
240+
function handleToggleReadStatus(event) {
241+
if (!event.target.closest(".toggle-read-btn")) return;
242+
243+
const bookId = event.target.closest(".toggle-read-btn").dataset.bookId;
244+
245+
// Find book in library using Library class method
246+
const book = myLibrary.findBookById(bookId);
247+
248+
if (book) {
249+
// Toggle read using Book class method
250+
book.toggleReadStatus();
251+
252+
// Update display
253+
displayBooks();
254+
}
255+
}
256+
257+
// Add sample books
258+
function initializeSampleBooks() {
259+
// Add sample books to library
260+
addBookToLibrary("The Hobbit", "J.R.R. Tolkien", 310, "Fantasy", true);
261+
addBookToLibrary("To Kill a Mockingbird", "Harper Lee", 281, "Fiction", true);
262+
addBookToLibrary("1984", "George Orwell", 328, "Dystopian", false);
263+
addBookToLibrary("Pride and Prejudice", "Jane Austen", 432, "Classic", true);
264+
265+
// Display initial books
266+
displayBooks();
267+
}

0 commit comments

Comments
 (0)