I am currently working through the Javascript portion of the Odin Project curriculum and have completed the library app project. As far as I can see everything is working as expected and I have completed all the object requirements (users can add books to their library, delete books, and update the 'read' status of each book).
I am now looking for some help/feedback on my code so that I can improve for future projects. Specifically, I am looking for some advice on how I can implement the SOLID principles and OOP within the project, but any general feedback/do's and don'ts would be greatly appreciated as well.
Here is my Javascript code:
let bookArray = [];
class Book {
constructor(title, author, numPages, status) {
this.title = title;
this.author = author;
this.numPages = numPages;
this.status = status;
}
}
//adds each book that the user inputs into 'database'
function addToLibrary(book) {
bookArray.push(book);
}
//add new DOM element each time book is added
function makeNewCard(array) {
let card = document.createElement("div");
let title = document.createElement("p");
let author = document.createElement("p");
let numPages = document.createElement("p");
let statusBtn = document.createElement("button");
let removeBookBtn = document.createElement("button");
title.setAttribute("class", "title");
author.setAttribute("class", "author");
numPages.setAttribute("class", "numPages");
statusBtn.setAttribute("class", "status");
card.setAttribute("class", "book-card");
removeBookBtn.setAttribute("class", "remove-book");
//displays book array contents on the page
for (let i = 0; i < array.length; i++) {
title.textContent = array[i]["title"];
author.textContent = array[i]["author"];
numPages.textContent = array[i]["numPages"];
statusBtn.textContent = array[i]["status"];
removeBookBtn.textContent = "Delete";
card.dataset.index = i;
}
statusBtn.textContent === "Read"
? (statusBtn.style.backgroundColor = "var(--read-book)")
: (statusBtn.style.backgroundColor = "var(--unread-book)");
statusBtn.addEventListener("click", (e) =>
//function is called with event target
updateStatus(e.target)
);
removeBookBtn.addEventListener("click", (e) => deleteBook(e.target));
card.append(title, author, numPages, statusBtn, removeBookBtn);
document.querySelector(".library-contents").appendChild(card);
}
const form = document.querySelector("form");
const inputTitle = document.querySelector("#book-title");
const inputAuthor = document.querySelector("#book-author");
const inputNumPages = document.querySelector("input[type=number]");
const checkBox = document.querySelector("#book-status");
const addNewBtn = document.querySelector(".add-new");
const overlay = document.querySelector(".overlay");
//new book is added when form is submitted
form.addEventListener("submit", function (e) {
e.preventDefault();
const book = new Book(
inputTitle.value,
inputAuthor.value,
inputNumPages.value,
checkBox.checked === true ? "Read" : "Unread"
);
addToLibrary(book);
console.log(bookArray);
makeNewCard(bookArray);
form.classList.toggle("form-visible");
overlay.classList.toggle("overlay-visible");
return book;
});
function updateStatus(button) {
const arrayPosition = parseInt(button.parentElement.dataset.index);
const selectedBook = bookArray[arrayPosition];
console.log(selectedBook);
if (selectedBook.status === "Read") {
selectedBook.status = "Unread";
button.style.backgroundColor = "var(--unread-book)";
} else {
selectedBook.status = "Read";
button.style.backgroundColor = "var(--read-book)";
}
console.log(selectedBook);
button.textContent = selectedBook.status;
}
function deleteBook(deleteButtton) {
const arrayPosition = parseInt(deleteButtton.parentElement.dataset.index);
bookArray.splice(arrayPosition, 1);
console.log(bookArray);
const selectedCard = deleteButtton.parentElement;
selectedCard.parentElement.removeChild(selectedCard);
//make array from remaining cards on screen and reassign a data value according
to their position on screen
let cards = document.querySelectorAll(".book-card");
console.log(cards);
for (let i = 0; i < cards.length; i++) {
console.log(i);
cards[i].dataset.index = i;
console.log(cards[i]);
}
}
addNewBtn.addEventListener("click", function () {
form.classList.toggle("form-visible");
overlay.classList.toggle("overlay-visible");
});
HTML File
Link to repository
Live Project
This is my first time asking for a code review so I do apologize if I have missed out any important information!