Skip to content

Commit 031d755

Browse files
committed
the bugs are fixed and add buttom is working
1 parent c89c066 commit 031d755

File tree

2 files changed

+184
-172
lines changed

2 files changed

+184
-172
lines changed

debugging/book-library/index.html

Lines changed: 81 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,88 @@
1-
<!DOCTYPE html>
2-
<html>
3-
<head>
4-
<title> </title>
5-
<meta
6-
charset="utf-8"
7-
name="viewport"
8-
content="width=device-width, initial-scale=1.0"
9-
/>
10-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
11-
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
12-
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
13-
<link
14-
rel="stylesheet"
15-
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
16-
/>
17-
<link rel="stylesheet" type="text/css" href="style.css" />
18-
</head>
1+
<!doctype html>
2+
<html lang="en">
193

20-
<body>
21-
<div class="jumbotron text-center">
22-
<h1>Library</h1>
23-
<p>Add books to your virtual library</p>
24-
</div>
4+
<head>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<title>My Book Library</title>
8+
9+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
10+
<style>
11+
body {
12+
padding: 1.5rem;
13+
}
14+
15+
.btn-small {
16+
padding: 0.25rem 0.5rem;
17+
font-size: 0.9rem;
18+
}
19+
</style>
20+
</head>
21+
22+
<body>
23+
<div class="jumbotron text-center">
24+
<h1 class="display-4">Library</h1>
25+
<p class="lead">Add books to your virtual library</p>
26+
</div>
27+
28+
<button class="btn btn-info mb-3" data-toggle="collapse" data-target="#addForm">
29+
Add new book
30+
</button>
31+
32+
<div id="addForm" class="collapse">
33+
<div class="card card-body mb-4">
34+
<div class="form-row">
2535

26-
<button data-toggle="collapse" data-target="#demo" class="btn btn-info">
27-
Add new book
28-
</button>
36+
<div class="form-group col-md-4">
37+
<label for="title">Title</label>
38+
<input id="title" class="form-control" type="text" required>
39+
</div>
2940

30-
<div id="demo" class="collapse">
31-
<div class="form-group">
32-
<label for="title">Title:</label>
33-
<input
34-
type="title"
35-
class="form-control"
36-
id="title"
37-
name="title"
38-
required
39-
/>
40-
<label for="author">Author: </label>
41-
<input
42-
type="author"
43-
class="form-control"
44-
id="author"
45-
name="author"
46-
required
47-
/>
48-
<label for="pages">Pages:</label>
49-
<input
50-
type="number"
51-
class="form-control"
52-
id="pages"
53-
name="pages"
54-
required
55-
/>
56-
<label class="form-check-label">
57-
<input
58-
type="checkbox"
59-
class="form-check-input"
60-
id="check"
61-
value=""
62-
/>Read
63-
</label>
64-
<input
65-
type="submit"
66-
value="Submit"
67-
class="btn btn-primary"
68-
onclick="submit();"
69-
/>
41+
<div class="form-group col-md-4">
42+
<label for="author">Author</label>
43+
<input id="author" class="form-control" type="text" required>
44+
</div>
45+
46+
<div class="form-group col-md-2">
47+
<label for="pages">Pages</label>
48+
<input id="pages" class="form-control" type="number" min="1" required>
49+
</div>
50+
51+
<div class="form-group col-md-2 d-flex align-items-end">
52+
<div class="form-check">
53+
<input id="check" class="form-check-input" type="checkbox">
54+
<label class="form-check-label" for="check">Read</label>
55+
</div>
56+
</div>
57+
</div>
58+
59+
<div class="text-right">
60+
<button id="addBtn" class="btn btn-primary">Submit</button>
7061
</div>
7162
</div>
63+
</div>
64+
65+
<table class="table table-striped" id="display">
66+
<thead class="thead-dark">
67+
<tr>
68+
<th>Title</th>
69+
<th>Author</th>
70+
<th>Pages</th>
71+
<th>Read</th>
72+
<th></th>
73+
</tr>
74+
</thead>
75+
<tbody>
76+
<!-- books appear here -->
77+
</tbody>
78+
</table>
79+
80+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
81+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
82+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
7283

