-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
105 lines (92 loc) · 2.76 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
// Define UI Element
const form = document.querySelector('#form')
const bookList = document.querySelector('#book-list')
// Event Listener
form.addEventListener('submit', addBook);
document.addEventListener('DOMContentLoaded', render);
bookList.addEventListener('click', removeBook);
// Book Class
class Book {
constructor(title, author, isbn) {
this.title = title
this.author = author
this.isbn = isbn
}
}
// UI Class
class UI {
static showAlert(message, className) {
const div = document.createElement('div')
div.className = `alert ${className}`
div.appendChild(document.createTextNode(message));
const container = document.querySelector('.container');
container.insertBefore(div, form);
setTimeout(() => {
document.querySelector('.alert').remove()
}, 3000);
}
}
// Store class
class Store {
static getBook() {
let books = JSON.parse(localStorage.getItem('books'))
if(!books) {
books = []
}
return books;
}
static addBook(book) {
const books = Store.getBook();
books.push(book);
localStorage.setItem('books', JSON.stringify(books))
render()
}
static pushBookList(books) {
localStorage.setItem('books', JSON.stringify(books))
render()
}
}
// Add Book
function addBook(e) {
e.preventDefault()
const title = document.querySelector('#title')
const author = document.querySelector('#author');
const isbn = document.querySelector('#isbn');
if(title.value && author.value && isbn.value) {
const book = new Book(title.value, author.value, isbn.value);
title.value = ''
author.value = ''
isbn.value = ''
Store.addBook(book);
UI.showAlert("Successfully Add Book", 'success');
} else {
UI.showAlert("Please fill up full form", 'error');
}
}
// render display
function render() {
let books = Store.getBook()
let bookList = ''
books.forEach(({title, author, isbn}) => {
bookList += `<tr>
<td>${title}</td>
<td>${author}</td>
<td>${isbn}</td>
<td><a href="#">X</a></td>
</tr>`
});
document.querySelector('#book-list').innerHTML = bookList
}
function removeBook(e) {
if(e.target.hasAttribute('href')) {
let isbn = e.target.parentElement.previousElementSibling.textContent.trim()
let books = Store.getBook();
books.forEach((book, index) => {
if(isbn === book.isbn) {
books.splice(index, 1)
}
})
Store.pushBookList(books);
UI.showAlert('Delete book', 'success')
}
}