Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,71 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>Personal Library Manager</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="style.css">

<script src="script.js" type="module"></script>
</head>

<body>
<div class="jumbotron text-center">
<h1>Library</h1>
<p>Add books to your virtual library</p>
</div>

<div class="container">
<button data-toggle="collapse" data-target="#demo" class="btn btn-info mb-3">
Add new book
</button>

<div id="demo" class="collapse mb-4">
<div class="form-group">
<label for="title">Title:</label>
<input type="text" class="form-control" id="title" name="title" required>

<label for="author" class="mt-2">Author:</label>
<input type="text" class="form-control" id="author" name="author" required>

<label for="pages" class="mt-2">Pages:</label>
<input type="number" class="form-control" id="pages" name="pages" min="1" required>

<div class="form-check mt-3 mb-3">
<input type="checkbox" class="form-check-input" id="check">
<label class="form-check-label" for="check">Read</label>
</div>

<button id="submitBtn" class="btn btn-primary">Submit</button>
</div>
</div>

<table class="table" id="display">
<thead class="thead-dark">
<tr>
<th>Title</th>
<th>Author</th>
<th>Pages</th>
<th>Read Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="tableBody">
</tbody>
</table>
</div>
</body>

</html>

<!--

<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -94,3 +162,5 @@ <h1>Library</h1>
<script src="script.js"></script>
</body>
</html>

-->
166 changes: 84 additions & 82 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,21 @@
let myLibrary = [];
const myLibrary = [];

window.addEventListener("load", function (e) {
// DOM Element Selectors
const titleInput = document.getElementById("title");
const authorInput = document.getElementById("author");
const pagesInput = document.getElementById("pages");
const checkInput = document.getElementById("check");
const submitBtn = document.getElementById("submitBtn");
const tableBody = document.getElementById("tableBody");

// Initialize app
window.addEventListener("load", () => {
populateStorage();
render();
submitBtn.addEventListener("click", submitBook);
});

function populateStorage() {
if (myLibrary.length == 0) {
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
let book2 = new Book(
"The Old Man and the Sea",
"Ernest Hemingway",
"127",
true
);
myLibrary.push(book1);
myLibrary.push(book2);
render();
}
}

const title = document.getElementById("title");
const author = document.getElementById("author");
const pages = document.getElementById("pages");
const check = document.getElementById("check");

//check the right input from forms and if its ok -> add the new book (object in array)
//via Book function and start render function
function submit() {
if (
title.value == null ||
title.value == "" ||
pages.value == null ||
pages.value == ""
) {
alert("Please fill all fields!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
render();
}
}

function Book(title, author, pages, check) {
this.title = title;
Expand All @@ -50,54 +24,82 @@ function Book(title, author, pages, check) {
this.check = check;
}

function render() {
let table = document.getElementById("display");
let rowsNumber = table.rows.length;
//delete old table
for (let n = rowsNumber - 1; n > 0; n-- {
table.deleteRow(n);
function populateStorage() {
if (myLibrary.length === 0) {
// Note: pages are stored as numbers
const book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true);
const book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true);
myLibrary.push(book1, book2);
}
//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
let row = table.insertRow(1);
let titleCell = row.insertCell(0);
let authorCell = row.insertCell(1);
let pagesCell = row.insertCell(2);
let wasReadCell = row.insertCell(3);
let deleteCell = row.insertCell(4);
titleCell.innerHTML = myLibrary[i].title;
authorCell.innerHTML = myLibrary[i].author;
pagesCell.innerHTML = myLibrary[i].pages;

//add and wait for action for read/unread button
let changeBut = document.createElement("button");
changeBut.id = i;
changeBut.className = "btn btn-success";
wasReadCell.appendChild(changeBut);
let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "Yes";
} else {
readStatus = "No";
}
changeBut.innerText = readStatus;

changeBut.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
}

function submitBook() {
// Input Validation & Normalization
const titleVal = titleInput.value.trim();
const authorVal = authorInput.value.trim();
const pagesVal = parseInt(pagesInput.value);

// Reject empty strings or invalid page numbers
if (!titleVal || !authorVal || isNaN(pagesVal) || pagesVal <= 0) {
alert("Please provide a valid Title, Author, and Page Count.");
return;
}

const newBook = new Book(titleVal, authorVal, pagesVal, checkInput.checked);
myLibrary.push(newBook);

// Clear inputs
titleInput.value = "";
authorInput.value = "";
pagesInput.value = "";
checkInput.checked = false;

render();
}


function render() {
tableBody.innerHTML = "";

myLibrary.forEach((book, index) => {
// You can also use row = tableBody.insertRow() here!
const row = document.createElement("tr");

// Title Cell (Index 0)
const titleCell = row.insertCell(0);
titleCell.textContent = book.title;

// Author Cell (Index 1)
const authorCell = row.insertCell(1);
authorCell.textContent = book.author;

// Pages Cell (Index 2)
const pagesCell = row.insertCell(2);
pagesCell.textContent = book.pages;

// Read Status Cell (Index 3)
const statusCell = row.insertCell(3);
const statusBtn = document.createElement("button");
statusBtn.className = "btn btn-sm btn-success";
statusBtn.textContent = book.check ? "Yes" : "No";
statusBtn.addEventListener("click", () => {
book.check = !book.check;
render();
});
statusCell.appendChild(statusBtn);

//add delete button to every row and render again
let delButton = document.createElement("button");
delBut.id = i + 5;
deleteCell.appendChild(delBut);
delBut.className = "btn btn-warning";
delBut.innerHTML = "Delete";
delBut.addEventListener("clicks", function () {
alert(`You've deleted title: ${myLibrary[i].title}`);
myLibrary.splice(i, 1);
// Delete Cell (Index 4)
const deleteCell = row.insertCell(4);
const deleteBtn = document.createElement("button");
deleteBtn.className = "btn btn-sm btn-warning";
deleteBtn.textContent = "Delete";
deleteBtn.addEventListener("click", () => {
myLibrary.splice(index, 1);
render();
alert(`Deleted: ${book.title}`);
});
}
deleteCell.appendChild(deleteBtn);

tableBody.appendChild(row);
});
}
Loading