-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
167 lines (136 loc) · 4.68 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
// Book Class: Represents a Book
class Book {
constructor(title, author, isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
}
}
// UI Class: Handle UI Tasks
class UI {
static displayBooks() {
// Get currently stored books
const books = Store.getBooks();
// Iterates "books" & calls "addBookToList()" func
books.forEach((book) => UI.addBookToList(book));
}
static addBookToList(book) {
// Creates a row to insert into the table within index.html
// Obtain DOM element "#book-list" (stored in list var)
const list = document.querySelector('#book-list');
// Create a table-row HTML element (stored in row var)
const row = document.createElement('tr');
// Store input in new table-row element
row.innerHTML = `
<td>${book.title}</td>
<td>${book.author}</td>
<td>${book.isbn}</td>
<td><a href="#" class="btn btn-danger btn-sm delete">X</a></td>
`;
// Add new row to the table
list.appendChild(row);
}
// Deletes a targeted element(book)
static deleteBook(el) {
// Check if element has the class "delete"
if(el.classList.contains('delete')) {
// Remove "parent el" of the "parent el" (2 levels up to get entire row)
el.parentElement.parentElement.remove();
}
}
// Alert
static showAlert(message, className) {
// Creates a DOM "div" element
const div = document.createElement('div');
// Assigns "className" param as the new "div"s class'
div.className = `alert alert-${className}`;
// Assigns "message" param as the new "div"s text
div.appendChild(document.createTextNode(message));
// Find desired parent element & place the new alert within it:
// The parent element: container
const container = document.querySelector('.container');
// The form element: book-form
const form = document.querySelector('#book-form');
// Take the "container" and insert our "div" before the "form"
container.insertBefore(div, form);
// Vanish in 3 seconds
setTimeout(() => document.querySelector('.alert').remove(), 3000);
}
// Clears fields after submission
static clearFields() {
document.querySelector('#title').value = '';
document.querySelector('#author').value = '';
document.querySelector('#isbn').value = '';
}
}
// Store Class: Handles Storage
class Store {
static getBooks() {
let books;
// Check if there's a current "book" item
if(localStorage.getItem('books') === null) {
books = [];
} else {
// parse as json to be used as reg JS array of objs
books = JSON.parse(localStorage.getItem('books'));
}
return books;
}
static addBook(book) {
// Get currently stored books
const books = Store.getBooks();
// Add new book to "books" array
books.push(book);
// Set (or save) local storage with books array (converted to a string)
localStorage.setItem('books', JSON.stringify(books));
}
static removeBook(isbn) {
// Get currently stored books
const books = Store.getBooks();
// Iterate through "books" array
books.forEach((book, index) => {
// Check if current iterated item's "isbn" matches passed "isbn" param
if(book.isbn === isbn) {
// Splice out current iterated item from array by its index
books.splice(index, 1);
}
});
localStorage.setItem('books', JSON.stringify(books));
}
}
// Event: Display Books
document.addEventListener('DOMContentLoaded', UI.displayBooks);
// Event: Add a Book
document.querySelector('#book-form').addEventListener('submit', (e) => {
// Prevent default submit
e.preventDefault();
// Get Form Values
const title = document.querySelector('#title').value;
const author = document.querySelector('#author').value;
const isbn = document.querySelector('#isbn').value;
// Validate
if(title === '' || author === '' || isbn === '') {
UI.showAlert('Please fill in all fields', 'danger');
} else {
// Instantiate Book
const book = new Book(title, author, isbn);
// Add Book to UI
UI.addBookToList(book);
// Add Book to store
Store.addBook(book);
// Show success message
UI.showAlert('Book Added', 'success');
// Clear Fields
UI.clearFields();
}
});
// Event: Remove a Book
document.querySelector('#book-list').addEventListener('click', (e) => {
// "deleteBook()" targets specific element (else it would only target first instance)
// Remove book from UI
UI.deleteBook(e.target);
// Remove book from store: target ISBN instead of entire row (what e.target points to)
Store.removeBook(e.target.parentElement.previousElementSibling.textContent);
// Show success message
UI.showAlert('Book Removed', 'success');
});