mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2025-06-28 04:45:36 -04:00
feat: logic completed.
This commit is contained in:
parent
104849154f
commit
72997086cc
3 changed files with 84 additions and 24 deletions
|
@ -31,15 +31,43 @@
|
||||||
|
|
||||||
.book-cards {
|
.book-cards {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
|
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
||||||
}
|
}
|
||||||
|
|
||||||
.card {
|
.card {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
gap: 20px;
|
gap: 5px;
|
||||||
min-width: 200px;
|
width: 300px;
|
||||||
min-height: 200px;
|
min-height: 250px;
|
||||||
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.card p {
|
||||||
|
padding: 0;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
min-height: 50vh;
|
||||||
|
min-width: 20vw;
|
||||||
|
border: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-header {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-around;
|
||||||
|
}
|
||||||
|
|
||||||
|
.close-btn {
|
||||||
|
height: 30px;
|
||||||
|
align-self: center;
|
||||||
|
}
|
||||||
|
|
|
@ -26,18 +26,21 @@
|
||||||
</footer>
|
</footer>
|
||||||
</div>
|
</div>
|
||||||
<dialog class="modal">
|
<dialog class="modal">
|
||||||
<main>
|
<div class="form-header">
|
||||||
<div class="form">
|
<h1 class="form-header">Add Book</h1>
|
||||||
<form action="" >
|
|
||||||
<input type="text" name="title" id="title">
|
|
||||||
<input type="text" name="author" id="author">
|
|
||||||
<button type="submit" id="submit-btn" class="btn">Submit</button>
|
|
||||||
</form>
|
|
||||||
<button class="btn close-btn">
|
<button class="btn close-btn">
|
||||||
<span class="material-symbols-outlined">close</span>
|
<span class="material-symbols-outlined">close</span>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
<form action="" class="form">
|
||||||
|
<input type="text" name="title" id="title">
|
||||||
|
<input type="text" name="author" id="author">
|
||||||
|
<div class="form-item">
|
||||||
|
<label for="checkbox">Have you read the book?</label>
|
||||||
|
<input type="checkbox" id="read-button">
|
||||||
|
</div>
|
||||||
|
<button type="submit" id="submit-btn" class="btn">Submit</button>
|
||||||
|
</form>
|
||||||
</dialog>
|
</dialog>
|
||||||
<script src="js/script.js"></script>
|
<script src="js/script.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -23,8 +23,12 @@ function Book(title, author, read) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Book.prototype.toggleBookReadStatus = function(){
|
||||||
|
this.read = !this.read;
|
||||||
|
}
|
||||||
|
|
||||||
function addBookToLibrary(book) {
|
function addBookToLibrary(book) {
|
||||||
let newBook = new Book(book.title, book.author, book.pages, book.read);
|
let newBook = new Book(book.title, book.author, book.read);
|
||||||
myLibrary.push(newBook);
|
myLibrary.push(newBook);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,25 +39,57 @@ function displayBooks(books) {
|
||||||
books.forEach((element, i) => {
|
books.forEach((element, i) => {
|
||||||
const card = document.createElement('div');
|
const card = document.createElement('div');
|
||||||
card.classList.add('card');
|
card.classList.add('card');
|
||||||
card.classList.add(`data-card${i}`)
|
card.setAttribute("data-card", i);
|
||||||
|
|
||||||
const title = document.createElement('p');
|
const title = document.createElement('p');
|
||||||
|
title.classList.add('card-title');
|
||||||
const author = document.createElement('p');
|
const author = document.createElement('p');
|
||||||
|
author.classList.add('card-author');
|
||||||
|
const readBtn = document.createElement('button');
|
||||||
|
readBtn.classList.add('btn','read-btn');
|
||||||
|
readBtn.textContent = element.read ? "read" : "not read";
|
||||||
|
const deleteBtn = document.createElement('button');
|
||||||
|
deleteBtn.classList.add('btn', 'delete-btn')
|
||||||
|
deleteBtn.textContent = 'delete book';
|
||||||
|
|
||||||
title.textContent = element.title;
|
|
||||||
|
title.textContent = `"${element.title}"`;
|
||||||
author.textContent = element.author;
|
author.textContent = element.author;
|
||||||
|
|
||||||
card.appendChild(title);
|
card.appendChild(title);
|
||||||
card.appendChild(author)
|
card.appendChild(author);
|
||||||
|
card.appendChild(readBtn);
|
||||||
|
card.appendChild(deleteBtn);
|
||||||
booksDiv.appendChild(card);
|
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 => {
|
submitButton.addEventListener('click', event => {
|
||||||
const author = document.querySelector('#author').value;
|
const author = document.querySelector('#author').value;
|
||||||
const title = document.querySelector('#title').value;
|
const title = document.querySelector('#title').value;
|
||||||
const newBook = new Book(title, author, false);
|
const readBtn = document.querySelector('#read-button').value;
|
||||||
|
const bookIsRead = readBtn === 'on' ? true : false;
|
||||||
|
|
||||||
|
// create book object
|
||||||
|
const newBook = new Book(title, author, bookIsRead);
|
||||||
|
|
||||||
addBookToLibrary(newBook);
|
addBookToLibrary(newBook);
|
||||||
displayBooks(myLibrary);
|
displayBooks(myLibrary);
|
||||||
|
@ -68,13 +104,6 @@ closeDialogButton.addEventListener('click', () => {
|
||||||
dialog.close();
|
dialog.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
// document.addEventListener('click', e => {
|
|
||||||
// if (!e.target.closest("dialog")) {
|
|
||||||
// dialog.showModal();
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
|
|
||||||
|
|
||||||
// default data
|
// default data
|
||||||
myLibrary.push(defaultData)
|
myLibrary.push(defaultData)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue