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

View File

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