-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
173 lines (154 loc) · 4.35 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
class Book {
constructor(title, author, numPages, read) {
this.title = title;
this.author = author;
this.numPages = numPages;
this.read = read;
}
}
class Library {
constructor() {
this.books = [];
}
addBookToLibrary = (book) => {
this.books.push(book);
};
getBook(bookName) {
return this.books.find((book) => book.title == bookName);
}
removeFromLibrary(title) {
this.books = this.books.filter((book) => book.title !== title);
}
}
const library = new Library();
// this function creates the card component
function createBookCard(book) {
const card = createHtmlElement("div", "card", book.title);
const cardBookNameSection = createHtmlElement(
"p",
"card-book-name",
"",
`Book Name: ${book.title}`
);
const cardAuthorNameSection = createHtmlElement(
"p",
"card-author-name",
"",
`Author: ${book.author}`
);
const cardNumOfPages = createHtmlElement(
"p",
"card-num-of-pages",
"",
`Pages: ${book.numPages}`
);
const readButton = createHtmlElement("button", "read-button", "", "");
readButtonState(book, readButton);
const removeButton = createHtmlElement(
"button",
"remove-button",
"",
"Remove"
);
removeButtonFunctionality(card, removeButton);
card.append(
cardBookNameSection,
cardAuthorNameSection,
cardNumOfPages,
readButton,
removeButton
);
booksContainer.append(card);
}
function createHtmlElement(element, elementClass, data, text) {
const el = document.createElement(element);
if (elementClass) el.className = elementClass;
if (text) el.innerText = text;
if (data) el.dataset.title = data;
return el;
}
function readButtonState(book, button) {
if (book.read) {
button.innerText = "Read";
button.style.backgroundColor = "rgb(49, 206, 49)";
} else {
button.innerText = "Didn't Read";
button.style.backgroundColor = "rgb(218, 79, 79)";
}
button.addEventListener("click", () => {
console.log("clicked");
if (button.innerText == "Read") {
book.read = false;
button.innerText = "Didn't Read";
button.style.backgroundColor = "rgb(218, 79, 79)";
} else {
button.innerText = "Read";
book.read = true;
button.style.backgroundColor = "rgb(49, 206, 49)";
}
});
}
function removeButtonFunctionality(card, button) {
button.addEventListener("click", () => {
card.remove();
library.removeFromLibrary(card.dataset.title);
});
}
const popUp = document.querySelector(".form-popup");
const modalLayover = document.querySelector(".modal-background");
const booksContainer = document.querySelector(".books-container");
const form = document.getElementById("book-form");
const bookNameInputField = document.querySelector("#book-name");
const authorNameInputField = document.querySelector("#author-name");
const numOfPagesInputField = document.querySelector("#number-of-pages");
hideForm(popUp);
hideForm(modalLayover);
function setErrorMessagesOnInputs(formInput, errorMessage) {
formInput.addEventListener("input", () => {
formInput.setCustomValidity("");
formInput.checkValidity();
});
formInput.addEventListener("invalid", () => {
if (formInput.value === "") {
formInput.setCustomValidity(errorMessage);
}
});
}
setErrorMessagesOnInputs(bookNameInputField, "Please enter book name");
setErrorMessagesOnInputs(
authorNameInputField,
"Please enter the author's name"
);
setErrorMessagesOnInputs(
numOfPagesInputField,
"Please enter the number of pages"
);
form.addEventListener("submit", (event) => {
event.preventDefault();
const bookName = document.getElementById("book-name").value;
const bookAuthor = document.getElementById("author-name").value;
const bookNumPages = document.getElementById("number-of-pages").value;
let read = document.getElementById("read-validation");
if (read.checked) {
read = true;
} else {
read = false;
}
const newBook = new Book(bookName, bookAuthor, bookNumPages, read);
createBookCard(newBook);
library.addBookToLibrary(newBook);
hideForm(popUp);
hideForm(modalLayover);
});
function showForm(element) {
element.style.display = "block";
}
function hideForm(element) {
element.style.display = "none";
}
const addBookButton = document.querySelector(".new-book");
addBookButton.addEventListener("click", () => {
form.reset();
showForm(popUp);
showForm(modalLayover);
});