Skip to content

Commit a796835

Browse files
committed
bug fixes
1 parent 3be304c commit a796835

File tree

2 files changed

+88
-68
lines changed

2 files changed

+88
-68
lines changed

debugging/book-library/index.html

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,50 +27,49 @@ <h1>Library</h1>
2727
Add new book
2828
</button>
2929

30-
<div id="demo" class="collapse">
31-
<form>
32-
<div class="form-group">
33-
<label for="title">Title:</label>
30+
<div id="demo" class="collapse">
31+
<form id="book-form">
32+
<div class="form-group">
33+
<label for="title">Title:</label>
34+
<input
35+
type="text"
36+
class="form-control"
37+
id="title"
38+
name="title"
39+
required
40+
/>
41+
<label for="author">Author:</label>
42+
<input
43+
type="text"
44+
class="form-control"
45+
id="author"
46+
name="author"
47+
required
48+
/>
49+
<label for="pages">Pages:</label>
50+
<input
51+
type="number"
52+
class="form-control"
53+
id="pages"
54+
name="pages"
55+
required
56+
/>
57+
<label class="form-check-label">
3458
<input
35-
type="text"
36-
class="form-control"
37-
id="title"
38-
name="title"
39-
required
40-
/>
41-
<label for="author">Author: </label>
42-
<input
43-
type="text"
44-
class="form-control"
45-
id="author"
46-
name="author"
47-
required
48-
/>
49-
<label for="pages">Pages:</label>
50-
<input
51-
type="number"
52-
class="form-control"
53-
id="pages"
54-
name="pages"
55-
required
56-
/>
57-
<label class="form-check-label">
58-
<input
59-
type="checkbox"
60-
class="form-check-input"
61-
id="check"
62-
value=""
63-
/>Read
64-
</label>
65-
<input
66-
type="submit"
67-
value="Submit"
68-
class="btn btn-primary"
69-
onclick="submit();"
70-
/>
71-
</div>
72-
</form>
59+
type="checkbox"
60+
class="form-check-input"
61+
id="check"
62+
value=""
63+
/>Read
64+
</label>
65+
<input
66+
type="submit"
67+
value="Submit"
68+
class="btn btn-primary"
69+
/>
7370
</div>
71+
</form>
72+
</div>
7473

7574
<table class="table" id="display">
7675
<thead class="thead-dark">

debugging/book-library/script.js

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
let myLibrary = [];
22

3-
window.addEventListener("load", function (e) {
3+
window.addEventListener("load", function () {
4+
// console.log("Page loaded, initializing library");
45
populateStorage();
56
render();
67
});
78

9+
// Attach form submit event listener
10+
document.getElementById("book-form").addEventListener("submit", function (event) {
11+
// console.log("Form submit event triggered");
12+
event.preventDefault(); // Prevent default form submission
13+
submit();
14+
});
15+
816
function populateStorage() {
9-
if (myLibrary.length === 0) {
17+
const storedLibrary = localStorage.getItem("myLibrary");
18+
if (storedLibrary) {
19+
// console.log("Loading myLibrary from localStorage:", storedLibrary);
20+
myLibrary = JSON.parse(storedLibrary);
21+
} else {
22+
// console.log("No data in localStorage, initializing with default books");
1023
let book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true);
1124
let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true);
12-
myLibrary.push(book1);
13-
myLibrary.push(book2);
14-
render();
25+
myLibrary.push(book1, book2);
26+
localStorage.setItem("myLibrary", JSON.stringify(myLibrary));
1527
}
28+
render();
1629
}
1730

1831
const title = document.getElementById("title");
@@ -21,21 +34,30 @@ const pages = document.getElementById("pages");
2134
const check = document.getElementById("check");
2235

2336
function submit() {
24-
if (
25-
!title.value ||
26-
!author.value ||
27-
!pages.value
28-
) {
29-
alert("Please fill all fields!");
30-
return false;
31-
} else {
32-
let book = new Book(title.value, author.value, pages.value, check.checked);
33-
myLibrary.push(book);
34-
render();
35-
// Reset form and collapse
36-
document.querySelector("form").reset();
37-
$("#demo").collapse("hide");
38-
}
37+
// console.log("submit() function called");
38+
// console.log("Input values:", {
39+
// title: title.value,
40+
// author: author.value,
41+
// pages: pages.value,
42+
// read: check.checked
43+
// });
44+
45+
// if (!title.value.trim() || !author.value.trim() || !pages.value || isNaN(parseInt(pages.value))) {
46+
// console.log("Validation failed: One or more fields are empty or invalid");
47+
// alert("Please fill all fields with valid data!");
48+
// return;
49+
// }
50+
51+
let book = new Book(title.value.trim(), author.value.trim(), parseInt(pages.value), check.checked);
52+
// console.log("New book created:", book);
53+
myLibrary.push(book);
54+
// console.log("myLibrary after push:", myLibrary);
55+
localStorage.setItem("myLibrary", JSON.stringify(myLibrary));
56+
render();
57+
document.querySelector("form").reset();
58+
// console.log("Form reset");
59+
$("#demo").collapse("hide");
60+
// console.log("Form collapsed");
3961
}
4062

4163
function Book(title, author, pages, check) {
@@ -46,15 +68,14 @@ function Book(title, author, pages, check) {
4668
}
4769

4870
function render() {
71+
// console.log("render() called, myLibrary:", myLibrary);
4972
let table = document.getElementById("display");
5073
let rowsNumber = table.rows.length;
51-
// Delete old table rows (except header)
5274
for (let n = rowsNumber - 1; n > 0; n--) {
5375
table.deleteRow(n);
5476
}
55-
// Insert updated rows
5677
for (let i = 0; i < myLibrary.length; i++) {
57-
let row = table.insertRow(-1); // Append at the end of tbody
78+
let row = table.insertRow(-1);
5879
let titleCell = row.insertCell(0);
5980
let authorCell = row.insertCell(1);
6081
let pagesCell = row.insertCell(2);
@@ -64,18 +85,17 @@ function render() {
6485
authorCell.innerHTML = myLibrary[i].author;
6586
pagesCell.innerHTML = myLibrary[i].pages;
6687

67-
// Read/Unread button
6888
let changeBut = document.createElement("button");
6989
changeBut.id = `read-${i}`;
7090
changeBut.className = "btn btn-success";
71-
wasReadCell.appendChild(changeBut);
7291
changeBut.innerText = myLibrary[i].check ? "Yes" : "No";
92+
wasReadCell.appendChild(changeBut);
7393
changeBut.addEventListener("click", function () {
7494
myLibrary[i].check = !myLibrary[i].check;
95+
localStorage.setItem("myLibrary", JSON.stringify(myLibrary));
7596
render();
7697
});
7798

78-
// Delete button
7999
let delButton = document.createElement("button");
80100
delButton.id = `delete-${i}`;
81101
delButton.className = "btn btn-warning";
@@ -84,6 +104,7 @@ function render() {
84104
delButton.addEventListener("click", function () {
85105
alert(`You've deleted title: ${myLibrary[i].title}`);
86106
myLibrary.splice(i, 1);
107+
localStorage.setItem("myLibrary", JSON.stringify(myLibrary));
87108
render();
88109
});
89110
}

0 commit comments

Comments
 (0)