Spaces:
Running
Running
File size: 5,709 Bytes
d8290f1 0b3f135 d8290f1 0b3f135 d8290f1 0b3f135 d8290f1 0b3f135 d8290f1 | 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 |
// Authentication state
let isAuthenticated = false;
let currentUser = null;
// Check authentication on page load
function checkAuth() {
const token = localStorage.getItem('authToken');
if (token && window.location.pathname.includes('login.html')) {
window.location.href = 'index.html';
} else if (!token && !window.location.pathname.includes('login.html')) {
window.location.href = 'login.html';
}
}
// Login function
function login(email, password) {
// In a real app, this would be an API call
if (email === 'admin@shopsphere.com' && password === 'admin123') {
localStorage.setItem('authToken', 'dummy-token');
localStorage.setItem('userRole', 'admin');
isAuthenticated = true;
currentUser = { email, role: 'admin' };
return true;
}
return false;
}
// Logout function
function logout() {
localStorage.removeItem('authToken');
localStorage.removeItem('userRole');
isAuthenticated = false;
currentUser = null;
window.location.href = 'login.html';
}
document.addEventListener('DOMContentLoaded', function() {
checkAuth();
// Handle login form submission
const loginForm = document.getElementById('login-form');
if (loginForm) {
loginForm.addEventListener('submit', function(e) {
e.preventDefault();
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
if (login(email, password)) {
window.location.href = 'index.html';
} else {
alert('Invalid credentials');
}
});
}
// Sample product data - in a real app, this would come from an API
const products = [
{
id: 1,
name: 'Wireless Headphones',
price: 99.99,
stock: 45,
image: 'http://static.photos/technology/200x200/1'
},
{
id: 2,
name: 'Smart Watch',
price: 199.99,
stock: 12,
image: 'http://static.photos/technology/200x200/2'
},
{
id: 3,
name: 'Bluetooth Speaker',
price: 59.99,
stock: 30,
image: 'http://static.photos/technology/200x200/3'
}
];
// Render products table
const productsTable = document.getElementById('products-table');
if (productsTable) {
products.forEach(product => {
const row = document.createElement('tr');
row.className = 'hover:bg-gray-50';
row.innerHTML = `
<td class="px-6 py-4 whitespace-nowrap">
<div class="flex items-center">
<div class="flex-shrink-0 h-10 w-10">
<img class="h-10 w-10 rounded-full" src="${product.image}" alt="${product.name}">
</div>
<div class="ml-4">
<div class="text-sm font-medium text-gray-900">${product.name}</div>
</div>
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">$${product.price.toFixed(2)}</td>
<td class="px-6 py-4 whitespace-nowrap">
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full ${product.stock > 10 ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'}">
${product.stock} in stock
</span>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
<a href="edit-product.html?id=${product.id}" class="text-primary hover:text-primary-600 mr-3">Edit</a>
<button class="text-red-600 hover:text-red-900" onclick="deleteProduct(${product.id})">Delete</button>
</td>
`;
productsTable.appendChild(row);
});
}
// Handle product form submission
const productForm = document.getElementById('product-form');
if (productForm) {
productForm.addEventListener('submit', function(e) {
e.preventDefault();
alert('Product saved successfully!');
window.location.href = 'index.html';
});
}
});
function deleteProduct(id) {
if (!isAuthenticated) {
alert('Please login to perform this action');
return;
}
if (confirm('Are you sure you want to delete this product?')) {
alert(`Product ${id} deleted`);
// In a real app, you would call an API to delete the product
// Then refresh the page or remove the row from the table
}
}
// Add logout button to navbar
function setupNavbar() {
const navbar = document.querySelector('custom-navbar');
if (navbar && isAuthenticated) {
const userMenu = navbar.shadowRoot.getElementById('user-menu-dropdown');
if (userMenu) {
userMenu.innerHTML = `
<a href="account.html" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">Your Profile</a>
<a href="settings.html" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">Settings</a>
<a href="#" id="logout-btn" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">Sign out</a>
`;
const logoutBtn = navbar.shadowRoot.getElementById('logout-btn');
if (logoutBtn) {
logoutBtn.addEventListener('click', logout);
}
}
}
}
// Call setup functions
setupNavbar();
|