Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
1d58b64
added index.html + index.js + styles.css
Jeelex Apr 22, 2021
d0fa939
changed index.html skeleton file + added js code from SHA
Jeelex Apr 22, 2021
d9f932d
added function Book() from previous exercise
Jeelex Apr 22, 2021
75f0f06
changed boolean property name
Jeelex Apr 22, 2021
92c2b31
added more books
Jeelex Apr 22, 2021
c6ba906
added bootstrap link + added dropdown list
Jeelex Apr 22, 2021
0eb06a2
added header + footer + height 100vh
Jeelex Apr 23, 2021
04bf79a
added book template
Jeelex Apr 23, 2021
f0bd906
fixed title + btn new book
Jeelex Apr 23, 2021
3c70333
renamed folder and started html from scratch
Jeelex Apr 23, 2021
08fd520
started creating render function
Jeelex Apr 24, 2021
b47e3cf
completed render function
Jeelex Apr 24, 2021
0de5c7b
added btnNewBook which makes form appear when clicked
Jeelex Apr 24, 2021
6ce30f6
added helper funcion for querySelector
Jeelex Apr 24, 2021
654f812
deleted for loop fron render function
Jeelex Apr 24, 2021
8e2f9dc
added clearForm()
Jeelex Apr 24, 2021
8c9ac7d
created event listener to grab clicks inside table
Jeelex Apr 24, 2021
4b0717f
added event listener to remove table row when REMOVE button is clicked
Jeelex Apr 24, 2021
e30a1b6
trying to pinpoint books inside myLibrary when deleting from the DOM
Jeelex Apr 24, 2021
3a7f0dd
added .closest() to find the parent of remove btn
Jeelex Apr 25, 2021
bfa53ff
fixed rended() -> now it loops through array and displays books in DOM
Jeelex Apr 25, 2021
acfa0be
swapped if statement from render() with ternary operator
Jeelex Apr 25, 2021
8e49d0f
made input placeholders uppercase
Jeelex Apr 25, 2021
48a4c94
swapped for of loop for a classic for loop to add 'data-index' when b…
Jeelex Apr 26, 2021
6397e35
added function removeBookFromLibrary()
Jeelex Apr 26, 2021
b87ce23
code tyding and refactoring
Jeelex Apr 26, 2021
794298b
added function toggleStatus() inside BOOK prototype
Jeelex Apr 26, 2021
dba8650
added function toggleBookStatus() which changes the DOM and the book …
Jeelex Apr 26, 2021
7daf25e
code tyding and refactoring
Jeelex Apr 26, 2021
45a775e
created function saveToLocalStorage() + function checkLocalStorage(ar…
Jeelex Apr 26, 2021
32b08a7
hardcoded 1st book in the html
Jeelex Apr 26, 2021
80098e9
refactored addBookToLibrary() + removed hardcoded html table row with…
Jeelex Apr 26, 2021
adada63
refactored addBookToLibrary() into pure function + completed localSto…
Jeelex Apr 27, 2021
25ffb3d
refactoring + when localStorage is empty the page renders Harry Potter
Jeelex Apr 27, 2021
550df1c
code tyding in index.html
Jeelex Apr 27, 2021
d56862f
added bootstrap styling + some JS refactoring with mentor + html update
Jeelex Apr 27, 2021
afed600
fixed btn + form + added h2 + added function to add display none
Jeelex Apr 27, 2021
d28a13e
code tyding + removed querySelector function
Jeelex Apr 27, 2021
280398f
code tyding
Jeelex Apr 27, 2021
e7e5cda
added browser tab icon
Jeelex May 4, 2021
fa77fb8
added meta image + title for website thumbnail to work
Jeelex May 4, 2021
672f4c5
changed meta image for website thumbnail to work
Jeelex May 4, 2021
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
53 changes: 53 additions & 0 deletions Project Library/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="title" content="Project Library" />
<meta name="image" property="og:image" content="library-icon.png" href="library-icon.png" />
<meta name="description" content="Project Library for Social Hackers Academy" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="styles.css" />
<link rel="icon" type="image" href="library-icon.png">
<title>Project Library</title>
</head>
<body class="container bg-light ">
<section class="container mt-5 justify-content-center text-center">
<h1>A Personal Library inside your Browser</h1>
<h2 class="mt-4">Add Books to the list below!</h2>
<button class="mt-5 mb-5 btn btn-primary btn-lg" id="btn-new-book" type="button">NEW BOOK!</button>
</section>

<form class="mt-4 col-md-6 mx-auto text-center " id="form">
<div>
<input class="form-control text-center fs-5" type="text" id="title" placeholder="Title" required aria-required="true" />
</div>
<div class="mt-2">
<input class="form-control text-center fs-5" type="text" id="author" placeholder="Author" required aria-required="true"/>
</div>
<div class="mt-2">
<input class="form-control text-center fs-5" type="number" id="pages" placeholder="Pages" />
</div>
<div class="form-check form-check-inline mt-2">
<input class="form-check-input fs-5" type="checkbox" id="status" />
<label class="form-check-label fs-5 " for="status"> Read </label>
</div>
<div>
<button class="btn btn-primary mt-2" id="btn-add-book" type="submit">Add Book!</button>
</div>
</form>

<table class="table table-hover text-center mt-5 " id="table-library">
<thead class="table-dark">
<th>Book Title</th>
<th>Author</th>
<th>Page No</th>
<th>Have your read it?</th>
<th>Remove Book?</th>
</thead>
<tbody class="table-secondary"></tbody>
</table>

<script src="index.js"></script>
</body>
</html>
166 changes: 166 additions & 0 deletions Project Library/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
"use strict";

let myLibrary = [];
const form = document.querySelector("#form");
const bookTitle = document.querySelector("#title");
const bookAuthor = document.querySelector("#author");
const bookPages = document.querySelector("#pages");
const bookStatus = document.querySelector("#status");
const btnNewBook = document.querySelector("#btn-new-book");
const btnAddBook = document.querySelector("#btn-add-book");
const tableBody = document.querySelector("#table-library > tbody");

// Book Object constructor
function Book(title, author, pages, status) {
this.title = title;
this.author = author;
this.pages = pages;
this.status = status;
this.toggleStatus = function () {
this.status = !this.status;
};
this.info = function () {
if (!status) {
return `${this.title} by ${this.author}, ${this.pages} pages, has not been read yet`;
} else {
return `${this.title} by ${this.author}, ${this.pages} pages, has been read`;
}
};
}

// adding new book to the Library array
function addBookToLibrary(title, author, pages, status, array) {
const newBook = new Book(title, author, pages, status);
array.push(newBook);
}

// reload localStorage
function reloadLocalStorage() {
if (localStorage.length === 0 || localStorage.myLibrary === "[]") {
addBookToLibrary("Harry Potter", "J. K. Rowling", 395, true, myLibrary);
} else {
let arrayDestringified = JSON.parse(localStorage.getItem("myLibrary"));
arrayDestringified.forEach((element) => {
addBookToLibrary(element.title, element.author, element.pages, element.status, myLibrary);
});
}
}
reloadLocalStorage();

// displaying form (modal)
btnNewBook.addEventListener("click", () => {
// form.classList.remove("hide");
displayElement(form);
form.classList.add("display-block");
hideElement(btnNewBook);
});

// adding book in DOM list
const getFormInfo = (e) => {
e.preventDefault();
const targetTitle = e.target.title.value;
const targetAuthor = e.target.author.value;
const targetPages = e.target.pages.value;
const targetStatus = e.target.status.checked;

addBookToLibrary(targetTitle, targetAuthor, targetPages, targetStatus, myLibrary);
render(myLibrary, tableBody);
saveLibraryToLocalStorage("myLibrary", myLibrary);
clearForm();
hideElement(form);
displayElement(btnNewBook);
};
form.addEventListener("submit", getFormInfo);

// rendering new book from myLibrary array to the Dom
function render(array, parentDiv) {
parentDiv.innerHTML = "";
for (let i = 0; i < array.length; i++) {
let row = `<tr data-book-title="${array[i].title}">
<td>${array[i].title}</td>
<td>${array[i].author}</td>
<td>${array[i].pages}</td>
<td>
<button class="${array[i].status ? "btn-status btn-success" : "btn-status btn-warning"} btn">
${array[i].status ? "Read!" : "Unread"}
</button>
</td>
<td><button class="btn btn-danger btn-remove-book">REMOVE</button></td>
</tr>`;

parentDiv.insertAdjacentHTML("beforeend", row);
}
}
render(myLibrary, tableBody);

// hiding element from the page
function hideElement(element) {
element.classList.add("hide");
}
hideElement(form);

// displaying element to the page
function displayElement(element) {
element.classList.remove("hide");
form.classList.add("display-block");
}

// clearing form
function clearForm() {
bookTitle.value = "";
bookAuthor.value = "";
bookPages.value = "";
bookStatus.checked = "";
}

// removing book from Library and from the DOM
tableBody.addEventListener("click", (e) => {
if (!e.target.classList.contains("btn-remove-book")) {
return;
}
e.target.closest("tr").remove();

let currentBookTitle = e.target.closest("tr").dataset.bookTitle;
removeBookFromLibrary(myLibrary, currentBookTitle);
saveLibraryToLocalStorage("myLibrary", myLibrary);
});

function removeBookFromLibrary(libraryArray, bookTitle) {
if (libraryArray.length === 0) {
return;
}

for (let book of libraryArray) {
if (book.title === bookTitle) {
let bookToRemoveIndex = libraryArray.indexOf(book);
libraryArray.splice(bookToRemoveIndex, 1);
}
}
}

// Changing the book status in the DOM and inside myLibrary
tableBody.addEventListener("click", (e) => {
if (!e.target.classList.contains("btn-status")) {
return;
}
let currentBtn = e.target;
let currentBookTitle = e.target.closest("tr").dataset.bookTitle;
toggleBookStatus(myLibrary, currentBookTitle, currentBtn);
saveLibraryToLocalStorage("myLibrary", myLibrary);
});

function toggleBookStatus(libraryArray, bookTitle, currentElement) {
for (let book of libraryArray) {
if (book.title === bookTitle) {
book.toggleStatus();
currentElement.innerText = book.status ? "Read" : "Unread";
currentElement.classList.remove(`${book.status ? "btn-warning" : "btn-success"}`);
currentElement.classList.add(`${book.status ? "btn-success" : "btn-warning"}`);
}
}
}

// saving library to localStorage
function saveLibraryToLocalStorage(arrayName, array) {
localStorage.setItem(arrayName, JSON.stringify(array));
}
Binary file added Project Library/library-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions Project Library/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.hide {
display: none!important;
/* visibility: hidden!important; */
}