forked from dwarvesf/df-frontend-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
153 lines (131 loc) · 5.66 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
147
148
149
150
151
152
153
const openModalBtn = document.querySelector('#open-modal-btn');
const closeModalBtn = document.getElementById('close-modal-btn');
const modal = document.getElementById('modal');
const searchBox = document.getElementById('search-input');
const delButtons = document.querySelectorAll('button.delete-btn');
function App() {
// database for storing records of books
let bookList = [
{"name": "Refactoring", "author": "Martin Fowler", "topic": "Software Engineering"},
{"name": "Clean Code", "author": "Robert Cecil Martin", "topic": "Software Engineering"},
{"name": "Adaptive Code", "author": "Gary McLean Hall", "topic": "Software Engineering"},
{"name": "DON'T MAKE ME THINK", "author": "Steve Krug", "topic": "Software Engineering"},
{"name": "Soft Skills", "author": "John Sonmez", "topic": "Software Engineering"},
{"name": "The Pragmatic Programmer", "author": "David Thomas", "topic": "Software Engineering"},
{"name": "Head First Design Patterns", "author": "Eric Freeman", "topic": "Software Engineering"},
];
// build the table
function buildTable(data) {
var table = document.getElementById("table-body");
table.innerHTML = "";
for (var i = 0; i < data.length; i++) {
row = `
<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600">
<td class="px-6 py-4">
${data[i].name}
</td>
<td class="px-6 py-4">
${data[i].author}
</td>
<td class="px-6 py-4">
${data[i].topic}
</td>
<td class="px-6 py-4">
<button id="delete-${data[i].name}" class="delete-btn font-medium text-blue-600 dark:text-blue-500 hover:underline">
Delete
</button>
</td>
</tr> `
// table.appendChild(row);
table.innerHTML += row;
}
// add event listener to delete button
const delButtons = document.querySelectorAll('button.delete-btn');
console.log(delButtons);
delButtons.forEach((btn,i) => btn.addEventListener('click', (e) => handleDeleteBook(e, bookList[i])))
}
// assign event listener to buttons
function handleAddEvent() {
openModalBtn.addEventListener('click', () => {
console.log('open modal');
modal.classList.replace('hidden', 'block');
document.getElementById('overlay').classList.replace('hidden', 'block');
})
closeModalBtn.addEventListener('click', () => {
console.log('close modal');
modal.classList.replace('block', 'hidden');
document.getElementById('overlay').classList.replace('block', 'hidden');
emptyModal();
})
document.getElementById('cancel-btn').addEventListener('click', () => {
console.log('close modal');
modal.classList.replace('block', 'hidden');
document.getElementById('overlay').classList.replace('block', 'hidden');
emptyModal();
})
document.getElementById('add-book-btn').addEventListener('click', () => {
console.log('add book');
addBook();
modal.classList.replace('block', 'hidden');
document.getElementById('overlay').classList.replace('block', 'hidden');
emptyModal();
})
searchBox.addEventListener('input', () => {searchBook();})
}
// delete a book
function handleDeleteBook(e, dataBook) {
const consfirmed = window.confirm(`Are you sure you want to delete this book?`)
console.log(e.target)
if (consfirmed) {
const row = e.target.parentElement.parentElement;
row.remove();
let newList = bookList.filter((book) => book.name !== dataBook.name)
bookList = [...newList]
console.log(bookList)
}
}
// search for a book
function searchBook() {
var search = document.getElementById("search-input").value;
var result = filter(search, bookList);
buildTable(result);
};
function filter(search, bookList) {
var filteredData = [];
for (var i = 0; i < bookList.length; i++) {
search = search.toLowerCase();
let name = bookList[i].name.toLowerCase();
if (name.includes(search)) {
console.log(bookList[i]);
filteredData.push(bookList[i]);
}
}
return filteredData;
}
// add a new book
function addBook() {
let name = document.getElementById("name").value;
let author = document.getElementById("author").value;
let topic = document.getElementById("topic").value;
let book = {
"name": name,
"author": author,
"topic": topic
};
if (name === "" || author === "" || topic === "" ) {
alert("Please fill in all fields");
name.focus();
} else {
bookList.push(book);
buildTable(bookList);
}
}
function emptyModal() {
document.getElementById("name").value = "";
document.getElementById("author").value = "";
document.getElementById("topic").value = "";
}
buildTable(bookList);
handleAddEvent();
}
App();