-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
146 lines (108 loc) · 4.21 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
//TODOs
//Fix form submission when "cancel is clicked"
//DOM elements
//dialog
const dialog = document.querySelector("dialog");
//buttons
const addBookButton = document.querySelector('.add-button');
const submitFormButton = document.querySelector('.submit-form');
const closeModalButton = document.querySelector('.close-modal');
const bookDetailsForm = document.querySelector('.book-details-form');
//Initial Library for testing
const myLibrary = [
{title: 'Jean Bean', author: 'clarpo', pages: 100, isRead: false},
{title: 'Jean Bean', author: 'clarpo', pages: 100, isRead: false},
{title: 'Jean Bea', author: 'clarpo', pages: 100, isRead: false},
{title: 'Jean Bean', author: 'clarpo', pages: 100, isRead: true},
{title: 'Jean Bean', author: 'clarpo', pages: 100, isRead: false},
{title: 'Jean Bean', author: 'clarpo', pages: 100, isRead: false},
{title: 'Jean Bean', author: 'clarpo', pages: 100, isRead: false}
];
//Book Constructor
function Book(title, author, pageCount) {
this.title = title;
this.author = author;
this.pageCount = pageCount;
this.isRead = false
}
Book.prototype.toggleRead = function() {
this.isRead = !this.isRead;
}
//Add listeners to elements
addBookButton.addEventListener('click', () => {
dialog.showModal();
});
closeModalButton.addEventListener('click', () => {
dialog.close();
});
submitFormButton.addEventListener('click', handleAddBookSubmission);
//Functions
//Create a new book and add it to the library
function addBookToLibrary(title, author, pageCount) {
const newBook = new Book(title, author, pageCount);
myLibrary.push(newBook);
displayAllBooks();
return myLibrary;
}
//Event handlers
function handleAddBookSubmission (e) {
e.preventDefault();
const formData = new FormData(e.target.form);
const formProps = Object.fromEntries(formData);
addBookToLibrary(formProps.title, formProps.author, formProps.pageCount);
dialog.close();
bookDetailsForm.reset();
}
function removeBookFromLibrary(e) {
const indexToBeRemoved = e.target.dataset.index;
myLibrary.splice(indexToBeRemoved, 1);
displayAllBooks();
}
function handleReadStatusUpdate (e) {
const indexToBeUpdated = e.target.dataset.index;
myLibrary[indexToBeUpdated].isRead = !myLibrary[indexToBeUpdated].isRead;
displayAllBooks();
}
//Create a book element for the UI from book obj
function createBookCard(book) {
const bookCard = document.createElement('div');
bookCard.classList.add('book-card');
const titleAndAuthor = document.createElement('h2');
titleAndAuthor.textContent = `${book.title} by ${book.author}`;
const updateReadStatusButton = document.createElement('button');
updateReadStatusButton.classList.add('update-read-status-button');
const isRead = document.createElement('p');
if (book.isRead) {
isRead.textContent = 'Read it';
updateReadStatusButton.textContent = 'Mark Unread';
}
else {
isRead.textContent = 'Not read yet!';
updateReadStatusButton.textContent = 'Mark Read';
}
const removeBookButton = document.createElement('button');
removeBookButton.classList.add('remove-book-button');
removeBookButton.textContent = 'Remove book';
bookCard.appendChild(titleAndAuthor);
bookCard.appendChild(isRead);
bookCard.appendChild(updateReadStatusButton);
bookCard.appendChild(removeBookButton);
return bookCard;
}
//Render any books in the library as cards
function displayAllBooks() {
const libraryContainer = document.querySelector('.library-container');
libraryContainer.replaceChildren();
myLibrary.forEach((book) => {
const newCard = createBookCard(book);
currentIndex = myLibrary.indexOf(book);
removeButton = newCard.querySelector('.remove-book-button');
removeButton.setAttribute('data-index', currentIndex);
removeButton.addEventListener('click', removeBookFromLibrary);
updateReadStatusButton = newCard.querySelector('.update-read-status-button');
updateReadStatusButton.setAttribute('data-index', currentIndex);
updateReadStatusButton.addEventListener('click', handleReadStatusUpdate);
libraryContainer.appendChild(newCard);
})
}
displayAllBooks();