refactor: simplify cache management in SharedCarousel and CategoryList components to enhance data fetching efficiency and maintainability
This commit is contained in:
@@ -170,24 +170,21 @@ class CategoryList extends Component {
|
||||
const windowObj = (typeof global !== "undefined" && global.window) ||
|
||||
(typeof window !== "undefined" && window);
|
||||
|
||||
if (windowObj && !windowObj.productCache) {
|
||||
windowObj.productCache = {};
|
||||
}
|
||||
|
||||
const cacheKey = `categoryTree_209_${currentLanguage}`;
|
||||
const cachedData = windowObj?.productCache?.[cacheKey];
|
||||
// Ensure cache exists
|
||||
windowObj.productCache = windowObj.productCache || {};
|
||||
|
||||
// Use cache if available
|
||||
if (cachedData?.categoryTree) {
|
||||
this.processCategoryTree(cachedData.categoryTree);
|
||||
// Get cache key
|
||||
const cacheKey = `categoryTree_209_${currentLanguage}`;
|
||||
|
||||
// USE THE CACHE directly - if it has data, use it
|
||||
const categoryTree = windowObj.productCache[cacheKey]?.categoryTree;
|
||||
if (categoryTree) {
|
||||
this.processCategoryTree(categoryTree);
|
||||
return;
|
||||
}
|
||||
|
||||
// Mark as being fetched
|
||||
if (windowObj?.productCache) {
|
||||
windowObj.productCache[cacheKey] = { fetching: true, timestamp: Date.now() };
|
||||
}
|
||||
|
||||
// No cache, so fetch
|
||||
windowObj.productCache[cacheKey] = { fetching: true, timestamp: Date.now() };
|
||||
this.setState({ fetchedCategories: true });
|
||||
window.socketManager.emit("categoryList", { categoryId: 209, language: currentLanguage, requestTranslation: true }, (response) => {
|
||||
if (response && response.success) {
|
||||
|
||||
Reference in New Issue
Block a user