Fix: Restore website functionality - all pages and APIs working
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
console.log('[main.js] Loading...');
|
||||
console.log("[main.js] Loading...");
|
||||
|
||||
// Global state management
|
||||
window.AppState = {
|
||||
@@ -20,12 +20,18 @@
|
||||
|
||||
// Initialize state from localStorage
|
||||
init() {
|
||||
console.log('[AppState] Initializing...');
|
||||
console.log('[AppState] window.AppState exists:', !!window.AppState);
|
||||
console.log("[AppState] Initializing...");
|
||||
console.log("[AppState] window.AppState exists:", !!window.AppState);
|
||||
this.loadCart();
|
||||
this.loadWishlist();
|
||||
this.updateUI();
|
||||
console.log('[AppState] Initialized - Cart:', this.cart.length, 'items, Wishlist:', this.wishlist.length, 'items');
|
||||
console.log(
|
||||
"[AppState] Initialized - Cart:",
|
||||
this.cart.length,
|
||||
"items, Wishlist:",
|
||||
this.wishlist.length,
|
||||
"items"
|
||||
);
|
||||
},
|
||||
|
||||
// Cart management
|
||||
@@ -55,18 +61,26 @@
|
||||
},
|
||||
|
||||
addToCart(product, quantity = 1) {
|
||||
console.log('[AppState] addToCart called:', product, 'quantity:', quantity);
|
||||
console.log(
|
||||
"[AppState] addToCart called:",
|
||||
product,
|
||||
"quantity:",
|
||||
quantity
|
||||
);
|
||||
const existing = this.cart.find((item) => item.id === product.id);
|
||||
if (existing) {
|
||||
console.log('[AppState] Product exists in cart, updating quantity');
|
||||
console.log("[AppState] Product exists in cart, updating quantity");
|
||||
existing.quantity += quantity;
|
||||
} else {
|
||||
console.log('[AppState] Adding new product to cart');
|
||||
console.log("[AppState] Adding new product to cart");
|
||||
this.cart.push({ ...product, quantity });
|
||||
}
|
||||
console.log('[AppState] Cart after add:', this.cart);
|
||||
console.log("[AppState] Cart after add:", this.cart);
|
||||
this.saveCart();
|
||||
this.showNotification(`${product.name || product.title || 'Item'} added to cart`, "success");
|
||||
this.showNotification(
|
||||
`${product.name || product.title || "Item"} added to cart`,
|
||||
"success"
|
||||
);
|
||||
},
|
||||
|
||||
removeFromCart(productId) {
|
||||
@@ -77,9 +91,11 @@
|
||||
|
||||
updateCartQuantity(productId, quantity) {
|
||||
const item = this.cart.find((item) => item.id === productId);
|
||||
|
||||
// Dispatch custom event for cart dropdown
|
||||
window.dispatchEvent(new CustomEvent('cart-updated', { detail: this.cart }));
|
||||
|
||||
// Dispatch custom event for cart dropdown
|
||||
window.dispatchEvent(
|
||||
new CustomEvent("cart-updated", { detail: this.cart })
|
||||
);
|
||||
if (item) {
|
||||
item.quantity = Math.max(1, quantity);
|
||||
this.saveCart();
|
||||
@@ -118,18 +134,20 @@
|
||||
},
|
||||
|
||||
addToWishlist(product) {
|
||||
if (!this.wishlist.find(`${product.name || product.title || 'Item'} added to wishlist`, "success");
|
||||
|
||||
// Dispatch custom event for wishlist dropdown
|
||||
window.dispatchEvent(new CustomEvent('wishlist-updated', { detail: this.wishlist }));
|
||||
} else {
|
||||
this.showNotification("Already in wishlist", "info.id)) {
|
||||
if (!this.wishlist.find((item) => item.id === product.id)) {
|
||||
this.wishlist.push(product);
|
||||
this.saveWishlist();
|
||||
|
||||
// Dispatch custom event for wishlist dropdown
|
||||
window.dispatchEvent(new CustomEvent('wishlist-updated', { detail: this.wishlist }));
|
||||
this.showNotification("Added to wishlist", "success");
|
||||
this.showNotification(
|
||||
`${product.name || product.title || "Item"} added to wishlist`,
|
||||
"success"
|
||||
);
|
||||
|
||||
// Dispatch custom event for wishlist dropdown
|
||||
window.dispatchEvent(
|
||||
new CustomEvent("wishlist-updated", { detail: this.wishlist })
|
||||
);
|
||||
} else {
|
||||
this.showNotification("Already in wishlist", "info");
|
||||
}
|
||||
},
|
||||
|
||||
@@ -151,32 +169,44 @@
|
||||
|
||||
updateCartUI() {
|
||||
const count = this.getCartCount();
|
||||
console.log('[AppState] Updating cart UI, count:', count);
|
||||
console.log("[AppState] Updating cart UI, count:", count);
|
||||
const badge = document.getElementById("cartCount");
|
||||
if (badge) {
|
||||
badge.textContent = count;
|
||||
badge.style.display = count > 0 ? "flex" : "none";
|
||||
console.log('[AppState] Cart badge updated');
|
||||
if (count > 0) {
|
||||
badge.classList.add("show");
|
||||
} else {
|
||||
badge.classList.remove("show");
|
||||
}
|
||||
console.log("[AppState] Cart badge updated");
|
||||
} else {
|
||||
console.warn('[AppState] Cart badge element not found');
|
||||
console.warn("[AppState] Cart badge element not found");
|
||||
}
|
||||
// Also trigger cart dropdown update
|
||||
window.dispatchEvent(new CustomEvent('cart-updated', { detail: this.cart }));
|
||||
window.dispatchEvent(
|
||||
new CustomEvent("cart-updated", { detail: this.cart })
|
||||
);
|
||||
},
|
||||
|
||||
updateWishlistUI() {
|
||||
const count = this.wishlist.length;
|
||||
console.log('[AppState] Updating wishlist UI, count:', count);
|
||||
console.log("[AppState] Updating wishlist UI, count:", count);
|
||||
const badge = document.getElementById("wishlistCount");
|
||||
if (badge) {
|
||||
badge.textContent = count;
|
||||
badge.style.display = count > 0 ? "flex" : "none";
|
||||
console.log('[AppState] Wishlist badge updated');
|
||||
if (count > 0) {
|
||||
badge.classList.add("show");
|
||||
} else {
|
||||
badge.classList.remove("show");
|
||||
}
|
||||
console.log("[AppState] Wishlist badge updated");
|
||||
} else {
|
||||
console.warn('[AppState] Wishlist badge element not found');
|
||||
console.warn("[AppState] Wishlist badge element not found");
|
||||
}
|
||||
// Also trigger wishlist dropdown update
|
||||
window.dispatchEvent(new CustomEvent('wishlist-updated', { detail: this.wishlist }));
|
||||
window.dispatchEvent(
|
||||
new CustomEvent("wishlist-updated", { detail: this.wishlist })
|
||||
);
|
||||
},
|
||||
|
||||
// Notifications
|
||||
@@ -341,14 +371,17 @@
|
||||
};
|
||||
|
||||
// Initialize on DOM ready
|
||||
console.log('[main.js] Script loaded, document.readyState:', document.readyState);
|
||||
console.log(
|
||||
"[main.js] Script loaded, document.readyState:",
|
||||
document.readyState
|
||||
);
|
||||
if (document.readyState === "loading") {
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
console.log('[main.js] DOMContentLoaded fired');
|
||||
console.log("[main.js] DOMContentLoaded fired");
|
||||
window.AppState.init();
|
||||
});
|
||||
} else {
|
||||
console.log('[main.js] DOM already loaded, initializing immediately');
|
||||
console.log("[main.js] DOM already loaded, initializing immediately");
|
||||
window.AppState.init();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user