Skip to content

Commit 1ff7eb7

Browse files
committed
changes to render, saving, and naming
1 parent e7f2081 commit 1ff7eb7

File tree

1 file changed

+44
-55
lines changed

1 file changed

+44
-55
lines changed

debugging/book-library/script.js

Lines changed: 44 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,34 @@ if (localStorage.getItem("myLibrary")) {
99
render();
1010
});
1111
function populateStorage() {
12-
if (myLibrary.length == 0) {
13-
let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true);
14-
let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true);
12+
if (myLibrary.length === 0) {
13+
let book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true);
14+
let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true);
1515
myLibrary.push(book1);
1616
myLibrary.push(book2);
17-
render();
1817
}
1918
}
2019

21-
const title = document.getElementById("title");
22-
const author = document.getElementById("author");
23-
const pages = document.getElementById("pages");
24-
const check = document.getElementById("check");
20+
const titleInput = document.getElementById("title");
21+
const authorInput = document.getElementById("author");
22+
const pagesInput = document.getElementById("pages");
23+
const readCheckBox = document.getElementById("check");
2524

26-
//check the right input from forms and if its ok -> add the new book (object in array)
25+
//check the right input from forms and if its ok -> add the new book
2726
//via Book function and start render function
2827
function submit() {
29-
if (
30-
title.value == null ||
31-
title.value == "" ||
32-
author.value == null ||
33-
author.value == "" ||
34-
pages.value == null ||
35-
pages.value == ""
36-
) {
37-
alert("Please fill all fields!");
38-
return false;
39-
} else {
40-
let book = new Book(title.value, author.value, pages.value, check.checked);
28+
const title = titleInput.value.trim();
29+
const author = authorInput.value.trim();
30+
const pages = pagesInput.value.trim();
31+
32+
if (!title || !author || !pages || isNaN(pages) || pages <=0) {
33+
alert("Please fill all fields correctly!");
34+
return;
35+
}
36+
let book = new Book(title, author, Number(pages), readCheckBox.checked);
4137
myLibrary.push(book);
4238
render();
43-
}
39+
4440
}
4541

4642
class Book {
@@ -53,53 +49,46 @@ class Book {
5349
}
5450

5551
function render() {
56-
let table = document.getElementById("display");
57-
let rowsNumber = table.rows.length;
58-
//delete old table
59-
for (let n = rowsNumber - 1; n > 0; n--) {
60-
table.deleteRow(n);
61-
}
52+
const tableBody = document.querySelector("#display tbody");
53+
tableBody.innerHTML = "";
54+
6255
//insert updated row and cells
63-
let length = myLibrary.length;
64-
for (let i = 0; i < length; i++) {
65-
let row = table.insertRow(-1);
56+
for (let i = 0; i < myLibrary.length; i++) {
57+
let row = tableBody.insertRow(-1);
6658
let titleCell = row.insertCell(0);
6759
let authorCell = row.insertCell(1);
6860
let pagesCell = row.insertCell(2);
6961
let wasReadCell = row.insertCell(3);
7062
let deleteCell = row.insertCell(4);
71-
titleCell.innerHTML = myLibrary[i].title;
72-
authorCell.innerHTML = myLibrary[i].author;
73-
pagesCell.innerHTML = myLibrary[i].pages;
7463

75-
//add and wait for action for read/unread button
76-
let changeBut = document.createElement("button");
77-
changeBut.id = i;
78-
changeBut.className = "btn btn-success";
79-
wasReadCell.appendChild(changeBut);
80-
let readStatus = "";
81-
if (myLibrary[i].check == true) {
82-
readStatus = "Yes";
83-
} else {
84-
readStatus = "No";
85-
}
86-
changeBut.innerText = readStatus;
64+
titleCell.textContent= myLibrary[i].title;
65+
authorCell.textContent = myLibrary[i].author;
66+
pagesCell.textContent = myLibrary[i].pages;
67+
68+
let toggleBtn = document.createElement("button");
69+
toggleBtn.className = "btn btn-success";
70+
toggleBtn.textContent = myLibrary[i].check ? "Yes" : "No";
71+
wasReadCell.appendChild(toggleBtn);
8772

88-
changeBut.addEventListener("click", function () {
73+
toggleBtn.addEventListener("click", function () {
8974
myLibrary[i].check = !myLibrary[i].check;
9075
render();
9176
});
9277

9378
//add delete button to every row and render again
94-
let delBut = document.createElement("button");
95-
delBut.id = i + 5;
96-
deleteCell.appendChild(delBut);
97-
delBut.className = "btn btn-warning";
98-
delBut.innerHTML = "Delete";
99-
delBut.addEventListener("click", function () {
100-
alert(`You've deleted title: ${myLibrary[i].title}`);
79+
let delBtn = document.createElement("button");
80+
delBtn.className = "btn btn-warning";
81+
delBtn.textContent = "Delete";
82+
deleteCell.appendChild(delBtn);
83+
84+
delBtn.addEventListener("click", function () {
85+
if(confirm(`Are you sure you want to delete " ${myLibrary[i].title}"?`)){
10186
myLibrary.splice(i, 1);
10287
render();
88+
}
10389
});
10490
}
105-
}
91+
// Save updated library to localStorage
92+
localStorage.setItem("myLibrary", JSON.stringify(myLibrary));
93+
}
94+

0 commit comments

Comments
 (0)