refactor: simplify cache management in SharedCarousel and CategoryList components to enhance data fetching efficiency and maintainability

This commit is contained in:
sebseb7
2025-07-23 10:15:49 +02:00
parent e472e6bb77
commit 146daf8eb1
2 changed files with 27 additions and 31 deletions

View File

@@ -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) {