-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
295 lines (223 loc) · 7.33 KB
/
index.js
File metadata and controls
295 lines (223 loc) · 7.33 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Service Worker!
if('serviceWorker' in navigator){
navigator.serviceWorker.register('/service-worker.js')
}
const newBookInput = `
<label for="">
Book Name
<input type="text" id="bookName" placeholder="Enter Book Name">
</label>
<label for="">
Author Name
<input type="text" id="authorName" placeholder="Enter Author Name">
</label>
<label for="">
Publisher Name
<input type="text" id="publisherName" placeholder="Enter Publisher Name">
</label>
<label for="">
Publication Date
<input type="date" id="publicationDate"placeholder="Enter Publication Date">
</label>
<button id="Submit" type="submit" onclick= "saveData()">Add Book</button>
`;
let bookData = [
{
bookName : "War and Peace",
authorName : "Leo Tolstoy",
publisherName : "David Campbell",
publicationDate : "09-03-2003"
}
,
{
bookName : "Middle March",
authorName : "George ELiot",
publisherName : "Ursula Martin",
publicationDate : "12-03-2000"
}
,
{
bookName : "Nineteen Eighty-Four",
authorName : "George Orwell",
publisherName : "D J Taylor",
publicationDate : "04-04-1993"
}
,
{
bookName : "The Confessions",
authorName : "Augustine",
publisherName : "Simon Yarrow",
publicationDate : "10-04-2009"
}
];
document.getElementById('book_input').innerHTML = newBookInput;
// getdata from local storage
const getData = () => {
let data = localStorage.getItem("booksData");
if(data){
bookData = JSON.parse(data);
}
else{
setData();
}
}
// setting data in local storage
const setData = () => {
localStorage.setItem("booksData",JSON.stringify(bookData));
}
getData();
// for checking duplicate entry
const duplicateCheck = (bookName,authorName,publisherName,publicationDate) => {
let flag = true;
for(let i=0; i<bookData.length;i++){
if(bookName === bookData[i].bookName && publisherName === bookData[i].publisherName && authorName === bookData[i].authorName && publicationDate === bookData[i].publicationDate)
{
flag = false;
}
else {
flag = true;
}
}
console.log(flag)
return flag;
}
// for saving data in local storage
const saveData = () => {
let newBookName = document.getElementById('bookName');
let newAuthorName = document.getElementById('authorName');
let newPublisherName = document.getElementById('publisherName');
let newPublicationDate = document.getElementById('publicationDate');
console.log(newBookName.value,newAuthorName.value,newPublisherName.value,newPublicationDate.value);
if(newBookName.value || newAuthorName.value || newPublicationDate.value || newPublisherName.value)
{
if(duplicateCheck(newBookName.value,newAuthorName.value,newPublisherName.value,newPublicationDate.value))
{
let bookDetails = {
bookName : newBookName.value,
authorName: newAuthorName.value,
publisherName: newPublisherName.value,
publicationDate: newPublicationDate.value
}
console.log(bookDetails)
bookData.push(bookDetails);
console.log(bookData)
outputData()
setData();
newBookName.value = "";
newAuthorName.value = "";
newPublisherName.value = "";
newPublicationDate.value = "";
}
else{
alert("Book Already exists");
newBookName.value = "";
newAuthorName.value = "";
newPublisherName.value = "";
newPublicationDate.value = "";
}
}
else{
alert("Plz Input Valid Credentials")
newBookName.value = "";
newAuthorName.value = "";
newPublisherName.value = "";
newPublicationDate.value = "";
}
}
// to show data in DOM
const outputData = () => {
let dataToAdd = `<table class="table table-borderless">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Book Name</th>
<th scope="col">Author Name</th>
<th scope="col">Publisher Name</th>
<th scope="col">Publication Date</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>`;
for(let i=0;i<bookData.length;i++){
dataToAdd += `
<tr>
<th scope="row">${i+1}</th>
<td>${bookData[i].bookName}</td>
<td>${bookData[i].authorName}</td>
<td>${bookData[i].publisherName}</td>
<td>${bookData[i].publicationDate}</td>
<td><button class= "grid_item" id="edit" onclick="editData(${i})">Edit</button></td>
<td> <button class= "grid_item" id="delete" onclick = "deleteData(${i})">Delete</button> </td>
</tr>
`;
}
dataToAdd = dataToAdd + `</tbody>
</table>`;
document.getElementById('allBooks').innerHTML = dataToAdd;
}
outputData();
// to delete book
const deleteData = (index) => {
bookData=bookData.filter((books,i) => i !==index);
document.getElementById('book_input').innerHTML = newBookInput;
setData();
outputData();
}
// to edit book data
const editData = (index) => {
const editInput = `
<label for="">
Book Name
<input type="text" id="newBookName" placeholder="Enter Book Name" value = "${bookData[index].bookName}">
</label>
<label for="">
Author Name
<input type="text" id="newAuthorName" placeholder="Enter Author Name" value = "${bookData[index].authorName}">
</label>
<label for="">
Publisher Name
<input type="text" id="newPublisherName" placeholder="Enter Publisher Name" value = "${bookData[index].publisherName}">
</label>
<label for="">
Publication Date
<input type="date" id="newPublicationDate" placeholder="Enter Publication Date" value = "${bookData[index].publicationDate}">
</label>
<button id="Submit" type="submit" onclick= "updateData(${index})">Update Book</button>
`;
document.getElementById('book_input').innerHTML = editInput;
}
// to update the edited book data
const updateData = (index) => {
let newBookName = document.getElementById('newBookName');
let newAuthorName = document.getElementById('newAuthorName');
let newPublisherName = document.getElementById('newPublisherName');
let newPublicationDate = document.getElementById('newPublicationDate');
if(duplicateCheck(newBookName.value,newAuthorName.value,newPublisherName.value,newPublicationDate.value))
{
bookData[index] = {
bookName: newBookName.value,
authorName: newAuthorName.value,
publisherName: newPublisherName.value,
publicationDate: newPublicationDate.value
}
setData();
outputData();
newBookName.value = "";
newAuthorName.value = "";
newPublisherName.value = "";
newPublicationDate.value = "";
document.getElementById('book_input').innerHTML = newBookInput;
}
else{
alert("Book Already exists");
newBookName.value = "";
newAuthorName.value = "";
newPublisherName.value = "";
newPublicationDate.value = "";
}
}
// for hamburger menu toggle
const toggle = () => {
document.getElementById('hamburger').classList.toggle('show');
}