-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
220 lines (191 loc) · 5.83 KB
/
library.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
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
//This is our DB
let lib = [
{
id: "1",
book: "Book1",
author: "Author1",
lender: "UserC",
borrower: "UserB",
action: "-"
},
{
id: "2",
book: "Book2",
author: "Author2",
lender: "UserC",
borrower: "-",
action: "-"
},
{
id: "3",
book: "Book3",
author: "Author3",
lender: "UserD",
borrower: "UserC",
action: "-"
},
{
id: "4",
book: "Book4",
author: "Author4",
lender: "UserA",
borrower: "-",
action: "-"
},
{
id: "5",
book: "Book5",
author: "Author5",
lender: "UserA",
borrower: "-",
action: "-"
},
{
id: "6",
book: "Book6",
author: "Author6",
lender: "UserB",
borrower: "UserA",
action: "-"
}
] //data will be stored here
const table = document.getElementById("info-table");
// function to add rows to table in DOM
const addRow = (ele) => {
const row = document.createElement("tr")
row.className = "element"
row.setAttribute("id", `row-${ele.id}`)
row.innerHTML = `
<td id="id-${ele.id}">${ele.id}</td>
<td id="book-${ele.id}">${ele.book}</td>
<td id="author-${ele.id}">${ele.author}</td>
<td id="lender-${ele.id}">${ele.lender}</td>
<td id="borrower-${ele.id}">${ele.borrower}</td>
<td id="action-${ele.id}">${ele.action}</td>
`
table.appendChild(row)
}
// Function to render the table #DB
const renderTable = () => {
lib.forEach(ele => {
addRow(ele, "element")
})
document.getElementById("logged-in-user-name").innerText = "No user logged in"
}
renderTable() //calling the function to render
let isLoggedIn = false
let loggedInUser = ""
// Function to login/change user
const changeLoggedInUser = () => {
const user = document.getElementById("logged-user")
const userName = document.getElementById("logged-in-user-name")
// if user is already logged in then log him out
if (isLoggedIn) {
logoutOldUser(userName)
}
lib.forEach(ele => {
if (ele.lender == user.value) {
userName.innerText = `Logged in user: ${user.value}`
isLoggedIn = true
loggedInUser = user.value
}
})
if (isLoggedIn)
displayLoggedInFeatures()
else { //if user not logged in (i.e. user not present in DB)
alert("User not found. Please try again.")
window.location.reload() //then refresh
}
}
// to logout exiting user
const logoutOldUser = (userName) => {
userName.innerText = "No user logged in"
isLoggedIn = false
loggedInUser = ""
const lastRow = document.getElementById(`row-${lib.length+1}`)
lastRow.remove()
}
// Function to display features once the user is logged in
const displayLoggedInFeatures = () => {
addBookFeature()
returnBookFeature()
borrowBookFeature()
}
// Function for last row of table #form to add new book
const addBookFeature = () => {
const ele = {
id: lib.length + 1,
book: `<input type="text" id="newBook" placeholder="Title"></input>`,
author: `<input type="text" id="newAuthor" placeholder="Author"></input>`,
lender: loggedInUser,
borrower: "-",
action: `<button onClick="addBook()">Add Book</button>` //the addBook() is invoked from here
}
addRow(ele)
}
// Function to add new book in table and last row as the addBookFeature()
const addBook = () => {
const lastRow = document.getElementById(`row-${lib.length + 1}`)
if(document.getElementById("newBook").value==="" || document.getElementById("newAuthor").value===""){
alert("Please provide both title and author's name.")
} //edge case of entering no data and hitting add
else{
const ele = {
id: lib.length + 1,
book: document.getElementById("newBook").value,
author: document.getElementById("newAuthor").value,
lender: loggedInUser,
borrower: "-",
action: "-"
}
lastRow.remove();
lib.push(ele);
addRow(ele);
addBookFeature(); //addBookFeature is placed again at last
}
}
// Function implementing return book functionality
const returnBookFeature = () => {
lib.forEach((ele) => {
let action = document.getElementById(`action-${ele.id}`)
if (ele.borrower == loggedInUser) {
action.innerHTML = `<button id=return-button-${ele.id} onClick="returnBook(${ele.id})">
Return
</button>`
}
else{
action.innerHTML="-"
}
})
}
// function to implement return book onClick
const returnBook = (id) => {
const borrower = document.getElementById(`borrower-${id}`)
const action = document.getElementById(`action-${id}`)
borrower.innerText = "-"
lib[id - 1].borrower = "-"
action.innerHTML = `<button id=borrow-button-${id} onClick="borrowBook(${id})">
Borrow
</button>`
}
// Function implementing borrow book functionality
const borrowBookFeature = () => {
lib.forEach((ele) => {
if (ele.borrower == "-" && ele.lender != loggedInUser) {
let action = document.getElementById(`action-${ele.id}`)
action.innerHTML = `<button id=borrow-button-${ele.id} onClick="borrowBook(${ele.id})">
Borrow
</button>`
}
})
}
// function to implement borrow book onClick
const borrowBook = (id) => {
const borrower = document.getElementById(`borrower-${id}`)
const action = document.getElementById(`action-${id}`)
borrower.innerText = loggedInUser
lib[id - 1].borrower = loggedInUser
action.innerHTML = `<button id=borrow-button-${id} onClick="returnBook(${id})">
Return
</button>`
}