-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
148 lines (120 loc) · 3.39 KB
/
script.js
File metadata and controls
148 lines (120 loc) · 3.39 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
// ================== PI INIT ==================
Pi.init({ version: "2.0" });
let user = null;
let cart = [];
// ================== FIREBASE CONFIG ==================
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_PROJECT.firebaseapp.com",
projectId: "YOUR_PROJECT",
storageBucket: "YOUR_PROJECT.appspot.com",
messagingSenderId: "XXXX",
appId: "XXXX"
};
// INIT FIREBASE
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
const storage = firebase.storage();
// ================== LOGIN ==================
async function login() {
try {
const scopes = ['username', 'payments'];
const auth = await Pi.authenticate(scopes, onIncompletePaymentFound);
user = auth.user;
document.getElementById("user").innerText =
"Welcome " + user.username;
loadAds();
} catch (e) {
console.error(e);
}
}
// ================== POST AD ==================
async function postAd() {
const file = document.getElementById("imageInput").files[0];
const title = document.getElementById("titleInput").value;
const price = document.getElementById("priceInput").value;
if (!file !title !price) {
alert("Fill all fields");
return;
}
// UPLOAD IMAGE
const storageRef = storage.ref("ads/" + Date.now());
await storageRef.put(file);
const imageUrl = await storageRef.getDownloadURL();
// SAVE TO FIRESTORE
await db.collection("ads").add({
title: title,
price: parseFloat(price),
image: imageUrl,
owner: user.username,
createdAt: Date.now()
});
alert("Advert posted!");
loadAds();
}
// ================== LOAD ADS ==================
async function loadAds() {
const container = document.getElementById("products");
container.innerHTML = "Loading...";
const snapshot = await db.collection("ads")
.orderBy("createdAt", "desc")
.get();
container.innerHTML = "";
snapshot.forEach(doc => {
const ad = doc.data();
container.innerHTML +=
<div class="card">
<img src="${ad.image}" style="width:100%">
<h3>${ad.title}</h3>
<p>${ad.price} Pi</p>
<small>Seller: ${ad.owner}</small><br>
<button onclick="addToCart('${doc.id}', ${ad.price}, '${ad.title}')">
Add to Cart
</button>
</div>
;
});
}
// ================== CART ==================
function addToCart(id, price, title) {
cart.push({ id, price, title });
renderCart();
}
function renderCart() {
const container = document.getElementById("cartItems");
container.innerHTML = "";
let total = 0;
cart.forEach(item => {
container.innerHTML += <p>${item.title} - ${item.price} Pi</p>;
total += item.price;
});
document.getElementById("total").innerText =
"Total: " + total + " Pi";
}
// ================== CHECKOUT ==================
function checkout() {
if (cart.length === 0) {
alert("Cart empty");
return;
}
const total = cart.reduce((sum, i) => sum + i.price, 0);
Pi.createPayment({
amount: total,
memo: "AZ ZAMAN Order",
metadata: cart
}, {
onReadyForServerApproval: (paymentId) => {
console.log(paymentId);
},
onReadyForServerCompletion: (paymentId, txid) => {
alert("Payment successful!");
cart = [];
renderCart();
},
onCancel: () => alert("Cancelled"),
onError: (err) => console.error(err)
});
}
function onIncompletePaymentFound(payment) {
console.log(payment);
}