refactor: improve cache utilization and data fetching logic in SharedCarousel and CategoryList components for enhanced performance and maintainability

This commit is contained in:
sebseb7
2025-07-23 10:20:35 +02:00
parent 146daf8eb1
commit 226ca3e834
2 changed files with 39 additions and 40 deletions

View File

@@ -78,50 +78,48 @@ const SharedCarousel = () => {
}, [i18n]);
useEffect(() => {
// Only fetch if we don't have categories
// Check cache first
const cache = getProductCache();
const categoryTree = cache[`categoryTree_209_${currentLanguage}`]?.categoryTree ||
(currentLanguage === 'de' && cache["categoryTree_209"]?.categoryTree);
// Use cache if available
if (categoryTree) {
setRootCategories(processCategoryTree(categoryTree));
return;
}
// Only fetch if needed
if (rootCategories.length === 0 && typeof window !== "undefined") {
const cache = getProductCache();
const categoryTree = cache[`categoryTree_209_${currentLanguage}`]?.categoryTree ||
(currentLanguage === 'de' && cache["categoryTree_209"]?.categoryTree);
// If no cache, fetch
if (!categoryTree) {
window.socketManager.emit("categoryList", { categoryId: 209, language: currentLanguage, requestTranslation: true }, (response) => {
if (response && response.success) {
// Use translated data if available, otherwise fall back to original
const categoryTreeToUse = response.translation || response.categoryTree;
if (categoryTreeToUse) {
// Store in cache with language-specific key
try {
if (!window.productCache) window.productCache = {};
window.productCache[`categoryTree_209_${currentLanguage}`] = {
categoryTree: categoryTreeToUse,
timestamp: Date.now(),
};
} catch (err) {
console.error(err);
}
const newCategories = categoryTreeToUse.children || [];
setRootCategories(newCategories);
}
} else if (response && response.categoryTree) {
// Fallback for old response format
window.socketManager.emit("categoryList", { categoryId: 209, language: currentLanguage, requestTranslation: true }, (response) => {
if (response && response.success) {
// Use translated data if available, otherwise fall back to original
const categoryTreeToUse = response.translation || response.categoryTree;
if (categoryTreeToUse) {
// Store in cache with language-specific key
try {
if (!window.productCache) window.productCache = {};
window.productCache[`categoryTree_209_${currentLanguage}`] = {
categoryTree: response.categoryTree,
timestamp: Date.now(),
};
} catch (err) {
console.error(err);
}
const newCategories = response.categoryTree.children || [];
if (!window.productCache) window.productCache = {};
window.productCache[`categoryTree_209_${currentLanguage}`] = {
categoryTree: categoryTreeToUse,
timestamp: Date.now(),
};
const newCategories = categoryTreeToUse.children || [];
setRootCategories(newCategories);
}
});
}
} else if (response && response.categoryTree) {
// Fallback for old response format
// Store in cache with language-specific key
if (!window.productCache) window.productCache = {};
window.productCache[`categoryTree_209_${currentLanguage}`] = {
categoryTree: response.categoryTree,
timestamp: Date.now(),
};
const newCategories = response.categoryTree.children || [];
setRootCategories(newCategories);
}
});
}
}, [rootCategories.length, currentLanguage]);

View File

@@ -180,6 +180,7 @@ class CategoryList extends Component {
const categoryTree = windowObj.productCache[cacheKey]?.categoryTree;
if (categoryTree) {
this.processCategoryTree(categoryTree);
this.setState({ fetchedCategories: true });
return;
}