Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 6 additions & 29 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,9 @@ <h1>Library</h1>
<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
/>
<input type="text" class="form-control" id="title" name="title" required />
<label for="author">Author: </label>
<input
type="author"
class="form-control"
id="author"
name="author"
required
/>
<input type="text" class="form-control" id="author" name="author" required />
<label for="pages">Pages:</label>
<input
type="number"
Expand All @@ -61,12 +49,7 @@ <h1>Library</h1>
value=""
/>Read
</label>
<input
type="submit"
value="Submit"
class="btn btn-primary"
onclick="submit();"
/>
<input type="button" value="Submit" class="btn btn-primary" onclick="addBook();" />
</div>
</div>

Expand All @@ -80,15 +63,9 @@ <h1>Library</h1>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
<tbody>
</tbody>

</table>

<script src="script.js"></script>
Expand Down
197 changes: 114 additions & 83 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,134 @@
let myLibrary = [];

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

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();
}
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);
// Check the input from forms and, if valid, add the new book (object in array)
// via Book constructor and start render function
function addBook() {
if (
title.value == null ||
title.value === "" ||
pages.value == null ||
pages.value === ""
) {
showError("Please fill all fields!");
return false;
}

if (myLibrary.some(b => b.title === title.value && b.author === author.value)) {
showError("This book already exists!");
return;
}

if (!isNaN(title.value.trim()) || !isNaN(author.value.trim())) {
showError("Title and Author must be text!");
return;
}

let book = new Book(title.value, author.value, Number(pages.value), check.checked);
myLibrary.push(book);
render();
}
}

function Book(title, author, pages, check) {
this.title = title;
this.author = author;
this.pages = pages;
this.check = check;
// Clear any previous error messages
document.getElementById("errorMessage").style.display = "none";

function Book(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
}


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;
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";
let table = document.getElementById("display");
let rowsNumber = table.rows.length;

// Delete old table rows
for (let n = rowsNumber - 1; n > 0; n--) {
table.deleteRow(n);
}

// Insert updated rows 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 configure read/unread button
let changeBut = document.createElement("button");
changeBut.id = i;
changeBut.className = "btn btn-primary";
wasReadCell.appendChild(changeBut);

let readStatus = myLibrary[i].check ? "Yes" : "No";
changeBut.innerHTML = readStatus;

if (myLibrary[i].check) {
changeBut.className = "btn btn-success";
}

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

// Add delete button to each row
let delBut = document.createElement("button");
delBut.id = i + 5;
delBut.className = "btn btn-warning";
delBut.innerHTML = "Delete";
deleteCell.appendChild(delBut);

delBut.setAttribute("data-index", i);
delBut.addEventListener("click", function () {
let index = this.getAttribute("data-index");
showError(`You've deleted title: ${myLibrary[index].title}`);
myLibrary.splice(index, 1);
render();
});
}
changeBut.innerText = readStatus;

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

//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);
render();
});
}
}

// Handle error messages
function showError(message) {
let errorMessCont = document.getElementById("errorMessage");
errorMessCont.textContent = message;
errorMessCont.classList.add("show");

setTimeout(() => {
errorMessCont.classList.remove("show");
}, 3000);
}
21 changes: 21 additions & 0 deletions debugging/book-library/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,24 @@
button.btn-info {
margin: 20px;
}
#errorMessage {
margin-right: 0;
color: white;
background-color: hsla(0, 88%, 42%, 0.7);
padding: 20px;
border-radius: 5px;
width: 30%;
text-align: center;
font-weight: bold;
display:none ;
position: absolute;
z-index: 1000;
top: 20px;
right: 20px
}

#errorMessage.show {
display: block;
opacity: 1;
transform: translateX(0)
}
Loading