-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.html
197 lines (192 loc) · 8.5 KB
/
cart.html
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
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<!-- Path: contact.html -->
<html lang="en">
<head>
<link rel="shortcut icon" href="./assets/favicon/favicon.ico" type="image/x-icon" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="UTF-8" />
<meta name="description" content="Monbakery is a bakery that sells the best cakes and pastries in town." />
<meta name="keywords" content="bakery, cakes, pastries, monbakery" />
<meta name="theme-color" content="#ffffff" />
<title>Monbakery | Category</title>
<title>Payment</title>
<script src="./javascript/apicaller.js"></script>
<link rel="stylesheet" href="./css/style.css" />
<link rel="stylesheet" href="./css/cart.css" />
<!-- <script src="https://www.gstatic.com/firebasejs/7.21.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.21.1/firebase-firestore.js"></script> -->
<script src="https://www.gstatic.com/firebasejs/9.15.0/firebase-app-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.15.0/firebase-firestore-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.15.0/firebase-auth-compat.js"></script>
</head>
<body>
<header id="headercomp"></header>
<div style="display: flex">
<div class="CartContainer" style="flex: 4">
<div class="Header">
<h1 class="Heading">Shopping Cart</h1>
<!-- remove all elements in local storage -->
<div id="all-items-remove">
<h5 class="Action">Remove all</h5>
</div>
</div>
<div class="cart-items"></div>
<hr />
<div class="checkout">
<div class="total">
<div>
<div class="Subtotal">Sub-Total</div>
<div class="items">2 items</div>
</div>
<div class="total-amount">$0.00</div>
</div>
<button id="google-login" class="button" style="margin-bottom: 20px">Checkout with google</button>
</div>
</div>
<div class="CartContainer" style="margin-left: 20px; flex: 2; padding: 10px">
<div style="display: flex; align-items: center; justify-content: center">
<div id="userimage"></div>
<h2 id="username"></h2>
<div id="logout" style="cursor: pointer; display: flex; justify-content: center"></div>
</div>
<h2 class="Heading">Successful orders</h2>
<div id="compeltedorders"></div>
</div>
</div>
<script type="module" src="./javascript/header.js"></script>
<script type="module" src="./javascript/cart/index.js"></script>
<script>
// Initialize Firebase
var firebaseConfig = {
apiKey: "AIzaSyARvS2BcfjF8fmEVuoC1YFoQqNadA52-ZQ",
authDomain: "monbakery-32a8e.firebaseapp.com",
projectId: "monbakery-32a8e",
storageBucket: "monbakery-32a8e.appspot.com",
messagingSenderId: "246831590849",
appId: "1:246831590849:web:f022b50688f7d6d5321340",
measurementId: "G-P4QBW1W8DM",
};
firebase.initializeApp(firebaseConfig);
var db = firebase.firestore();
// get user from local storage if exists
var user = JSON.parse(localStorage.getItem("user"));
if (user) {
console.log(user);
document.getElementById(
"logout"
).innerHTML = `<img src='data:image/svg+xml;base64,PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj4KPCEtLSBVcGxvYWRlZCB0bzogU1ZHIFJlcG8sIHd3dy5zdmdyZXBvLmNvbSwgVHJhbnNmb3JtZWQgYnk6IFNWRyBSZXBvIE1peGVyIFRvb2xzIC0tPgo8c3ZnIGZpbGw9IiM1ZTM0NWUiIGhlaWdodD0iODAw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Cw2My40NmMtNC43MDQsNC43NTItNC43MDQsMTIuNDM5LDAsMTcuMTc5IGM0LjcwNCw0Ljc1MiwxMi4zMTksNC43NTIsMTcuMDExLDBsODIuOTk3LTg0LjJDMzg2LjExMywxOTYuNTg4LDM4Ni4xNjEsMTg4Ljc1NiwzODEuNDgxLDE4NC4wODh6Ii8+IDwvZz4gPGc+IDwvZz4gPGc+IDwvZz4gPGc+IDwvZz4gPGc+IDwvZz4gPGc+IDwvZz4gPGc+IDwvZz4gPC9nPiA8L2c+Cg08L3N2Zz4=' style="width:25px;height:25px;margin-left:15px"/>`;
document.getElementById("username").innerHTML = user.displayName;
document.getElementById(
"userimage"
).innerHTML = `<img src="${user.photoURL}" style="width: 50px; height: 50px; border-radius: 50%; margin-right: 10px" />`;
// get products from firestore where orders are in collection users user id docs cart collection
db.collection("users")
.doc(user.uid)
.collection("cart")
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
const _data = doc.data();
document.getElementById("compeltedorders").innerHTML += `<div class="Cart-Items pad">
<div class="image-box">
<img src=${_data?.image || ""} height="120px" alt="photo of item" />
</div>
<div class="about">
<h1 class="title">${_data?.nameItem || "Name of item".split(" ").slice(0, 2).join(" ")}</h1>
<h3 class="subtitle" style="margin-top:20px">${_data?.description.split(" ").slice(0, 5).join(" ")}</h3>
</div>
<div class="prices">
<div class="amount">${_data?.quantity || "1"}Ш</div>
</div>
</div>
`;
});
});
}
document.getElementById("google-login").addEventListener("click", function () {
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().useDeviceLanguage();
firebase
.auth()
.signInWithPopup(provider)
.then(async function (result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
console.log(user);
// save user info to local storage
localStorage.setItem("user", JSON.stringify(user));
// save user info to firestore
await db
.collection("users")
.doc(user.uid)
.set({
name: user.displayName,
// phone
phone: user.phoneNumber,
email: user.email,
photoURL: user.photoURL,
uuid: user.uid,
})
.then(async function () {
// write items in cart to firestore
var cartItems = await JSON.parse(localStorage.getItem("products"));
// console.log(cartItems);
cartItems.forEach(function (item) {
db.collection("users")
.doc(user.uid)
.collection("cart")
.doc(item.id)
.set({
nameItem: item.title || "0 ",
description: item.content || "0 ",
price: item.price || "0 tugrik",
quantity: item.count || "0 ",
image: item.image,
})
.catch(function (error) {
console.error("Error writing document: ", error);
});
});
})
.catch(function (error) {
console.error("Error writing document: ", error);
});
})
.then(function () {
// set local storage item to 0
if (window.location.href.includes("cart.html")) {
const myCart = document.querySelector("cart-component");
myCart.RemoveAll();
}
window.location.reload();
})
.catch(function (error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
});
// logout
document.getElementById("logout").addEventListener("click", function () {
firebase
.auth()
.signOut()
.then(function () {
// Sign-out successful.
localStorage.removeItem("user");
window.location.reload();
})
.catch(function (error) {
// An error happened.
});
});
</script>
</body>
</html>