-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
212 lines (196 loc) · 5.98 KB
/
script.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const booksContainer = document.querySelector("#books-container");
const newButton = document.querySelector(".add-button");
const modal = document.querySelector("#modal");
const darken = document.querySelector(".darken");
const closeModal = document.querySelector(".modal-header > button");
const coverImages = document.querySelectorAll(".book-cover");
const filters = document.querySelector("#filter-select");
const titleInput = document.querySelector(".title-input");
const authorInput = document.querySelector(".author-input");
const pagesInput = document.querySelector(".pages-input");
const urlInput = document.querySelector(".url-input");
const readInput = document.querySelector(".read-input");
const addBookButton = document.querySelector(".add-book");
const library = [
{
title: "Secrets of the JavaScript Ninja",
author: "John Resig",
pages: 392,
read: false,
url: "https://m.media-amazon.com/images/I/711+-FJC3sL._AC_UF1000,1000_QL80_.jpg",
},
{
title: "Don't Make Me Think, Revisited",
author: "Masashi Kishimoto",
pages: 216,
read: true,
url: "https://m.media-amazon.com/images/I/617rPny7-LL._AC_UF1000,1000_QL80_.jpg",
},
{
title: "Clean Code",
author: "Robert C. Martin",
pages: 464,
read: false,
url: "https://m.media-amazon.com/images/I/51E2055ZGUL._AC_UF1000,1000_QL80_.jpg",
},
{
title: "Naruto Gold Vol. 27",
author: "Masashi Kishimoto",
pages: 192,
read: true,
url: "https://m.media-amazon.com/images/I/91qf9wFcEsL._SL1500_.jpg",
},
{
title: "O guia do mochileiro das galáxias",
author: "Douglas Adams",
pages: 208,
read: true,
url: "https://m.media-amazon.com/images/I/81eMrK+qVCL._SL1500_.jpg",
},
{
title: "Grokking Algorithms",
author: "Aditya Bhargava",
pages: 256,
read: true,
url: "https://m.media-amazon.com/images/I/81HwgKQ39lS._SL1500_.jpg",
},
{
title: "The Creative Act: A Way of Being",
author: "Rick Rubin",
pages: 432,
read: true,
url: "https://m.media-amazon.com/images/I/918EkrTDaRL._SL1500_.jpg",
},
{
title: "O Estranho Misterioso",
author: "Mark Twain",
pages: 214,
read: true,
url: "https://m.media-amazon.com/images/I/71ixBb6jqLL._SL1000_.jpg",
},
];
function Book(title, author, pages, read, url) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
this.url = url;
this.info = function () {
return `${this.title} by ${this.author}, ${this.pages} pages, ${
this.read ? "Already read" : "Not read yet"
}`;
};
}
function addBookToLibrary(title, author, pages, read, url) {
library.push(new Book(title, author, pages, read, url));
}
addBookButton.addEventListener("click", (event) => {
event.preventDefault();
const title = titleInput.value;
const author = authorInput.value;
const pages = pagesInput.value;
const read = readInput.checked;
const url = urlInput.value;
addBookToLibrary(title, author, pages, read, url);
closeModalFunction();
displayBooks(library);
titleInput.value = "";
authorInput.value = "";
pagesInput.value = "";
readInput.checked = false;
urlInput.value = "";
});
function filterByRead(array) {
return array.filter((book) => book.read);
}
function filterByNotRead(array) {
return array.filter((book) => book.read === false);
}
function clearContainer() {
booksContainer.innerHTML = "";
}
function displayBooks(array) {
clearContainer();
array.forEach((book, index) => {
booksContainer.innerHTML += `
<div class="book" data-attribute="${index}">
<img class="book-cover" src="${book.url}" />
<div class="status-container">
<button class="status" data-attribute="${index}">${
book.read ? "Already read" : "Not read yet"
}</button>
<button class="delete-button" data-attribute="${index}"><img class="trash-icon" src="assets/trash.svg">Delete</button>
</div>
<div class="book-info">
<div class="title-pages">
<h1 class="book-title">${book.title}</h1>
<div class="pages-container">
<img src="assets/pages.svg">
<div class="pages-info">
<p class="book-pages">${book.pages}</p>
<p class="book-pages-text">pages</p>
</div>
<div>
</div>
</div>
</div>
<h2 class="book-author">${book.author}</h2>
`;
});
const deleteBookButtons = document.querySelectorAll(".delete-button");
deleteBookButtons.forEach((button) => {
button.addEventListener("click", (event) => {
const targetElement = event.target.closest(".delete-button");
const index = targetElement.dataset.attribute;
console.log(index);
library.splice(index, 1);
displayBooks(library);
});
});
const readStatus = document.querySelectorAll(".status");
readStatus.forEach((button) => {
button.addEventListener("click", (event) => {
const targetElement = event.target.closest(".status");
const index = targetElement.dataset.attribute;
if (library[index].read) {
library[index].read = false;
} else {
library[index].read = true;
}
displayBooks(library);
});
});
}
filters.addEventListener("change", () => {
const selectedValue = filters.value;
if (selectedValue === "read") {
displayBooks(filterByRead(library));
} else if (selectedValue === "not-read") {
displayBooks(filterByNotRead(library));
} else {
displayBooks(library);
}
});
newButton.addEventListener("click", () => {
modal.style.display = "flex";
setTimeout(() => {
modal.style.opacity = "1";
}, 100);
darken.style.display = "flex";
setTimeout(() => {
darken.style.opacity = "1";
}, 100);
console.log("oi");
});
closeModal.addEventListener("click", closeModalFunction);
function closeModalFunction() {
modal.style.opacity = "0";
setTimeout(() => {
modal.style.display = "none";
}, 200);
darken.style.opacity = "0";
setTimeout(() => {
darken.style.display = "none";
}, 200);
}
displayBooks(library);