forked from Elexy101/PrivMart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
366 lines (303 loc) · 11.3 KB
/
script.js
File metadata and controls
366 lines (303 loc) · 11.3 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Application State
const appState = {
currentPage: 'home',
isWalletConnected: false,
walletBalance: 0,
cart: [],
products: [
{
id: '1',
name: 'Leo T-Shirt',
price: 29.99,
image: 'Leo_Tshirt.jpeg',
category: 'Clothing'
},
{
id: '2',
name: 'Leo Hoodie',
price: 59.99,
image: 'Leo_Hoodie.jpeg',
category: 'Clothing'
},
{
id: '3',
name: 'Leo Mug',
price: 14.99,
image: 'Leo_Mug.jpeg',
category: 'Accessories'
}
]
};
// Utility Functions
function showToast(message, type = 'success') {
const container = document.getElementById('toastContainer');
const toast = document.createElement('div');
toast.className = `toast ${type}`;
toast.textContent = message;
container.appendChild(toast);
setTimeout(() => {
toast.remove();
}, 3000);
}
function formatPrice(price) {
return price.toFixed(2);
}
function updateCartBadge() {
const badge = document.getElementById('cartBadge');
const count = appState.cart.reduce((sum, item) => sum + item.quantity, 0);
badge.textContent = count;
badge.classList.toggle('show', count > 0);
}
function updateWalletBalance() {
document.getElementById('walletBalance').textContent = formatPrice(appState.walletBalance);
}
// Page Navigation
function showPage(pageId) {
// Hide all pages
document.querySelectorAll('.page').forEach(page => {
page.classList.remove('active');
});
// Show target page
document.getElementById(pageId + 'Page').classList.add('active');
// Update navigation
document.querySelectorAll('.nav-link').forEach(link => {
link.classList.remove('active');
});
const activeLink = document.querySelector(`[data-page="${pageId}"]`);
if (activeLink) {
activeLink.classList.add('active');
}
appState.currentPage = pageId;
// Load page content
if (pageId === 'products') {
loadProducts();
} else if (pageId === 'cart') {
loadCart();
}
}
// Wallet Functions
function connectWallet() {
if (!appState.isWalletConnected) {
// Simulate wallet connection
appState.isWalletConnected = true;
appState.walletBalance = 100.00; // Starting balance
updateWalletStatus();
updateWalletBalance();
showToast('Wallet connected successfully!');
}
}
function updateWalletStatus() {
const statusElements = document.querySelectorAll('#walletStatus');
statusElements.forEach(element => {
element.textContent = appState.isWalletConnected ? 'Connected' : 'Connect Wallet';
});
}
function depositFunds() {
const amount = parseFloat(document.getElementById('depositAmount').value);
if (!amount || amount <= 0) {
showToast('Please enter a valid amount', 'error');
return;
}
if (!appState.isWalletConnected) {
showToast('Please connect your wallet first', 'error');
return;
}
appState.walletBalance += amount;
updateWalletBalance();
document.getElementById('depositAmount').value = '';
showToast(`Successfully deposited $${formatPrice(amount)}`);
}
function withdrawFunds() {
const amount = parseFloat(document.getElementById('withdrawAmount').value);
if (!amount || amount <= 0) {
showToast('Please enter a valid amount', 'error');
return;
}
if (!appState.isWalletConnected) {
showToast('Please connect your wallet first', 'error');
return;
}
if (amount > appState.walletBalance) {
showToast('Insufficient funds', 'error');
return;
}
appState.walletBalance -= amount;
updateWalletBalance();
document.getElementById('withdrawAmount').value = '';
showToast(`Successfully withdrew $${formatPrice(amount)}`);
}
function checkBalance() {
if (!appState.isWalletConnected) {
showToast('Please connect your wallet first', 'error');
return;
}
// Simulate balance check with small random variation
const variation = (Math.random() - 0.5) * 0.1;
appState.walletBalance += variation;
updateWalletBalance();
showToast('Balance updated');
}
// Product Functions
function loadProducts() {
const grid = document.getElementById('productsGrid');
grid.innerHTML = '';
appState.products.forEach(product => {
const productCard = createProductCard(product);
grid.appendChild(productCard);
});
}
function createProductCard(product) {
const card = document.createElement('div');
card.className = 'product-card';
card.innerHTML = `
<div class="product-image">
<img src="${product.image}" alt="${product.name}" onerror="this.src='data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzAwIiBoZWlnaHQ9IjMwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cmVjdCB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiBmaWxsPSIjMzMzIi8+PHRleHQgeD0iNTAlIiB5PSI1MCUiIGZpbGw9IiNmZmYiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGR5PSIuM2VtIj5JbWFnZTwvdGV4dD48L3N2Zz4='">
</div>
<div class="product-info">
<h3>${product.name}</h3>
<p class="product-category">${product.category}</p>
</div>
<div class="product-footer">
<span class="product-price">$${formatPrice(product.price)}</span>
<button class="btn-add-to-cart" onclick="addToCart('${product.id}')">
Add to Cart
</button>
</div>
`;
return card;
}
function addToCart(productId) {
const product = appState.products.find(p => p.id === productId);
if (!product) return;
const existingItem = appState.cart.find(item => item.id === productId);
if (existingItem) {
existingItem.quantity += 1;
} else {
appState.cart.push({
...product,
quantity: 1
});
}
updateCartBadge();
showToast(`${product.name} added to cart`);
}
// Cart Functions
function loadCart() {
const cartItems = document.getElementById('cartItems');
const cartTotal = document.getElementById('cartTotal');
if (appState.cart.length === 0) {
cartItems.innerHTML = `
<div class="empty-cart">
<h2>Your cart is empty</h2>
<p>Add some products to get started!</p>
<button class="btn btn-wallet" onclick="showPage('products')">Shop Now</button>
</div>
`;
cartTotal.textContent = '0.00';
return;
}
cartItems.innerHTML = '';
let total = 0;
appState.cart.forEach(item => {
const cartItem = createCartItem(item);
cartItems.appendChild(cartItem);
total += item.price * item.quantity;
});
cartTotal.textContent = formatPrice(total);
}
function createCartItem(item) {
const cartItem = document.createElement('div');
cartItem.className = 'cart-item';
cartItem.innerHTML = `
<div class="cart-item-image">
<img src="${item.image}" alt="${item.name}" onerror="this.src='data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iODAiIGhlaWdodD0iODAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHJlY3Qgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgZmlsbD0iIzMzMyIvPjx0ZXh0IHg9IjUwJSIgeT0iNTAlIiBmaWxsPSIjZmZmIiB0ZXh0LWFuY2hvcj0ibWlkZGxlIiBkeT0iLjNlbSI+SW1nPC90ZXh0Pjwvc3ZnPg=='">
</div>
<div class="cart-item-info">
<div class="cart-item-name">${item.name}</div>
<div class="cart-item-price">$${formatPrice(item.price)}</div>
</div>
<div class="cart-item-controls">
<button class="quantity-btn" onclick="updateQuantity('${item.id}', -1)">-</button>
<span>${item.quantity}</span>
<button class="quantity-btn" onclick="updateQuantity('${item.id}', 1)">+</button>
<button class="btn btn-sm" onclick="removeFromCart('${item.id}')" style="margin-left: 1rem;">Remove</button>
</div>
`;
return cartItem;
}
function updateQuantity(productId, change) {
const item = appState.cart.find(item => item.id === productId);
if (!item) return;
item.quantity += change;
if (item.quantity <= 0) {
removeFromCart(productId);
return;
}
updateCartBadge();
loadCart();
}
function removeFromCart(productId) {
appState.cart = appState.cart.filter(item => item.id !== productId);
updateCartBadge();
loadCart();
showToast('Item removed from cart');
}
function checkout() {
if (appState.cart.length === 0) {
showToast('Your cart is empty', 'error');
return;
}
if (!appState.isWalletConnected) {
showToast('Please connect your wallet first', 'error');
return;
}
const total = appState.cart.reduce((sum, item) => sum + (item.price * item.quantity), 0);
if (total > appState.walletBalance) {
showToast('Insufficient funds for checkout', 'error');
return;
}
// Process payment
appState.walletBalance -= total;
appState.cart = [];
updateWalletBalance();
updateCartBadge();
loadCart();
showToast(`Payment successful! $${formatPrice(total)} charged to your wallet.`);
}
// Event Listeners
document.addEventListener('DOMContentLoaded', function() {
// Navigation
document.querySelectorAll('.nav-link[data-page]').forEach(link => {
link.addEventListener('click', (e) => {
e.preventDefault();
showPage(link.dataset.page);
});
});
// Wallet connections
document.getElementById('connectWallet').addEventListener('click', connectWallet);
document.getElementById('heroConnectWallet').addEventListener('click', connectWallet);
// Wallet actions
document.getElementById('depositBtn').addEventListener('click', depositFunds);
document.getElementById('withdrawBtn').addEventListener('click', withdrawFunds);
document.getElementById('checkBalance').addEventListener('click', checkBalance);
// Navigation buttons
document.getElementById('exploreProducts').addEventListener('click', () => showPage('products'));
document.getElementById('cartBtn').addEventListener('click', () => showPage('cart'));
document.getElementById('checkoutBtn').addEventListener('click', checkout);
// Enter key support for inputs
document.getElementById('depositAmount').addEventListener('keypress', (e) => {
if (e.key === 'Enter') depositFunds();
});
document.getElementById('withdrawAmount').addEventListener('keypress', (e) => {
if (e.key === 'Enter') withdrawFunds();
});
// Initialize
updateCartBadge();
updateWalletBalance();
loadProducts();
});
// Global functions for onclick handlers
window.addToCart = addToCart;
window.updateQuantity = updateQuantity;
window.removeFromCart = removeFromCart;
window.showPage = showPage;