73-
<table class="table" id="display">
74-
<thead class="thead-dark">
75-
<tr>
76-
<th>Title</th>
77-
<th>Author</th>
78-
<th>Number of Pages</th>
79-
<th>Read</th>
80-
<th></th>
81-
</tr>
82-
</thead>
83-
<tbody>
84-
<tr>
85-
<td></td>
86-
<td></td>
87-
<td></td>
88-
<td></td>
89-
<td></td>
90-
</tr>
91-
</tbody>
92-
</table>
84+
<!-- YOUR JS FILE -->
85+
<script src="script.js"></script>
86+
</body>
9387

94-
<script src="script.js"></script>
95-
</body>
96-
</html>
88+
</html>

debugging/book-library/script.js

Lines changed: 103 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,123 @@
1+
// Local storage key
2+
const STORAGE_KEY = "myLibraryBooks";
3+
4+
// Book list
15
let myLibrary = [];
26

3-
window.addEventListener("load", function (e) {
4-
populateStorage();
7+
// Cached DOM elements
8+
const title = document.getElementById("title");
9+
const author = document.getElementById("author");
10+
const pages = document.getElementById("pages");
11+
const check = document.getElementById("check");
12+
const addBtn = document.getElementById("addBtn");
13+
const displayBody = document.querySelector("#display tbody");
14+
15+
function Book(title, author, pages, read) {
16+
this.title = title;
17+
this.author = author;
18+
this.pages = pages;
19+
this.read = !!read;
20+
}
21+
22+
// Load from localStorage on page start
23+
window.addEventListener("DOMContentLoaded", () => {
24+
loadLibrary();
525
render();
626
});
727

8-
function populateStorage() {
9-
if (myLibrary.length == 0) {
10-
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
11-
let book2 = new Book(
12-
"The Old Man and the Sea",
13-
"Ernest Hemingway",
14-
"127",
15-
true
16-
);
17-
myLibrary.push(book1);
18-
myLibrary.push(book2);
19-
render();
28+
// Load saved books or create default sample books
29+
function loadLibrary() {
30+
const stored = localStorage.getItem(STORAGE_KEY);
31+
32+
if (stored) {
33+
myLibrary = JSON.parse(stored);
34+
} else {
35+
// First-time sample books
36+
myLibrary = [
37+
new Book("Robinson Crusoe", "Daniel Defoe", 252, true),
38+
new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true)
39+
];
40+
saveLibrary();
2041
}
2142
}
2243

23-
const title = document.getElementById("title");
24-
const author = document.getElementById("author");
25-
const pages = document.getElementById("pages");
26-
const check = document.getElementById("check");
44+
// Save books to localStorage
45+
function saveLibrary() {
46+
localStorage.setItem(STORAGE_KEY, JSON.stringify(myLibrary));
47+
}
2748

28-
//check the right input from forms and if its ok -> add the new book (object in array)
29-
//via Book function and start render function
30-
function submit() {
31-
if (
32-
title.value == null ||
33-
title.value == "" ||
34-
pages.value == null ||
35-
pages.value == ""
36-
) {
49+
// Clear form after adding
50+
function clearForm() {
51+
title.value = "";
52+
author.value = "";
53+
pages.value = "";
54+
check.checked = false;
55+
}
56+
57+
// Submit new book
58+
function submitBook() {
59+
if (!title.value.trim() || !author.value.trim() || !pages.value) {
3760
alert("Please fill all fields!");
38-
return false;
39-
} else {
40-
let book = new Book(title.value, title.value, pages.value, check.checked);
41-
library.push(book);
42-
render();
61+
return;
4362
}
44-
}
4563

46-
function Book(title, author, pages, check) {
47-
this.title = title;
48-
this.author = author;
49-
this.pages = pages;
50-
this.check = check;
64+
const book = new Book(
65+
title.value.trim(),
66+
author.value.trim(),
67+
Number(pages.value),
68+
check.checked
69+
);
70+
71+
myLibrary.push(book);
72+
saveLibrary();
73+
render();
74+
clearForm();
75+
76+
// Collapse form
77+
$("#addForm").collapse("hide");
5178
}
5279

80+
addBtn.addEventListener("click", submitBook);
81+
82+
// Render table
5383
function render() {
54-
let table = document.getElementById("display");
55-
let rowsNumber = table.rows.length;
56-
//delete old table
57-
for (let n = rowsNumber - 1; n > 0; n-- {
58-
table.deleteRow(n);
59-
}
60-
//insert updated row and cells
61-
let length = myLibrary.length;
62-
for (let i = 0; i < length; i++) {
63-
let row = table.insertRow(1);
64-
let titleCell = row.insertCell(0);
65-
let authorCell = row.insertCell(1);
66-
let pagesCell = row.insertCell(2);
67-
let wasReadCell = row.insertCell(3);
68-
let deleteCell = row.insertCell(4);
69-
titleCell.innerHTML = myLibrary[i].title;
70-
authorCell.innerHTML = myLibrary[i].author;
71-
pagesCell.innerHTML = myLibrary[i].pages;
72-
73-
//add and wait for action for read/unread button
74-
let changeBut = document.createElement("button");
75-
changeBut.id = i;
76-
changeBut.className = "btn btn-success";
77-
wasReadCell.appendChild(changeBut);
78-
let readStatus = "";
79-
if (myLibrary[i].check == false) {
80-
readStatus = "Yes";
81-
} else {
82-
readStatus = "No";
83-
}
84-
changeBut.innerText = readStatus;
85-
86-
changeBut.addEventListener("click", function () {
87-
myLibrary[i].check = !myLibrary[i].check;
84+
displayBody.innerHTML = "";
85+
86+
myLibrary.forEach((b, i) => {
87+
const row = document.createElement("tr");
88+
89+
row.innerHTML = `
90+
<td>${b.title}</td>
91+
<td>${b.author}</td>
92+
<td>${b.pages}</td>
93+
<td></td>
94+
<td></td>
95+
`;
96+
97+
// Read button
98+
const readBtn = document.createElement("button");
99+
readBtn.className = "btn btn-sm btn-success btn-small";
100+
readBtn.textContent = b.read ? "Yes" : "No";
101+
readBtn.addEventListener("click", () => {
102+
myLibrary[i].read = !myLibrary[i].read;
103+
saveLibrary();
88104
render();
89105
});
106+
row.children[3].appendChild(readBtn);
90107

91-
//add delete button to every row and render again
92-
let delButton = document.createElement("button");
93-
delBut.id = i + 5;
94-
deleteCell.appendChild(delBut);
95-
delBut.className = "btn btn-warning";
96-
delBut.innerHTML = "Delete";
97-
delBut.addEventListener("clicks", function () {
98-
alert(`You've deleted title: ${myLibrary[i].title}`);
99-
myLibrary.splice(i, 1);
100-
render();
108+
// Delete button
109+
const delBtn = document.createElement("button");
110+
delBtn.className = "btn btn-sm btn-warning btn-small";
111+
delBtn.textContent = "Delete";
112+
delBtn.addEventListener("click", () => {
113+
if (confirm(`Delete "${b.title}"?`)) {
114+
myLibrary.splice(i, 1);
115+
saveLibrary();
116+
render();
117+
}
101118
});
102-
}
119+
row.children[4].appendChild(delBtn);
120+
121+
displayBody.appendChild(row);
122+
});
103123
}

0 commit comments

Comments
 (0)