From 7a40d907d9a6a1d8ba128646533906921529b190 Mon Sep 17 00:00:00 2001
From: Smig <89040888+smiggiddy@users.noreply.github.com>
Date: Wed, 22 Nov 2023 11:32:12 -0500
Subject: [PATCH] Library (#6)
* feat: initial commit
* feat: initial javascript complete
* feat: modal added
* feat: logic completed.
* feat: modal base styling complete
* feat: more styling
* feat: styling complete
---
library/css/style.css | 161 ++++++++++++++++++++++++++++++++++++++++++
library/index.html | 53 ++++++++++++++
library/js/script.js | 122 ++++++++++++++++++++++++++++++++
3 files changed, 336 insertions(+)
create mode 100644 library/css/style.css
create mode 100644 library/index.html
create mode 100644 library/js/script.js
diff --git a/library/css/style.css b/library/css/style.css
new file mode 100644
index 0000000..4a80feb
--- /dev/null
+++ b/library/css/style.css
@@ -0,0 +1,161 @@
+:root {
+ --background-color: #DBDADD;
+ --card-color: #EFEEF0;
+ --btn-red-color: #e15252;
+ --btn-green-color: #7ed7a9;
+ --spacing-sm: 16px;
+ --spacing-md: 20px;
+}
+
+body, html {
+ font-size: 16px;
+ margin: 0;
+ padding: 0;
+ font-family: 'Roboto', sans-serif;
+ min-height: 100vh;
+ background-color: var(--background-color);
+}
+
+button {
+ font-size: 1.3rem;
+ font-family: inherit;
+ cursor: pointer;
+ border: none;
+ border-radius: 5px;
+}
+
+dialog {
+ box-shadow: 0 0 #0000, 0 0 #0000, 0 25px 50px -12px rgba(0, 0, 0, 0.25);
+}
+
+.container {
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ align-items: center;
+}
+
+.navbar {
+ margin-top: 20px;
+ margin-bottom: 100px;
+ display: flex;
+ flex-direction: column;
+ justify-content: space-between;
+ align-content: center;
+ align-items: center;
+}
+
+.navbar .header {
+ font-size: 2.6rem;
+ font-weight: 900;
+}
+
+.content {
+ min-width: 1200px;
+ margin-left: 30px;
+}
+
+.book-cards {
+ display: grid;
+ grid-template-columns: repeat(auto-fit, minmax(300px, 370px));
+ gap: 10px;
+}
+
+.card {
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ gap: 20px;
+ width: 300px;
+ min-height: 250px;
+ text-align: center;
+ box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
+ transition: 0.3s;
+ padding: 20px;
+ margin: 20px;
+ background-color: var(--card-color);
+}
+
+.card:hover {
+ box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
+}
+
+.card p {
+ font-size: 1.5rem;
+ padding: 0;
+ margin: 0;
+}
+
+.card>.card-title {
+ font-weight: 700;
+}
+
+.card>.card-author {
+ font-weight: 300;
+}
+
+.modal {
+ /* display: flex; */
+ flex-direction: column;
+ min-height: 43vh;
+ min-width: 20vw;
+ border: none;
+ padding: 20px 40px;
+}
+
+.form {
+ display: flex;
+ flex-direction: column;
+ gap: 10px;
+}
+
+.form input {
+ min-height: 2.2rem;
+ font-size: 1.2rem;
+}
+
+.form-header {
+ display: flex;
+ justify-content: space-between;
+}
+
+.form-header h1 {
+ font-size: 1.7rem;
+ align-self: center;
+}
+
+.form-item {
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ gap: 20px;
+}
+
+.form-item input {
+ transform: scale(1.5);
+}
+
+.form-item label {
+ font-weight: 500;
+ font-size: 1.3rem;
+}
+
+
+.close-btn {
+ height: 30px;
+ align-self: center;
+ padding: 4px;
+}
+
+.btn {
+ padding: var(--spacing-sm) var(--spacing-md);
+}
+
+.red-btn-color {
+ background-color: var(--btn-red-color);
+
+}
+
+.green-btn-color {
+ background-color: var(--btn-green-color);
+}
diff --git a/library/index.html b/library/index.html
new file mode 100644
index 0000000..8ec8cf8
--- /dev/null
+++ b/library/index.html
@@ -0,0 +1,53 @@
+
+
+
+
+
+
+
+
+
+
+
+ Anonymous Library
+
+
+
+
+
+
+
+
+
diff --git a/library/js/script.js b/library/js/script.js
new file mode 100644
index 0000000..a6c1dcb
--- /dev/null
+++ b/library/js/script.js
@@ -0,0 +1,122 @@
+const myLibrary = [];
+const submitButton = document.querySelector('#submit-btn');
+
+const booksDiv = document.querySelector('.book-cards');
+const dialog = document.querySelector("dialog");
+const addBookButton = document.querySelector(".show-modal");
+const closeDialogButton = document.querySelector(".close-btn");
+
+
+
+const defaultData = new Book('Atomic Habits', 'James Clear', false);
+
+
+function Book(title, author, read) {
+ this.title = title;
+ this.author = author;
+ this.read = read;
+
+ this.info = function() {
+ let haveRead = this.read ? "have read" : "have not read";
+
+ return `${this.title}, by ${this.author}, ${this.pages} pages, ${haveRead}`;
+ };
+}
+
+Book.prototype.toggleBookReadStatus = function(){
+ this.read = !this.read;
+}
+
+function addBookToLibrary(book) {
+ let newBook = new Book(book.title, book.author, book.read);
+ myLibrary.push(newBook);
+}
+
+function displayBooks(books) {
+ // Clear the screen before looping
+ booksDiv.innerHTML = "";
+
+ books.forEach((element, i) => {
+ const card = document.createElement('div');
+ card.classList.add('card');
+ card.setAttribute("data-card", i);
+
+ const title = document.createElement('p');
+ title.classList.add('card-title');
+ const author = document.createElement('p');
+ author.classList.add('card-author');
+ const readBtn = document.createElement('button');
+ const readBtnFlag = element.read ? "read" : "not read";
+ const readBtnColor = element.read ? "green-btn-color" : "red-btn-color";
+ console.log(readBtnColor, element.read, readBtnFlag);
+ readBtn.classList.add('btn','read-btn',readBtnColor);
+
+ readBtn.textContent = readBtnFlag;
+ const deleteBtn = document.createElement('button');
+ deleteBtn.classList.add('btn', 'delete-btn')
+ deleteBtn.textContent = 'delete book';
+
+
+ title.textContent = `"${element.title}"`;
+ author.textContent = element.author;
+
+ card.appendChild(title);
+ card.appendChild(author);
+ card.appendChild(readBtn);
+ card.appendChild(deleteBtn);
+ booksDiv.appendChild(card);
+ });
+}
+
+booksDiv.addEventListener('click', event => {
+ let target = event.target.className;
+ if (target.includes('read-btn')){
+ let cardIndex = event.target.parentElement.dataset.card;
+ myLibrary[cardIndex].toggleBookReadStatus();
+ displayBooks(myLibrary);
+ };
+});
+
+booksDiv.addEventListener('click', event => {
+ let target = event.target.className;
+ if (target.includes('delete-btn')){
+ let cardIndex = event.target.parentElement.dataset.card;
+ myLibrary.splice(cardIndex, 1);
+ displayBooks(myLibrary);
+ };
+});
+
+submitButton.addEventListener('click', event => {
+ const author = document.querySelector('#author').value;
+ const title = document.querySelector('#title').value;
+ const readBtn = document.querySelector('#read-button').value;
+ const bookIsRead = readBtn === 'on' ? true : false;
+
+ if (author === "" || title === "") {
+ return
+ }
+
+ // create book object
+ const newBook = new Book(title, author, bookIsRead);
+
+ addBookToLibrary(newBook);
+ displayBooks(myLibrary);
+ event.preventDefault();
+ dialog.close();
+
+});
+
+addBookButton.addEventListener('click', () => {
+ dialog.showModal();
+});
+
+closeDialogButton.addEventListener('click', () => {
+ dialog.close();
+});
+
+// default data
+myLibrary.push(defaultData)
+
+// Load books on page load
+displayBooks(myLibrary);
+