-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuserPanel.js
122 lines (106 loc) · 3.76 KB
/
userPanel.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
const bookList = document.querySelector('#book-list tbody');
const logoutButton = document.querySelector('#logout-button');
// Get the current user from local storage
let currentUser = localStorage.getItem('currentUser');
function displayBooks() {
const books = JSON.parse(localStorage.getItem('books')) || [];
const html = books.map((book, index) => `
<tr>
<td>${book.title}</td>
<td>${book.author}</td>
<td>${book.description}</td>
<td>${book.status}</td>
<td>${book.borrowedBy || ''}</td>
<td>
${book.status === 'Free' ? `<button onclick="reserveBook(${index})">Reserve</button>` : ''}
${book.status === 'Reserved' && book.reservedBy === currentUser ? `<button onclick="borrowBook(${index})">Borrow</button> <button onclick="unreserveBook(${index})">Unreserve</button>` : ''}
${book.status === 'Borrowed' && book.borrowedBy === currentUser ? `<button onclick="returnBook(${index})">Return</button>` : ''}
<button onclick="viewBookHistory(${index})">View History</button>
</td>
</tr>
`).join('');
bookList.innerHTML = html;
}
function reserveBook(index) {
const books = JSON.parse(localStorage.getItem('books'));
const book = books[index];
const history = book.history || [];
history.push({ status: book.status, date: new Date().toLocaleString() });
book.status = 'Reserved';
book.reservedBy = currentUser;
book.history = history;
localStorage.setItem('books', JSON.stringify(books));
displayBooks();
}
function unreserveBook(index) {
const books = JSON.parse(localStorage.getItem('books'));
const book = books[index];
const history = book.history || [];
if (book.reservedBy === currentUser) {
history.push({ status: book.status, date: new Date().toLocaleString() });
book.status = 'Free';
book.reservedBy = null;
book.history = history;
localStorage.setItem('books', JSON.stringify(books));
displayBooks();
} else {
alert('This book is not reserved by you.');
}
}
function borrowBook(index) {
const books = JSON.parse(localStorage.getItem('books'));
const book = books[index];
const history = book.history || [];
if (book.reservedBy === currentUser) {
history.push({ status: book.status, date: new Date().toLocaleString() });
book.status = 'Borrowed';
book.borrowedBy = currentUser;
book.history = history;
localStorage.setItem('books', JSON.stringify(books));
displayBooks();
} else {
alert('This book is not reserved by you.');
}
}
function returnBook(index) {
const books = JSON.parse(localStorage.getItem('books'));
const book = books[index];
const history = book.history || [];
if (book.borrowedBy === currentUser) {
history.push({ status: book.status, date: new Date().toLocaleString() });
book.status = 'Free';
book.reservedBy = null;
book.borrowedBy = null;
book.history = history;
localStorage.setItem('books', JSON.stringify(books));
displayBooks();
} else {
alert('This book is not borrowed by you.');
}
}
function viewBookHistory(index) {
const books = JSON.parse(localStorage.getItem('books'));
const book = books[index];
const history = book.history || [];
let html = '<h1>Book History</h1>';
if (history.length === 0) {
html += '<p>No history available for this book.</p>';
} else {
html += '<ul>';
history.forEach(item => {
html += `<li>${item.status} - ${item.date}</li>`;
});
html += '</ul>';
}
html += '<button onclick="goBack()">Return</button>';
document.body.innerHTML = html;
}
function goBack() {
window.history.back();
}
logoutButton.addEventListener('click', function() {
// Remove currentUser from local storage upon logout
localStorage.removeItem('currentUser');
window.location.href = 'index.html';
});
displayBooks();