Skip to content

London | ITP-May-25 | Emiliano Uruena | Data-Flows Sprint-2 | Book-Library 📚 #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
30 changes: 10 additions & 20 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<!DOCTYPE html>
<html>
<html lang="en">
<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 @@ -31,15 +28,15 @@ <h1>Library</h1>
<div class="form-group">
<label for="title">Title:</label>
<input
type="title"
type="text"
class="form-control"
id="title"
name="title"
required
/>
<label for="author">Author: </label>
<input
type="author"
type="text"
class="form-control"
id="author"
name="author"
Expand Down Expand Up @@ -73,21 +70,14 @@ <h1>Library</h1>
<table class="table" id="display">
<thead class="thead-dark">
<tr>
<th>Title</th>
<th>Author</th>
<th>Number of Pages</th>
<th>Read</th>
<th id="tab-title">Title</th>
<th id="tab-author">Author</th>
<th id="tab-pages">Number of Pages</th>
<th id="tab-check">Read</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>

Expand Down
60 changes: 29 additions & 31 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ let myLibrary = [];

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

function populateStorage() {
if (myLibrary.length == 0) {
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
let book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true);
let book2 = new Book(
"The Old Man and the Sea",
"Ernest Hemingway",
"127",
127,
true
);
myLibrary.push(book1);
Expand All @@ -20,25 +20,26 @@ function populateStorage() {
}
}

const title = document.getElementById("title");
const author = document.getElementById("author");
const pages = document.getElementById("pages");
const check = document.getElementById("check");
const titleEl = document.getElementById("title");
const authorEl = document.getElementById("author");
const pagesEl = document.getElementById("pages");
const checkEl = 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() {
const pageNumber = Number(pages.value);
if (
title.value == null ||
title.value == "" ||
pages.value == null ||
pages.value == ""
titleEl.value.trim() === "" ||
authorEl.value.trim() === "" ||
isNaN(pageNumber) ||
pageNumber <= 0
) {
alert("Please fill all fields!");
alert("Please fill all fields correctly!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
let book = new Book(titleEl.value.toLowerCase().replace(/\b\w/g, s => s.toUpperCase()), authorEl.value.toLowerCase().replace(/\b\w/g, s => s.toUpperCase()), pageNumber, checkEl.checked);
myLibrary.push(book);
render();
}
}
Expand All @@ -52,15 +53,17 @@ 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);
let tbody = table.getElementsByTagName("tbody")[0];

// Set the innerHTML of the tbody to an empty string to clear all rows
if (tbody) {
tbody.innerHTML = "";
}

//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
let row = table.insertRow(1);
let row = tbody.insertRow();
let titleCell = row.insertCell(0);
let authorCell = row.insertCell(1);
let pagesCell = row.insertCell(2);
Expand All @@ -75,28 +78,23 @@ function render() {
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.innerText = myLibrary[i].check ? "Yes" : "No";

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;
let delBut = document.createElement("button");
deleteCell.appendChild(delBut);
delBut.className = "btn btn-warning";
delBut.innerHTML = "Delete";
delBut.addEventListener("clicks", function () {
alert(`You've deleted title: ${myLibrary[i].title}`);
delBut.innerText = "Delete";
delBut.addEventListener("click", function () {
const nameBookDeleted = myLibrary[i].title;
myLibrary.splice(i, 1);
alert(`You've deleted title: ${nameBookDeleted}`);
render();
});
}
Expand Down