refactor: enhance category data fetching logic in SharedCarousel and CategoryList components by simplifying cache checks and improving logging for better maintainability

This commit is contained in:
sebseb7
2025-07-23 10:13:41 +02:00
parent a2b7a2509f
commit e472e6bb77
3 changed files with 69 additions and 138 deletions

View File

@@ -15,6 +15,15 @@ import { useParams, useSearchParams } from 'react-router-dom';
import { getAllSettingsWithPrefix } from '../utils/sessionStorage.js';
import { withI18n } from '../i18n/withTranslation.js';
// Check if window.productCache exists and has the categoryTree_209 key
if (typeof window !== 'undefined') {
console.log('Content.js loaded - window.productCache exists:', !!window.productCache);
if (window.productCache) {
console.log('Content.js loaded - window.productCache keys:', Object.keys(window.productCache));
console.log('Content.js loaded - categoryTree_209 exists:', !!window.productCache["categoryTree_209"]);
}
}
const isNew = (neu) => neu && (new Date().getTime() - new Date(neu).getTime() < 30 * 24 * 60 * 60 * 1000);
// @note SwashingtonCP font is now loaded globally via index.css
@@ -310,53 +319,26 @@ class Content extends Component {
fetchCategoryData(categoryId) {
console.log("============= FETCH CATEGORY DATA =============");
console.log(`Fetching category data for ID: ${categoryId}`);
console.log("window.productCache exists:", !!window.productCache);
// Detailed logging of window.productCache
// Simple direct check for the cache
console.log("fetchCategoryData - window.productCache exists:", !!window.productCache);
if (window.productCache) {
console.log("window.productCache keys:", Object.keys(window.productCache));
console.log("window.productCache type:", typeof window.productCache);
console.log("window.productCache is array:", Array.isArray(window.productCache));
// Log categoryTree_209 details
console.log("categoryTree_209 exists:", !!window.productCache["categoryTree_209"]);
console.log("fetchCategoryData - categoryTree_209 exists:", !!window.productCache["categoryTree_209"]);
if (window.productCache["categoryTree_209"]) {
console.log("categoryTree_209 type:", typeof window.productCache["categoryTree_209"]);
console.log("categoryTree_209 keys:", Object.keys(window.productCache["categoryTree_209"]));
console.log("categoryTree exists:", !!window.productCache["categoryTree_209"].categoryTree);
if (window.productCache["categoryTree_209"].categoryTree) {
console.log("categoryTree type:", typeof window.productCache["categoryTree_209"].categoryTree);
console.log("categoryTree is object:", typeof window.productCache["categoryTree_209"].categoryTree === 'object');
console.log("categoryTree sample:", JSON.stringify(window.productCache["categoryTree_209"].categoryTree).substring(0, 100) + "...");
}
console.log("fetchCategoryData - categoryTree_209.categoryTree exists:",
!!window.productCache["categoryTree_209"].categoryTree);
}
} else {
console.log("window.productCache is not defined or null");
}
// IMPORTANT: Check for the exact structure mentioned in the requirement
// window.productCache = {"categoryTree_209":{"categoryTree": ...}}
const hasCache = typeof window.productCache === 'object' &&
window.productCache !== null &&
typeof window.productCache["categoryTree_209"] === 'object' &&
window.productCache["categoryTree_209"] !== null &&
typeof window.productCache["categoryTree_209"].categoryTree === 'object' &&
window.productCache["categoryTree_209"].categoryTree !== null;
console.log("Has valid cache structure:", hasCache);
if (hasCache) {
console.log("✅ Found categoryTree_209.categoryTree in cache");
if (window.productCache &&
window.productCache["categoryTree_209"] &&
window.productCache["categoryTree_209"].categoryTree) {
console.log("Found categoryTree_209.categoryTree in cache");
// Check for specific category data in cache
const cachedData = getCachedCategoryData(categoryId);
console.log("Has cached category data:", !!cachedData);
if (cachedData) {
console.log("Using cached category data for", categoryId);
console.log("Using cached category data for", categoryId);
this.processDataWithCategoryTree(cachedData, categoryId);
return;
} else {
@@ -367,14 +349,13 @@ class Content extends Component {
attributes: [],
categoryName: this.getCategoryNameFromTree(categoryId)
};
console.log("Using category tree data without products");
console.log("Category name from tree:", emptyResponse.categoryName);
console.log("Using category tree data without products");
this.processDataWithCategoryTree(emptyResponse, categoryId);
return;
}
}
console.log("No category tree found in cache, proceeding with socket.io query");
console.log("No category tree found in cache, proceeding with socket.io query");
// Only if we don't have the category tree in cache, proceed with socket.io query
console.log(`Registering for productList:${categoryId}`);