-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddToFavorites.js
More file actions
68 lines (54 loc) · 2.24 KB
/
Copy pathaddToFavorites.js
File metadata and controls
68 lines (54 loc) · 2.24 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
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 ❤ ':notif.innerText = ' REMOVED FROM ❤ ';
toasts.appendChild(notif);
setTimeout(() => notif.remove(),
1000);
};
const favorite = document.querySelectorAll(".favorite");
for (let i = 0; i < favorite.length; i++) {
let favoriteArray = JSON.parse(localStorage.getItem("favorite"));
if(favoriteArray!=null){
if(favoriteArray.includes(favorite[i].parentElement.id)){
favorite[i].classList.add('favorite-active')
}
}
}
for (let i = 0; i < favorite.length; i++) {
favorite[i].addEventListener("click", (e) => {
e.stopPropagation();
favorite[i].classList.toggle("favorite-active");
if (localStorage.favorite == null) {
let favoriteArray = [];
if (favorite[i].classList.contains("favorite-active")) {
favoriteArray.push(favorite[i].parentElement.id);
// console.log("added to fav");
} else {
favoriteArray.pop(favorite[i].parentElement.id);
// console.log("removed from fav");
}
localStorage.setItem("favorite", JSON.stringify(favoriteArray));
} else {
let favoriteArray = JSON.parse(localStorage.getItem("favorite"));
if (favorite[i].classList.contains("favorite-active")) {
favoriteArray.push(favorite[i].parentElement.id);
createNotification('add');
// console.log("added to fav");
} else {
favoriteArray.pop(favorite[i].parentElement.id);
createNotification('rem');
// console.log("removed from fav");
}
localStorage.setItem("favorite", JSON.stringify(favoriteArray));
}
})
}
},
2000)
})