roshan1997's picture
fixed error
3638da2 verified
// Auth functions
window.auth = {
currentUser: JSON.parse(localStorage.getItem('currentUser')) || null,
signup: function(name, email, password) {
const user = { name, email };
localStorage.setItem('currentUser', JSON.stringify(user));
auth.currentUser = user;
showToast('Account created successfully!');
window.location.href = '/';
},
login: function(email, password) {
// In a real app, this would verify against a backend
const user = { name: 'Demo User', email };
localStorage.setItem('currentUser', JSON.stringify(user));
auth.currentUser = user;
showToast('Logged in successfully!');
window.location.href = '/';
},
logout: function() {
localStorage.removeItem('currentUser');
auth.currentUser = null;
showToast('Logged out successfully!');
window.location.href = '/';
},
isAuthenticated: function() {
return auth.currentUser !== null;
}
};
// Cart functions
window.cart = {
getCart: function() {
return JSON.parse(localStorage.getItem('cart')) || [];
},
addToCart: function(product) {
if (!auth.isAuthenticated()) {
window.location.href = '/login';
return;
}
const cartItems = cart.getCart();
const existingItem = cartItems.find(item => item.id === product.id);
if (existingItem) {
existingItem.quantity += 1;
} else {
cartItems.push({
...product,
quantity: 1
});
}
localStorage.setItem('cart', JSON.stringify(cartItems));
updateCartCount();
showToast(`${product.name} added to cart!`);
},
removeFromCart: function(productId) {
const cartItems = cart.getCart().filter(item => item.id !== productId);
localStorage.setItem('cart', JSON.stringify(cartItems));
updateCartCount();
},
updateQuantity: function(productId, newQuantity) {
const cartItems = cart.getCart();
const item = cartItems.find(item => item.id === productId);
if (item) {
if (newQuantity <= 0) {
cart.removeFromCart(productId);
} else {
item.quantity = newQuantity;
localStorage.setItem('cart', JSON.stringify(cartItems));
}
}
updateCartCount();
},
clearCart: function() {
localStorage.removeItem('cart');
updateCartCount();
}
};
// Helper functions
function updateCartCount() {
const cartCount = document.getElementById('cart-count');
const mobileCartCount = document.getElementById('mobile-cart-count');
const count = window.cart.getCart().reduce((sum, item) => sum + item.quantity, 0);
if (cartCount) {
cartCount.textContent = count;
cartCount.style.display = count > 0 ? 'flex' : 'none';
}
if (mobileCartCount) {
mobileCartCount.textContent = count;
mobileCartCount.style.display = count > 0 ? 'flex' : 'none';
}
}
function showToast(message) {
const toast = document.createElement('div');
toast.className = 'toast';
toast.textContent = message;
document.body.appendChild(toast);
setTimeout(() => {
toast.remove();
}, 3000);
}
// Load featured products
document.addEventListener('DOMContentLoaded', () => {
const featuredContainer = document.getElementById('featured-products');
if (featuredContainer) {
const products = [
{
id: 1,
name: 'Wireless Headphones',
price: 99.99,
image: 'http://static.photos/technology/320x240/1',
description: 'Premium wireless headphones with noise cancellation'
},
{
id: 2,
name: 'Smart Watch',
price: 199.99,
image: 'http://static.photos/technology/320x240/2',
description: 'Track your fitness and stay connected'
},
{
id: 3,
name: 'Bluetooth Speaker',
price: 79.99,
image: 'http://static.photos/technology/320x240/3',
description: 'Portable speaker with crystal clear sound'
},
{
id: 4,
name: 'Wireless Charger',
price: 29.99,
image: 'http://static.photos/technology/320x240/4',
description: 'Fast charging for all Qi-enabled devices'
}
];
products.forEach(product => {
const productCard = document.createElement('div');
productCard.className = 'product-card bg-white rounded-lg overflow-hidden shadow-md';
productCard.innerHTML = `
<img src="${product.image}" alt="${product.name}" class="w-full h-48 object-cover">
<div class="p-4">
<h3 class="font-semibold text-lg mb-1">${product.name}</h3>
<p class="text-gray-600 text-sm mb-2">${product.description}</p>
<div class="flex justify-between items-center mt-4">
<span class="font-bold text-primary">$${product.price.toFixed(2)}</span>
<button onclick="cart.addToCart(${JSON.stringify(product).replace(/"/g, '&quot;')})"
class="bg-primary text-white px-4 py-2 rounded hover:bg-primary-600 transition">
Add to Cart
</button>
</div>
</div>
`;
featuredContainer.appendChild(productCard);
});
}
updateCartCount();
});