-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibraryManagement.js
More file actions
196 lines (168 loc) · 7.21 KB
/
libraryManagement.js
File metadata and controls
196 lines (168 loc) · 7.21 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
// document.getElementById('test').style.backgroundColor = "#F6FBF4";
// document.querySelector('h1').innerHTML = "from JavaScript";
// document.querySelector('.submit').addEventListener('click', function(event){
// alert('you have pressed me....')
// });
let counter = 1;
let indexOfData;
let name = document.querySelector('#name');
let author = document.querySelector('#author');
let publisher = document.querySelector('#publisher');
let date = document.querySelector('#date');
function find(msg){
const myarray=msg.split(/\r?\n/);
console.log(myarray[0]);
console.log(myarray[1]);
console.log(myarray[2]);
console.log(myarray[3]);
console.log(myarray[4]);
let name = document.querySelector('#name');
let author = document.querySelector('#author');
let publisher = document.querySelector('#publisher');
let date = document.querySelector('#date');
name.setAttribute(myarray[0]);
author.setAttribute(myarray[1]);
// publisher.setAttribute(myarray[2]);
// date.setAttribute(myarray[3]);
}
displayData();
let library_card=5;
function Add() {
// displayData();
let name = document.querySelector('#name').value;
let author = document.querySelector('#author').value;
let publisher = document.querySelector('#publisher').value;
let date = document.querySelector('#date').value;
if (name === '' && author === '' && publisher === '' && date === '') {
document.querySelector('.error').innerHTML = '⚠ please fill this field';
document.querySelector('#authorError').innerHTML = '⚠ please fill this field';
document.querySelector('#publisherError').innerHTML = '⚠ please fill this field';
document.querySelector('#dateError').innerHTML = '⚠ please fill this field';
} else if (name != '' && author === '' && publisher === '' && date === '') {
document.querySelector('.error').innerHTML = '';
document.querySelector('#authorError').innerHTML = '⚠ please fill this field';
document.querySelector('#publisherError').innerHTML = '⚠ please fill this field';
document.querySelector('#dateError').innerHTML = '⚠ please fill this field';
} else if (name != '' && author != '' && publisher === '' && date === '') {
document.querySelector('.error').innerHTML = '';
document.querySelector('#authorError').innerHTML = '';
document.querySelector('#publisherError').innerHTML = '⚠ please fill this field';
document.querySelector('#dateError').innerHTML = '⚠ please fill this field';
} else if (name != '' && author != '' && publisher != '' && date === '') {
document.querySelector('.error').innerHTML = '';
document.querySelector('#authorError').innerHTML = '';
document.querySelector('#publisherError').innerHTML = '';
document.querySelector('#dateError').innerHTML = '⚠ please fill this field';
} else if (name === '' && author != '' && publisher === '' && date === '') {
document.querySelector('.error').innerHTML = '⚠ please fill this field';
document.querySelector('#authorError').innerHTML = '';
document.querySelector('#publisherError').innerHTML = '⚠ please fill this field';
document.querySelector('#dateError').innerHTML = '⚠ please fill this field';
} else if (name != '' && author != '' && publisher != '' && date != '') {
document.querySelector('.error').innerHTML = '';
document.querySelector('#authorError').innerHTML = '';
document.querySelector('#publisherError').innerHTML = '';
document.querySelector('#dateError').innerHTML = '';
let avalable = false;
let localdata = localStorage.getItem("bookData");
if (localdata == null) {
data = [];
} else {
data = JSON.parse(localdata);
}
data.forEach((item) =>
(item.name.toLowerCase() === name.toLowerCase()) ? avalable = true : ''
);
console.log(name)
if (avalable === false) {
data.push({ name, author, publisher, date });
localStorage.setItem('bookData', JSON.stringify(data));
document.querySelector('#name').value = '';
document.querySelector('#author').value = '';
document.querySelector('#publisher').value = '';
document.querySelector('#date').value = '';
counter += 1;
let btn = document.getElementById('Submit');
// console.log(btn)
console.log(library_card);
library_card--;
alert(library_card);
if(library_card==0){
console.log("helooo form card dedction");
btn.disabled = true;
setTimeout(disable,3000);
// btn.disabled = false;
}
function disable(){
let btn = document.getElementById('Submit');
btn.disabled = false;
}
} else {
document.querySelector('.error').innerHTML = 'Book already Exists';
// console.log(avalable)
}
// data.forEach(element => {
// console.log(element)
// });
}
displayData();
}
function displayData() {
let arr = localStorage.getItem('bookData');
if (arr == null) {
data = [];
} else {
data = JSON.parse(arr);
}
let rows = '';
data.map((item, index) => {
rows += `<tr class = 'tr'>
<td>${index + 1}</td>
<td>${item['name']}</td>
<td>${item['author']}</td>
<td>${item['publisher']}</td>
<td>${item['date']}</td>
<td>${item['date']}</td>
<td><button onClick = 'edit(${index})' id="edit">Edit</button></td>
<td><button onClick = 'deleteData(${index})'>Delete</button></td>
</tr>`
})
var tbody = document.getElementById("table");
tbody.innerHTML = rows;
// // document.querySelector('#table tbody').innerHTML = rows;
// data.forEach(a => console.log(a[0], a[1], a[2], a[3]));
}
function edit(index) {
let editBtn = document.querySelector('#edit');
let addBtn = document.querySelector('#add');
indexOfData = index;
editBtn.style.display = 'active';
addBtn.style.display = 'active';
name.value = data[index]['name'];
author.value = data[index]['author'];
publisher.value = data[index]['publisher'];
date.value = data[index]['date'];
console.log(indexOfData);
}
function deleteData(index) {
data.splice(index, 1);
localStorage.setItem('bookData', JSON.stringify(data));
displayData();
}
let editBtn = document.querySelector('#edit');
editBtn.addEventListener('click', function(){
let editBtn = document.querySelector('#edit');
let addBtn = document.querySelector('#add');
data[indexOfData]['name'] = name.value;
data[indexOfData]['author'] = author.value;
data[indexOfData]['publisher'] = publisher.value;
data[indexOfData]['date'] = date.value;
localStorage.setItem('bookData', JSON.stringify(data));
displayData();
editBtn.style.display = 'none';
addBtn.style.display = 'block';
name.value = '';
author.value = '';
publisher.value = '';
date.value = '';
})