Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
74 lines (60 loc) · 1.69 KB
/
Copy pathscript.js
File metadata and controls
74 lines (60 loc) · 1.69 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
import { createToast } from './js/toasts.js';
class Book {
constructor(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
}
changeReadStatus() {
this.read = this.read ? false : true;
}
}
class Library {
constructor() {
this.library = [];
}
addBook(book) {
this.library.push(book);
}
removeBook(bookIndex) {
this.library.splice(bookIndex, 1);
}
displayBooks() {
const main = document.querySelector('main');
const toasts = [];
for (let [index, book] of this.library.entries()) {
const toast = createToast(book, index);
toasts.push(toast);
}
main.replaceChildren(...toasts);
}
}
function getBookInfo() {
const bookTitle = document.querySelector('#title').value;
const bookAuthor = document.querySelector('#author').value;
const bookPages = document.querySelector('#pages').value;
const bookRead = document.querySelector('#read').checked;
return [bookTitle, bookAuthor, bookPages, bookRead];
}
const openFormButton = document.querySelector('.action');
const dialog = document.querySelector('#bookDialog');
const form = document.querySelector('#bookForm');
const cancelButton = document.querySelector('#cancel');
const submitButton = document.querySelector('#submit');
const library = new Library();
openFormButton.addEventListener('click', () => {
dialog.showModal();
});
cancelButton.addEventListener('click', () => {
dialog.close();
});
submitButton.addEventListener('click', () => {
if (form.reportValidity()) {
const book = new Book(...getBookInfo());
library.addBook(book);
dialog.close();
form.reset();
library.displayBooks();
}
});