-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon-session.js
28 lines (23 loc) · 924 Bytes
/
common-session.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
if (!isLoggedIn()) {
location.href = "index.html";
}
const userAccountBalanceElem = document.getElementById("user-account-balance");
const logoutBtn = document.getElementById("logout-btn");
// important: used in session pages
const currentUserAccountNumber = localStorage.getItem(
"MB_LOGGEDIN_USER_ACCOUNT_NUMBER"
);
logoutBtn.addEventListener("click", () => {
// remove the local storage item and redirect to login page
localStorage.removeItem("MB_LOGGEDIN_USER_ACCOUNT_NUMBER");
location.href = "index.html";
});
const getUserCurrentBalance = () => {
const currentUser = getUserByAccountNumber(currentUserAccountNumber);
const prevTransaction =
currentUser.transactions[currentUser.transactions.length - 1];
const currentBalance = prevTransaction?.balanceAfter || 0;
return currentBalance;
};
const currentBalance = getUserCurrentBalance();
userAccountBalanceElem.textContent = currentBalance;