-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
124 lines (106 loc) · 3.73 KB
/
Copy pathindex.js
File metadata and controls
124 lines (106 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const myLibrary = [];
class Book {
constructor(title, author, numPages, isRead) {
this.title = title;
this.author = author;
this.numPages = numPages;
this.isRead = isRead;
this.info = function () {
const infoString = `${this.title} by ${this.author}, ${this.numPages} pages, ${isRead ? `has been read` : `not read yet`}`;
console.log(infoString);
return infoString;
};
}
}
// Form
const bookForm = document.getElementById('add-book-form');
const titleInput = document.getElementById('add-book-title');
const authorInput = document.getElementById('add-book-author');
const pagesInput = document.getElementById('add-book-pages');
const readInput = document.querySelector('.add-book-read');
const submitBtn = document.getElementById('submit-book');
// Books grid & add book btn
const addBookBtn = document.getElementById('add-book');
const booksGrid = document.querySelector('.books__section');
// Modal
const modalOverlay = document.querySelector('.modal__overlay');
const modal = document.querySelector('.add-book__modal');
// Event listeners
bookForm.addEventListener('submit', submitBook);
booksGrid.addEventListener('click', handleBookActions);
modalOverlay.addEventListener('click', hideModal);
addBookBtn.addEventListener('click', showModal);
// Functions
function submitBook(event) {
event.preventDefault();
const bookTitle = titleInput.value;
const bookAuthor = authorInput.value;
const bookPages = pagesInput.value;
const bookRead = readInput.checked;
addBookToLibrary(bookTitle, bookAuthor, bookPages, bookRead);
hideModal();
resetGrid();
displayBooks(myLibrary);
}
function addBookToLibrary(title, author, pages, isRead) {
const newBook = new Book(title, author, pages, isRead);
myLibrary.push(newBook);
}
function createBookElement(book, index) {
const bookMarkup = `
<div class="book__data">
<h3 class="book__title">"${book.title}"</h3>
<p class="book__author">by ${book.author}</p>
<p class="book__pages"><strong>${book.numPages}</strong> pages</p>
</div>
<div class="book__actions">
<button class="action-btn read-btn" data-book-index=${index}>${book.isRead ? 'Completed' : 'Not read'}</button>
<button class="action-btn remove-btn" data-book-index="${index}">Remove</button>
</div>
`;
const bookElement = document.createElement('article');
bookElement.classList.add('book');
bookElement.innerHTML = bookMarkup;
return bookElement
}
function displayBooks(library) {
library.forEach((book, index) => {
const bookElement = createBookElement(book, index);
booksGrid.appendChild(bookElement);
});
}
function handleBookActions(event) {
if (event.target.classList.contains('remove-btn')) {
const bookIndex = event.target.dataset.bookIndex;
removeBookFromLibrary(bookIndex);
}
if (event.target.classList.contains('read-btn')) {
const button = event.target;
const bookIndex = event.target.dataset.bookIndex;
myLibrary[bookIndex].isRead = !myLibrary[bookIndex].isRead;
button.innerText = myLibrary[bookIndex].isRead ? 'Completed' : 'Not read';
}
}
function removeBookFromLibrary(bookIndex) {
myLibrary.splice(bookIndex, 1);
resetGrid();
displayBooks(myLibrary);
}
function resetGrid() {
booksGrid.innerHTML = '';
}
function hideModal() {
modal.style.display = 'none';
modalOverlay.style.display = 'none';
}
function showModal() {
cleanInputs();
modal.style.display = 'block';
modalOverlay.style.display = 'block';
}
function cleanInputs() {
titleInput.value = '';
authorInput.value = '';
pagesInput.value = '';
readInput.value = '';
}