-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfirebase.js
More file actions
187 lines (170 loc) · 5.89 KB
/
Copy pathfirebase.js
File metadata and controls
187 lines (170 loc) · 5.89 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.4.1/firebase-app.js";
import { getAuth, GoogleAuthProvider, signInWithPopup, signOut } from "https://www.gstatic.com/firebasejs/9.4.1/firebase-auth.js";
import { getDatabase, ref, set, get, child, remove} from "https://www.gstatic.com/firebasejs/9.4.1/firebase-database.js";
const firebaseConfig = {
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const provider = new GoogleAuthProvider(app);
const db = getDatabase();
let total = 0
const dbRef = ref(getDatabase());
Notiflix.Notify.Init({});
function createHash(username){
var result = "";
for(var i = 0; i < username.length;i++){
if(username[i] == '.' || username[i] == '#' || username[i] == "$" || username[i] == "[" || username[i] == "]")
continue;
result += username[i]
}
return result
}
async function addtoCart(displayName, price, img){
const userid = getAuth().currentUser;
if(!userid){
document.getElementById("login").click();
return;
}
const d = new Date();
const time = d.getTime().toString()
const name = displayName.toUpperCase()
const username = createHash(userid.email)
const str = "/users/" + username + "/cart/" + time
const obj = {
displayName:name,
price:price,
img:img,
time:time
}
set(ref(db, str), obj).then( () => {
Notiflix.Notify.Success("Added to cart");
}).catch( () => {
Notiflix.Notify.Failure("Please try again")
})
}
function removeItem(itemclass, price){
total -= price;
const username = createHash(getAuth().currentUser.email);
document.getElementById(itemclass).style.display = "none"
const str = "/users/" + username + "/cart/" + itemclass
remove(ref(db, str))
}
function pay(){
var options = {
"key":"rzp_test_ZHpFbfhR3rcJD8",
"amount":total * 100,
"currency":"INR",
"name": "Wardrobe",
"description":"Checkout",
"handler": function (response){
if(typeof response.razorpay_payment_id == 'undefined' || response.razorpay_payment_id < 1){
Notiflix.Notify.Failure("Payment failed");
}
else{
const username = createHash(getAuth().currentUser.email)
const str = "/users/" + username + "/cart/"
remove(ref(db, str))
Notiflix.Notify.Success("Payment successful");
}
},
"prefill": {
"name": "example",
"email" : "example@example.com",
"contact":+919900000000
},
"notes": {
"address" : "note value"
},
"theme": {
"color": "#040273"
}
}
var pay = new Razorpay(options)
pay.open()
}
async function loadCart(){
const userid = getAuth().currentUser
if(!userid){
return;
}
const username = userid.email
const div = document.getElementById("mySidebar")
div.innerHTML = `<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>`
get(child(dbRef, `users/${createHash(username)}/cart`)).then((snapshot) => {
var items = []
snapshot.forEach((i)=>{
items.push(i.val())
})
if(!items.length){
div.style.display = "block"
div.innerHTML += `<a><h4 class="float1" id = "empty__cart">The Cart is empty</h4></a>`
}
else{
total = 0
div.insertAdjacentHTML("afterbegin", `<button onclick = "pay()" class="cart__product button" id = "proceed">Proceed</button>`)
div.style.display = "flex"
div.style.flexDirection = "column"
div.style.alignItems = "center"
items.forEach((item) => {
div.insertAdjacentHTML("afterbegin",
`
<div class="product__product" id ="${item.time}">
<div class="product__box cart__product" style="height:350px">
<button onclick="removeItem(${item.time}, ${item.price})" class="button product__new" style="background-image: linear-gradient(to right, #e22d2d, #e00025);">Remove</button>
<img src="${item.img}" width=400 alt=""/>
</div>
<div class="product__data">
<h3 class="product__name">${item.displayName}</h3>
<span class="product__preci">$${item.price}</span>
</div>
</div>
`
)
total += item.price
})
}
})
}
getAuth().onAuthStateChanged(function (user) {
if (user) {
get(child(dbRef, `users/${createHash(user.email)}`)).then((snapshot) => {
if(!snapshot.exists()){
const obj = {
username:user.displayName,
email:user.email,
avatar:user.photoURL
}
set(ref(db, "users/"+createHash(user.email)), obj)
}
}).catch((error) => {
console.error(error);
});
document.getElementById("login").style.display = "none"
document.getElementById("logout").style.display = "block";
}
else {
document.getElementById("login").style.display = "block"
document.getElementById("logout").style.display = "none";
}
});
function login() {
signInWithPopup(auth, provider)
.then((result) => {
location.reload()
})
.catch((error) => {});
}
function logout() {
signOut(auth)
.then(() => {
location.reload()
})
.catch((error) => {});
location.reload()
}
window.login = login
window.logout = logout
window.addtoCart = addtoCart
window.loadCart = loadCart
window.removeItem = removeItem
window.pay = pay