refactor: simplify category data fetching logic in Content component by removing redundant cache checks and improving clarity in data handling
This commit is contained in:
@@ -259,37 +259,12 @@ class Content extends Component {
|
|||||||
|
|
||||||
|
|
||||||
fetchCategoryData(categoryId) {
|
fetchCategoryData(categoryId) {
|
||||||
// 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);
|
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);
|
|
||||||
this.processDataWithCategoryTree(cachedData, categoryId);
|
this.processDataWithCategoryTree(cachedData, categoryId);
|
||||||
return;
|
return;
|
||||||
} else {
|
|
||||||
// If we have the category tree but not the specific category data,
|
|
||||||
// we can still use the category tree to get child categories
|
|
||||||
const emptyResponse = {
|
|
||||||
products: [],
|
|
||||||
attributes: [],
|
|
||||||
categoryName: this.getCategoryNameFromTree(categoryId)
|
|
||||||
};
|
|
||||||
console.log("Using category tree data without products");
|
|
||||||
this.processDataWithCategoryTree(emptyResponse, categoryId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only if we don't have the category tree in cache, proceed with socket.io query
|
|
||||||
console.log(`productList:${categoryId}`);
|
console.log(`productList:${categoryId}`);
|
||||||
window.socketManager.off(`productList:${categoryId}`);
|
window.socketManager.off(`productList:${categoryId}`);
|
||||||
|
|
||||||
@@ -410,21 +385,6 @@ class Content extends Component {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to get category name from tree
|
|
||||||
getCategoryNameFromTree = (categoryId) => {
|
|
||||||
if (!window.productCache || !window.productCache["categoryTree_209"] ||
|
|
||||||
!window.productCache["categoryTree_209"].categoryTree) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If categoryId is a string (SEO name), find by seoName, otherwise by ID
|
|
||||||
const category = typeof categoryId === 'string'
|
|
||||||
? this.findCategoryBySeoName(window.productCache["categoryTree_209"].categoryTree, categoryId)
|
|
||||||
: this.findCategoryById(window.productCache["categoryTree_209"].categoryTree, categoryId);
|
|
||||||
|
|
||||||
return category ? category.name : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function to get current category ID from seoName
|
// Helper function to get current category ID from seoName
|
||||||
getCurrentCategoryId = () => {
|
getCurrentCategoryId = () => {
|
||||||
const seoName = this.props.params.categoryId;
|
const seoName = this.props.params.categoryId;
|
||||||
|
|||||||
Reference in New Issue
Block a user