-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
227 lines (194 loc) · 6.2 KB
/
app.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"use strict";
const bookDisplay = document.querySelector(".display-cards");
const cardModal = document.querySelector(".modal");
document.addEventListener("DOMContentLoaded", () => {
addBookToLibrary();
displayBooks();
handleCardClicks();
});
const myLibrary = [
{
bookId: 1,
title: "The Witcher: Blood of Elves",
author: "Andrzej Sapkowski",
pages: 398,
status: "Read",
},
{
bookId: 2,
title: "The Witcher: The Time of Contempt",
author: "Andrzej Sapkowski",
pages: 331,
status: "Not Read",
},
{
bookId: 3,
title: "The Witcher: Baptism of Fire",
author: "Andrzej Sapkowski",
pages: 349,
status: "Read",
},
];
function Book(bookId, title, author, pages, status) {
this.bookId = bookId;
this.title = title;
this.author = author;
this.pages = pages;
this.status = status;
}
function generateId() {
return myLibrary.length === 0
? 1
: myLibrary[myLibrary.length - 1].bookId + 1;
}
function addBookToLibrary() {
const bookForm = document.querySelector("form");
bookForm.addEventListener("submit", (e) => {
e.preventDefault();
const formData = Object.fromEntries(new FormData(e.target).entries());
const { title, author, pages, status } = formData;
const newBook = new Book(generateId(), title, author, pages, status);
myLibrary.push(newBook);
createBookCard(newBook);
bookForm.reset();
});
}
function createBookCard(book) {
const bookCard = document.createElement("div");
bookCard.setAttribute("data-id", `${book.bookId}`);
bookCard.classList.add("card");
bookCard.innerHTML = `<div class="card-title"><p>${book.title}</p></div>
<div class="card-author"><p>Author: ${book.author}</p></div>
<div class="card-pages"><p>Pages: ${book.pages}</p></div>
<div class="card-status">
<div>
<p>Status: <span>${book.status}</span></p>
</div>
<div>
<button type="button" class="status-btn">
<p class="visually-hidden">Change status</p>
<svg>
<use xlink:href="./icons/bx-revision.svg#revision" />
</svg>
</button>
</div>
</div>
<div class="control-btns">
<div>
<button type="button" class="delete-btn">
<p class="visually-hidden">Delete</p>
<svg width="24px" height="24px">
<use xlink:href="./icons/bx-trash.svg#trash" />
</svg>
</button>
</div>
<div>
<button type="button" class="edit-btn">
<p class="visually-hidden">Edit</p>
<svg width="24px" height="24px">
<use xlink:href="./icons/bx-edit-alt.svg#edit" />
</svg>
</button>
</div>
</div>`;
bookDisplay.appendChild(bookCard);
}
function displayBooks() {
for (const book of myLibrary) {
createBookCard(book);
}
}
function deleteBook(e) {
const card = e.target.closest("button").parentNode.parentNode.parentNode;
const dataId = card.getAttribute("data-id");
card.remove();
deleteBookFromArray(dataId);
}
function deleteBookFromArray(bookId) {
const bookIndex = myLibrary.findIndex((book) => book.bookId == bookId);
myLibrary.splice(bookIndex, 1);
}
function changeCardStatus(e) {
const card = e.target.closest("button").parentNode.parentNode.parentNode;
const cardStatus = card.querySelector(".card-status span");
const dataId = card.getAttribute("data-id");
if (cardStatus.textContent === "Read") {
cardStatus.textContent = "Not Read";
} else {
cardStatus.textContent = "Read";
}
changeBookStatus(dataId, cardStatus.textContent);
}
function changeBookStatus(bookId, newStatus) {
const bookIndex = myLibrary.findIndex((book) => book.bookId == bookId);
myLibrary[bookIndex].status = newStatus;
}
function handleBookEdit(e) {
const card = e.target.closest("button").parentNode.parentNode.parentNode;
const dataId = card.getAttribute("data-id");
const bookIndex = myLibrary.findIndex((book) => book.bookId == dataId);
const book = myLibrary[bookIndex];
cardModal.showModal();
showModalInputs(book);
exitModal();
editBookInLibrary(book);
}
function showModalInputs(book) {
const modalTitle = document.querySelector("#modal-book-title");
const modalAuthor = document.querySelector("#modal-book-author");
const modalPages = document.querySelector("#modal-book-pages");
const modalStatus = document.querySelector(
`#modal-book-status option[value="${book.status}"]`
);
modalTitle.setAttribute("value", book.title);
modalAuthor.setAttribute("value", book.author);
modalPages.setAttribute("value", book.pages);
modalStatus.setAttribute("selected", "");
}
function editBookInLibrary(book) {
const editForm = document.querySelector(".modal form");
editForm.addEventListener("submit", (e) => {
e.preventDefault();
const formData = Object.fromEntries(new FormData(e.target).entries());
const { title, author, pages, status } = formData;
book.title = title;
book.author = author;
book.pages = pages;
book.status = status;
renderEditedValues(book);
cardModal.close();
editForm.reset();
});
}
function renderEditedValues(book) {
const bookCard = document.querySelector(`.card[data-id="${book.bookId}"]`);
const bookCardTitle = bookCard.querySelector(".card-title p");
const bookCardAuthor = bookCard.querySelector(".card-author p");
const bookCardPages = bookCard.querySelector(".card-pages p");
const bookCardStatus = bookCard.querySelector(".card-status p span");
bookCardTitle.textContent = book.title;
bookCardAuthor.textContent = book.author;
bookCardPages.textContent = book.pages;
bookCardStatus.textContent = book.status;
}
function exitModal() {
const modalExitBtn = document.querySelector(".modal-exit");
modalExitBtn.addEventListener("click", (e) => {
if (e.target.closest("button").classList.contains("modal-exit"))
cardModal.close();
});
}
function handleCardClicks() {
bookDisplay.addEventListener("click", (e) => {
if (e.target.closest("button").classList.contains("delete-btn")) {
//do something
deleteBook(e);
} else if (e.target.closest("button").classList.contains("status-btn")) {
changeCardStatus(e);
} else if (e.target.closest("button").classList.contains("edit-btn")) {
handleBookEdit(e);
} else {
//future feature
}
});
}