Skip to content

Commit

Permalink
Feat: Refactor plain constructor of Book by class
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosfrontend committed Jan 9, 2024
1 parent 156dbed commit 0f56cab
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,26 @@ alt="book thumbnail"

let myLibrary = [];

// Create the Book constructor
// Create the Book class

function Book(title, author, isbn, src, pages, read) {
// Properties
this.title = title;
this.author = author;
this.isbn = isbn;
this.src = src;
this.pages = pages;
this.read = read;
this.id = self.crypto.randomUUID();
class Book {
// Constructor
constructor(title, author, isbn, src, pages, read) {
// Properties
this.title = title;
this.author = author;
this.isbn = isbn;
this.src = src;
this.pages = pages;
this.read = read;
this.id = self.crypto.randomUUID();
}
// Method
toggleRead() {
this.read === true ? (this.read = false) : (this.read = true);
return this.read;
}
}
Book.prototype.toggleRead = function () {
this.read === true ? (this.read = false) : (this.read = true);
return this.read;
};

// Create default books

Expand Down Expand Up @@ -245,18 +249,17 @@ function showBooks() {
}
// Function that remove books from myLibrary array from the delete button
function removeBookFromLibrary(event) {

for (let i = 0; i < myLibrary.length; i++) {
if (
myLibrary[i].id ===
event.target.parentNode.parentNode.parentNode.dataset.index
) {
if(confirm('Are you sure you want delete this Book?')){
myLibrary.splice(i, 1);
event.target.parentNode.parentNode.parentNode.remove();
}else{
return;
}
if (confirm("Are you sure you want delete this Book?")) {
myLibrary.splice(i, 1);
event.target.parentNode.parentNode.parentNode.remove();
} else {
return;
}
}
}
}
Expand All @@ -268,7 +271,7 @@ function updateStatus(event) {
? (event.target.textContent = "Readed")
: (event.target.textContent = "Unreaded");
if (event.target.textContent === "Unreaded") {
event.target.className = 'not-readed';
event.target.className = "not-readed";
event.target.parentElement.parentElement.classList.remove(
"less-contrast"
); // remove less-contrast class in the card
Expand All @@ -279,7 +282,7 @@ function updateStatus(event) {
"through"
); // remove author through class
} else {
event.target.className = 'readed';
event.target.className = "readed";
event.target.parentElement.parentElement.classList.add("less-contrast");
event.target.parentElement.parentElement.children[0].children[0].classList.add(
"through"
Expand Down

0 comments on commit 0f56cab

Please sign in to comment.