-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddToWishlist.js
More file actions
72 lines (53 loc) · 2.32 KB
/
Copy pathaddToWishlist.js
File metadata and controls
72 lines (53 loc) · 2.32 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
window.addEventListener("load", () => {
setTimeout(()=>{
const toasts = document.getElementById("toasts");
const createNotification = (info) => {
const notif = document.createElement("div");
notif.classList.add("toast");
info=='add'?notif.style.color='#00fc87':notif.style.color='#e31b23';
info=='add'?notif.innerText = ' ADDED TO WISHLIST 🌠':notif.innerText = ' REMOVED FROM WISHLIST 💫';
toasts.appendChild(notif);
setTimeout(() => notif.remove(),
1000);
};
const wishlist = document.querySelectorAll(".wishlist");
console.log(wishlist)
for (let i = 0; i < wishlist.length; i++) {
let wishlistArray = JSON.parse(localStorage.getItem("wishlist"));
if(wishlistArray!=null){
if(wishlistArray.includes(wishlist[i].parentElement.id)){
wishlist[i].classList.add('wishlist-active')
}
}
}
for (let i = 0; i < wishlist.length; i++) {
wishlist[i].addEventListener("click", (e) => {
e.stopPropagation();
wishlist[i].classList.toggle("wishlist-active");
if (localStorage.wishlist == null) {
let wishlistArray = [];
if (wishlist[i].classList.contains("wishlist-active")) {
wishlistArray.push(wishlist[i].parentElement.id);
console.log("added to wislist");
} else {
wishlistArray.pop(wishlist[i].parentElement.id);
console.log("removed from wislist");
}
localStorage.setItem("wishlist", JSON.stringify(wishlistArray));
} else {
let wishlistArray = JSON.parse(localStorage.getItem("wishlist"));
if (wishlist[i].classList.contains("wishlist-active")) {
wishlistArray.push(wishlist[i].parentElement.id);
createNotification('add');
console.log("added to wislist");
} else {
wishlistArray.pop(wishlist[i].parentElement.id);
createNotification('rem');
console.log("removed from wislist");
}
localStorage.setItem("wishlist", JSON.stringify(wishlistArray));
}
})
}
},1000)
})