-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.js
71 lines (65 loc) · 2.24 KB
/
status.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
const apiUrl = "http://localhost:8800/";
// const apiUrl = "https://ease-commerce.herokuapp.com/";
var currentUser = null;
const imgEl = document.getElementById("user-image");
const authWrapperEl = document.querySelector(".auth-wrapper");
const userLayoutEl = document.querySelector(".user-layout");
const userNameEl = document.querySelector(".user-name");
const userEmailEl = document.querySelector(".user-email");
// Check if user is logged in
const checkStatus = () => {
currentUser = JSON.parse(localStorage.getItem("currentUser"));
if (currentUser != null) {
if (authWrapperEl != null && userLayoutEl != null) {
authWrapperEl.classList.add("hidden");
userLayoutEl.classList.remove("hidden");
userNameEl.innerHTML = "@" + currentUser.name;
userEmailEl.innerHTML = currentUser.email;
}
if (imgEl !== null) {
imgEl.src = `https://avatars.dicebear.com/api/initials/${currentUser.name}.svg`;
}
// document.querySelector("#login").style.display = "none";
// document.querySelector("#logout").style.display = "block";
} else {
if (authWrapperEl != null && userLayoutEl != null) {
authWrapperEl.classList.remove("hidden");
userLayoutEl.classList.add("hidden");
}
if (imgEl !== null) {
imgEl.src = "https://avatars.dicebear.com/api/bottts/ram%20sam.svg";
}
// document.querySelector("#login").style.display = "block";
// document.querySelector("#logout").style.display = "none";
}
};
const changeStatus = (data) => {
localStorage.setItem("currentUser", JSON.stringify(data));
checkStatus();
};
const handleData = (data, res, success) => {
if ("success" in data) {
if (data.success) {
changeStatus(data);
showNotification(success ?? "Success");
} else {
showNotification(data.message);
}
} else {
if (res.status == 200) {
changeStatus(data);
showNotification(success ?? "Success");
} else {
showNotification("Something went wrong");
}
}
};
checkStatus();
const showNotification = (message) => {
const notificationEl = document.querySelector(".notification");
notificationEl.innerHTML = message;
notificationEl.classList.remove("hide");
setTimeout(() => {
notificationEl.classList.add("hide");
}, 3000);
};