Skip to content
Open
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
83 changes: 48 additions & 35 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<!DOCTYPE html>
<html>
<!doctype html>
<html lang="en-US">
<head>
<title> </title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Book Library</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>
Expand All @@ -28,31 +25,48 @@ <h1>Library</h1>
</button>

<div id="demo" class="collapse">
<div class="form-group">
<label for="title">Title:</label>
<input
type="title"
class="form-control"
id="title"
name="title"
required
/>
<label for="author">Author: </label>
<input
type="author"
class="form-control"
id="author"
name="author"
required
/>
<label for="pages">Pages:</label>
<input
type="number"
class="form-control"
id="pages"
name="pages"
required
/>
<form id="book-form" novalidate>
<div class="form-group">
<label for="title">Title:</label>
<input
type="text"
class="form-control"
id="title"
name="title"
required
/>
<div class="invalid-feedback" id="title-feedback">
Please enter a title.
</div>
</div>
<div class="form-group">
<label for="author">Author: </label>
<input
type="text"
class="form-control"
id="author"
name="author"
required
/>
<div class="invalid-feedback" id="author-feedback">
Please enter an author.
</div>
</div>
<div class="form-group">
<label for="pages">Pages:</label>
<input
type="number"
class="form-control"
id="pages"
name="pages"
min="1"
step="1"
required
/>
<div class="invalid-feedback" id="pages-feedback">
Please enter a valid positive integer page count.
</div>
</div>
<label class="form-check-label">
<input
type="checkbox"
Expand All @@ -65,9 +79,8 @@ <h1>Library</h1>
type="submit"
value="Submit"
class="btn btn-primary"
onclick="submit();"
/>
</div>
</form>
</div>

<table class="table" id="display">
Expand Down
202 changes: 134 additions & 68 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,108 @@
let myLibrary = [];
const myLibrary = [];
const bookForm = document.getElementById("book-form");
const titleInput = document.getElementById("title");
const authorInput = document.getElementById("author");
const pagesInput = document.getElementById("pages");
const checkInput = document.getElementById("check");

window.addEventListener("load", function (e) {
window.addEventListener("load", function () {
populateStorage();
render();

if (bookForm) {
bookForm.addEventListener("submit", function (event) {
event.preventDefault();
submit();
});
}

if (titleInput) {
titleInput.addEventListener("input", function () {
titleInput.classList.remove("is-invalid");
});
}

if (authorInput) {
authorInput.addEventListener("input", function () {
authorInput.classList.remove("is-invalid");
});
}

if (pagesInput) {
pagesInput.addEventListener("input", function () {
pagesInput.classList.remove("is-invalid");
});
}
});

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

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;
const titleValue = titleInput.value.trim();
const authorValue = authorInput.value.trim();
const pagesValue = pagesInput.value.trim();
const pagesNumber = Number(pagesValue);

const isTitleInvalid = !titleValue;
const isAuthorInvalid = !authorValue;
const isPagesInvalid =
!pagesValue ||
isNaN(pagesNumber) ||
pagesNumber <= 0 ||
!Number.isInteger(pagesNumber);

if (isTitleInvalid) {
titleInput.classList.add("is-invalid");
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
render();
titleInput.classList.remove("is-invalid");
}

if (isAuthorInvalid) {
authorInput.classList.add("is-invalid");
} else {
authorInput.classList.remove("is-invalid");
}

if (isPagesInvalid) {
pagesInput.classList.add("is-invalid");
} else {
pagesInput.classList.remove("is-invalid");
}

if (isTitleInvalid || isAuthorInvalid || isPagesInvalid) {
return false;
}
Comment thread
tomdu3 marked this conversation as resolved.

const book = new Book(
titleValue,
authorValue,
pagesNumber,
checkInput.checked
);
myLibrary.push(book);

// Reset form values
if (bookForm) {
bookForm.reset();
}

render();
}

// Attach submit function to global window for inline onclick handler compatibility
window.submit = submit;

function Book(title, author, pages, check) {
this.title = title;
this.author = author;
Expand All @@ -51,53 +111,59 @@ function Book(title, author, pages, 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);
}
//insert updated row and cells
let length = myLibrary.length;
const tableBody = document.querySelector("#display tbody");
tableBody.innerHTML = "";

const 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 () {
const row = tableBody.insertRow();
const titleCell = row.insertCell(0);
const authorCell = row.insertCell(1);
const pagesCell = row.insertCell(2);
const wasReadCell = row.insertCell(3);
const deleteCell = row.insertCell(4);

titleCell.textContent = myLibrary[i].title;
authorCell.textContent = myLibrary[i].author;
pagesCell.textContent = myLibrary[i].pages;

// Read status toggle button
const toggleReadBtn = document.createElement("button");
toggleReadBtn.className = "btn btn-success";
toggleReadBtn.textContent = myLibrary[i].check ? "Yes" : "No";

toggleReadBtn.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
render();
});
wasReadCell.appendChild(toggleReadBtn);

//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 button
const deleteBtn = document.createElement("button");
deleteBtn.className = "btn btn-warning";
deleteBtn.textContent = "Delete";

deleteBtn.addEventListener("click", function () {
const deletedBook = myLibrary.splice(i, 1)[0];
render();

// Non-blocking banner / status message update after state change
showNotification(`You've deleted title: ${deletedBook.title}`);
});
deleteCell.appendChild(deleteBtn);
}
}

function showNotification(message) {
let noteEl = document.getElementById("notification");
if (!noteEl) {
noteEl = document.createElement("div");
noteEl.id = "notification";
noteEl.className = "alert alert-info mt-3";
document.querySelector(".container, body").prepend(noteEl);
}
noteEl.textContent = message;
setTimeout(() => {
noteEl.remove();
}, 3000);
}
3 changes: 1 addition & 2 deletions debugging/book-library/style.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.form-group {
#book-form {
width: 400px;
height: 300px;
align-self: left;
padding-left: 20px;
}
Expand Down
Loading