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:
@@ -15,15 +15,6 @@ import { useParams, useSearchParams } from 'react-router-dom';
|
|||||||
import { getAllSettingsWithPrefix } from '../utils/sessionStorage.js';
|
import { getAllSettingsWithPrefix } from '../utils/sessionStorage.js';
|
||||||
import { withI18n } from '../i18n/withTranslation.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);
|
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
|
// @note SwashingtonCP font is now loaded globally via index.css
|
||||||
@@ -200,14 +191,6 @@ class Content extends Component {
|
|||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(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 = {
|
this.state = {
|
||||||
loaded: false,
|
loaded: false,
|
||||||
categoryName: null,
|
categoryName: null,
|
||||||
@@ -219,16 +202,6 @@ class Content extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
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: []}, () => {
|
if(this.props.params.categoryId) {this.setState({loaded: false, unfilteredProducts: [], filteredProducts: [], attributes: [], categoryName: null, childCategories: []}, () => {
|
||||||
this.fetchCategoryData(this.props.params.categoryId);
|
this.fetchCategoryData(this.props.params.categoryId);
|
||||||
})}
|
})}
|
||||||
@@ -286,24 +259,18 @@ class Content extends Component {
|
|||||||
|
|
||||||
|
|
||||||
fetchCategoryData(categoryId) {
|
fetchCategoryData(categoryId) {
|
||||||
// Simple direct check for the cache
|
// Check if we have the category tree data in window.productCache
|
||||||
console.log("fetchCategoryData - window.productCache exists:", !!window.productCache);
|
const hasCategoryTree = 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (window.productCache &&
|
|
||||||
window.productCache["categoryTree_209"] &&
|
window.productCache["categoryTree_209"] &&
|
||||||
window.productCache["categoryTree_209"].categoryTree) {
|
window.productCache["categoryTree_209"].categoryTree;
|
||||||
console.log("Found categoryTree_209.categoryTree in cache");
|
|
||||||
|
|
||||||
// Check for specific category data in cache
|
// Check for specific category data in cache
|
||||||
const cachedData = getCachedCategoryData(categoryId);
|
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) {
|
if (cachedData) {
|
||||||
console.log("Using cached category data for", categoryId);
|
console.log("Using cached category data for", categoryId);
|
||||||
this.processDataWithCategoryTree(cachedData, categoryId);
|
this.processDataWithCategoryTree(cachedData, categoryId);
|
||||||
@@ -322,18 +289,14 @@ class Content extends Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
// 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}`);
|
window.socketManager.off(`productList:${categoryId}`);
|
||||||
|
|
||||||
// Track if we've received the full response to ignore stub response if needed
|
// Track if we've received the full response to ignore stub response if needed
|
||||||
let receivedFullResponse = false;
|
let receivedFullResponse = false;
|
||||||
|
|
||||||
console.log(`⚠️ Setting up listener for productList:${categoryId}`);
|
|
||||||
window.socketManager.on(`productList:${categoryId}`,(response) => {
|
window.socketManager.on(`productList:${categoryId}`,(response) => {
|
||||||
console.log(`⚠️ RECEIVED productList:${categoryId} FULL RESPONSE`);
|
|
||||||
console.log("getCategoryProducts full response", response);
|
console.log("getCategoryProducts full response", response);
|
||||||
receivedFullResponse = true;
|
receivedFullResponse = true;
|
||||||
setCachedCategoryData(categoryId, response);
|
setCachedCategoryData(categoryId, response);
|
||||||
@@ -344,10 +307,8 @@ class Content extends Component {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`⚠️ EMITTING getCategoryProducts for ${categoryId}`);
|
|
||||||
window.socketManager.emit("getCategoryProducts", { categoryId: categoryId },
|
window.socketManager.emit("getCategoryProducts", { categoryId: categoryId },
|
||||||
(response) => {
|
(response) => {
|
||||||
console.log(`⚠️ RECEIVED getCategoryProducts STUB RESPONSE for ${categoryId}`);
|
|
||||||
console.log("getCategoryProducts stub response", response);
|
console.log("getCategoryProducts stub response", response);
|
||||||
// Only process stub response if we haven't received the full response yet
|
// Only process stub response if we haven't received the full response yet
|
||||||
if (!receivedFullResponse) {
|
if (!receivedFullResponse) {
|
||||||
@@ -456,12 +417,10 @@ class Content extends Component {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const categoryTree = window.productCache["categoryTree_209"].categoryTree;
|
|
||||||
|
|
||||||
// If categoryId is a string (SEO name), find by seoName, otherwise by ID
|
// If categoryId is a string (SEO name), find by seoName, otherwise by ID
|
||||||
const category = typeof categoryId === 'string'
|
const category = typeof categoryId === 'string'
|
||||||
? this.findCategoryBySeoName(categoryTree, categoryId)
|
? this.findCategoryBySeoName(window.productCache["categoryTree_209"].categoryTree, categoryId)
|
||||||
: this.findCategoryById(categoryTree, categoryId);
|
: this.findCategoryById(window.productCache["categoryTree_209"].categoryTree, categoryId);
|
||||||
|
|
||||||
return category ? category.name : null;
|
return category ? category.name : null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user