Skip to content

Commit 3dd9e9a

Browse files
updating code to accommodate PR.
1 parent 6980aeb commit 3dd9e9a

File tree

2 files changed

+66
-42
lines changed

2 files changed

+66
-42
lines changed

debugging/book-library/index.html

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,22 @@ <h1>Library</h1>
2222
</button>
2323

2424
<div id="demo" class="collapse">
25-
<div class="form-group">
26-
<label for="title">Title:</label>
27-
<input type="text" class="form-control" id="title" name="title" required>
28-
<label for="author">Author: </label>
29-
<input type="text" class="form-control" id="author" name="author" required>
30-
<label for="pages">Pages:</label>
31-
<input type="number" class="form-control" id="pages" name="pages" required>
32-
<label class="form-check-label">
33-
<input type="checkbox" class="form-check-input" id="check" value="">Read
34-
</label>
35-
<input type="submit" value="Submit" class="btn btn-primary" onclick="submit();">
36-
</div>
25+
<form id="addBookForm">
26+
<div class="form-group">
27+
<label for="title">Title:</label>
28+
<input type="text" class="form-control" id="title" name="title" required />
29+
<label for="author">Author: </label>
30+
<input type="text" class="form-control" id="author" name="author" required />
31+
<label for="pages">Pages:</label>
32+
<input type="number" class="form-control" id="pages" name="pages" required />
33+
<label class="form-check-label">
34+
<input type="checkbox" class="form-check-input" id="check" value="" />Read
35+
</label>
36+
<button type="submit" class="btn btn-primary">Submit</button>
37+
</div>
38+
</form>
3739
</div>
38-
40+
<div id="notification-container" class="mt-3"></div>
3941
<table class="table" id="display">
4042
<thead class="thead-dark">
4143
<tr>

debugging/book-library/script.js

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,74 @@ const titleInput = document.getElementById("title");
55
const authorInput = document.getElementById("author");
66
const pagesInput = document.getElementById("pages");
77
const checkInput = document.getElementById("check");
8+
const addBookForm = document.getElementById("addBookForm");
9+
810
// populate the library with some default books if it's empty
911
function populateStorage() {
1012
if (myLibrary.length === 0) {
1113
let book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true);
12-
let book2 = new Book("The Old Man and the Sea","Ernest Hemingway", 127, true);
14+
let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true);
1315
myLibrary.push(book1);
1416
myLibrary.push(book2);
15-
}
16-
}
17-
//check the right input from forms and if its ok -> add the new book (object in array)
18-
//via Book function and start render function
19-
function submit() {
20-
if (
21-
titleInput.value.trim == "" || // cant be null so removed all that
22-
authorInput.value.trim == "" ||
23-
pagesInput.value.trim == ""
24-
) {
25-
alert("Please fill all fields!");
26-
return false;
27-
} else {
28-
let newBook = new Book(titleInput.value, authorInput.value, parseInt(pagesInput.value), checkInput.checked);
29-
myLibrary.push(newBook);
30-
//clear form after adding newBook
31-
titleInput.value = '';
32-
authorInput.value = '';
33-
pagesInput.value = '';
34-
checkInput.value = false;
35-
render();
3617
}
3718
}
19+
20+
// this function is now exclusively for adding a new book to the array and rendering, replaced the old submit()
21+
function addBookToLibrary() {
22+
let newBook = new Book(
23+
titleInput.value,
24+
authorInput.value,
25+
parseInt(pagesInput.value),
26+
checkInput.checked
27+
);
28+
myLibrary.push(newBook);
29+
addBookForm.reset();
30+
render();
31+
}
32+
33+
addBookForm.addEventListener('submit', function (event) {
34+
event.preventDefault(); // prevents the default page reload
35+
addBookToLibrary();
36+
});
37+
3838
// book constructor
3939
function Book(title, author, pages, check) {
4040
this.title = title;
4141
this.author = author;
4242
this.pages = pages;
4343
this.check = check;
4444
}
45-
// renders lib data into HTML
45+
46+
function displayNotification(message) {
47+
const container = document.getElementById("notification-container");
48+
container.innerHTML = `<div class="alert alert-success alert-dismissible fade show" role="alert">
49+
${message}
50+
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
51+
<span aria-hidden="true">&times;</span>
52+
</button>
53+
</div>`;
54+
setTimeout(() => {
55+
const alert = container.querySelector('.alert');
56+
if (alert) {
57+
alert.classList.remove('show');
58+
alert.classList.add('fade');
59+
setTimeout(() =>
60+
alert.remove(), 150);
61+
}
62+
}, 3000); // 3 secs
63+
}
64+
65+
// renders library data into HTML
4666
function render() {
4767
let tableBody = document.querySelector("#display tbody");
48-
tableBody.innerHTML = ''; // get rid of all instead of one by one row deletion
49-
68+
tableBody.innerHTML = '';
69+
5070
myLibrary.forEach((book, index) => {
5171
let row = tableBody.insertRow();
52-
// post debate lets use textContent instead of innerHTML
5372
row.insertCell(0).textContent = book.title;
5473
row.insertCell(1).textContent = book.author;
5574
row.insertCell(2).textContent = book.pages;
75+
5676
// 'read' status
5777
const wasReadCell = row.insertCell(3);
5878
const readStatusBtn = document.createElement("button");
@@ -68,17 +88,19 @@ function render() {
6888
const deleteCell = row.insertCell(4);
6989
const deleteBtn = document.createElement("button");
7090
deleteBtn.className = "btn btn-warning";
71-
deleteBtn.innerHTML = "Delete";
91+
deleteBtn.textContent = "Delete";
7292
deleteCell.appendChild(deleteBtn);
93+
7394
deleteBtn.addEventListener("click", function () {
7495
const deletedTitle = myLibrary[index].title;
7596
myLibrary.splice(index, 1);
7697
render();
77-
alert(`You've deleted title: ${deletedTitle}`);
98+
displayNotification(`You've deleted "${deletedTitle}"`);
7899
});
79100
});
80101
}
81-
// moved to end to make sure all is defined before load
102+
103+
// ensures functions are defined before being called on page load
82104
window.addEventListener("load", function () {
83105
populateStorage();
84106
render();

0 commit comments

Comments
 (0)