-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfavourite.js
118 lines (108 loc) · 4.07 KB
/
favourite.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
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('./service-worker.js')
.then(function() { console.log('Service Worker Registered'); });
}
function signOut(){
firebase.auth().signOut().then(function() {
// Sign-out successful.
}).catch(function(error) {
// An error happened.
console.log(error);
});
}
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// console.log(user);
// User is signed in.
document.getElementById("activeUser").style.display="block";
document.getElementById("inActiveUser").style.display="none";
document.getElementById("postad").style.display="block";
// document.getElementById("userProfile").innerHTML=firebase.auth().currentUser.displayName;
document.getElementById(`suh`).innerHTML = `${firebase.auth().currentUser.displayName}'s Favourites`
document.getElementById("userProfile").style.display="block";
//Calling Function Of Fetching
fetchFavourites();
} else {
// No user is signed in
}
});
function signOut(){
firebase.auth().signOut().then(function() {
// Sign-out successful
}).catch(function(error) {
// An error happened
console.log(error);
});
}
//fetching favourites
var database = firebase.database();
//generateAdCard
function adCard(data,key){
return`
<div class="cardstyling col-lg-4 col-sm-6 portfolio-item">
<div class="card h-100">
<small>${data.displayName}</small>
<img class="validate card-img-top" src=${data.url} style="padding: 1rem !important"/>
<div class="card-body">
<h3 class="card-title">${data.title}</h3>
<h4 class="category">${data.category}</h4>
<p class="validate card-text">${data.description}</p>
<h5>${data.price}</h5>
<button type="button" class="btn btn-danger" onclick="deleteFavourite('${key}',this)">Remove From Favourite</button>
</div>
</div>
</div>
`
}
//Delete Favourite
function deleteFavourite(key, button) {
document.getElementById('row').removeChild(button.parentElement.parentElement.parentElement);
var favouritesRef = database.ref('favourites/' + firebase.auth().currentUser.uid + `/` + key).set({});
}
//search function
function searchFunction() {
var search = document.getElementById('search');
var filter = search.value.toUpperCase();
var list =document.getElementsByClassName('card-title');
for(i=0 ;i<list.length ;i++){
if(list[i].innerText.toUpperCase().indexOf(filter) > -1){
list[i].parentElement.parentElement.parentElement.style.display="";
}
else{
var a =list[i].parentElement.parentElement.parentElement;
a.parentElement.removeChild(a);
}
}
}
//CategoySelection
function selectCategory() {
var selectCategory = document.getElementById(`homePageCategorySelection`);
selectCategory.options[selectCategory.selectedIndex].value;
var categoryDivs =document.getElementsByClassName(`category`);
for(i=0 ;i<categoryDivs.length ;i++){
if(selectCategory.options[selectCategory.selectedIndex].value === `All Categories`){
categoryDivs[i].parentElement.parentElement.parentElement.style.display="";
}
else if(selectCategory.options[selectCategory.selectedIndex].value === `${categoryDivs[i].innerHTML}`){
categoryDivs[i].parentElement.parentElement.parentElement.style.display="";
}
else{
categoryDivs[i].parentElement.parentElement.parentElement.style.display="none";
}
}
}
//fetching code
function fetchFavourites(){
userId = firebase.auth().currentUser.uid;
fetch(`https://ecommerce-test-a26fc-default-rtdb.firebaseio.com/favourites/${userId}.json`)
.then(data => {
return data.json();
})
.then(data2 => {
document.getElementById(`row`).innerHTML = "";
for(let i in data2){
document.getElementById(`row`).innerHTML += adCard(data2[i],i);
}
})
}