refactor: clean up logging and simplify cache checks in Content component to enhance clarity and maintainability of category data fetching

This commit is contained in:
sebseb7
2025-07-23 10:33:28 +02:00
parent 934f6abc92
commit 31c302493a

View File

@@ -15,15 +15,6 @@ 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
@@ -200,14 +191,6 @@ class Content extends Component {
constructor(props) {
super(props);
// DEBUG: Check for window.productCache at constructor time
console.log("Content constructor - window.productCache exists:", !!window.productCache);
if (window.productCache && window.productCache["categoryTree_209"]) {
console.log("Content constructor - categoryTree_209 exists:", !!window.productCache["categoryTree_209"]);
console.log("Content constructor - categoryTree_209.categoryTree exists:",
!!window.productCache["categoryTree_209"].categoryTree);
}
this.state = {
loaded: false,
categoryName: null,
@@ -219,16 +202,6 @@ class Content extends Component {
}
componentDidMount() {
// Debug check for window.productCache
console.log("Content componentDidMount - window.productCache exists:", !!window.productCache);
if (window.productCache) {
console.log("Content componentDidMount - window.productCache keys:", Object.keys(window.productCache));
if (window.productCache["categoryTree_209"]) {
console.log("Content componentDidMount - categoryTree_209.categoryTree exists:",
!!window.productCache["categoryTree_209"].categoryTree);
}
}
if(this.props.params.categoryId) {this.setState({loaded: false, unfilteredProducts: [], filteredProducts: [], attributes: [], categoryName: null, childCategories: []}, () => {
this.fetchCategoryData(this.props.params.categoryId);
})}
@@ -286,23 +259,17 @@ class Content extends Component {
fetchCategoryData(categoryId) {
// Simple direct check for the cache
console.log("fetchCategoryData - window.productCache exists:", !!window.productCache);
if (window.productCache) {
console.log("fetchCategoryData - categoryTree_209 exists:", !!window.productCache["categoryTree_209"]);
if (window.productCache["categoryTree_209"]) {
console.log("fetchCategoryData - categoryTree_209.categoryTree exists:",
!!window.productCache["categoryTree_209"].categoryTree);
}
}
// Check if we have the category tree data in window.productCache
const hasCategoryTree = window.productCache &&
window.productCache["categoryTree_209"] &&
window.productCache["categoryTree_209"].categoryTree;
// Check for specific category data in cache
const cachedData = getCachedCategoryData(categoryId);
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);
// If we have the category tree data, use it and don't query socket.io
if (hasCategoryTree) {
console.log("Found category tree in cache");
if (cachedData) {
console.log("Using cached category data for", categoryId);
@@ -321,19 +288,15 @@ class Content extends Component {
return;
}
}
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}`);
console.log(`productList:${categoryId}`);
window.socketManager.off(`productList:${categoryId}`);
// Track if we've received the full response to ignore stub response if needed
let receivedFullResponse = false;
console.log(`⚠️ Setting up listener for productList:${categoryId}`);
window.socketManager.on(`productList:${categoryId}`,(response) => {
console.log(`⚠️ RECEIVED productList:${categoryId} FULL RESPONSE`);
console.log("getCategoryProducts full response", response);
receivedFullResponse = true;
setCachedCategoryData(categoryId, response);
@@ -344,10 +307,8 @@ class Content extends Component {
}
});
console.log(`⚠️ EMITTING getCategoryProducts for ${categoryId}`);
window.socketManager.emit("getCategoryProducts", { categoryId: categoryId },
(response) => {
console.log(`⚠️ RECEIVED getCategoryProducts STUB RESPONSE for ${categoryId}`);
console.log("getCategoryProducts stub response", response);
// Only process stub response if we haven't received the full response yet
if (!receivedFullResponse) {
@@ -456,12 +417,10 @@ class Content extends Component {
return null;
}
const categoryTree = window.productCache["categoryTree_209"].categoryTree;
// If categoryId is a string (SEO name), find by seoName, otherwise by ID
const category = typeof categoryId === 'string'
? this.findCategoryBySeoName(categoryTree, categoryId)
: this.findCategoryById(categoryTree, categoryId);
? this.findCategoryBySeoName(window.productCache["categoryTree_209"].categoryTree, categoryId)
: this.findCategoryById(window.productCache["categoryTree_209"].categoryTree, categoryId);
return category ? category.name : null;
}