refactor: enhance category data fetching logic in SharedCarousel and CategoryList components by simplifying cache checks and improving logging for better maintainability

This commit is contained in:
sebseb7
2025-07-23 10:13:41 +02:00
parent a2b7a2509f
commit e472e6bb77
3 changed files with 69 additions and 138 deletions

View File

@@ -39,24 +39,12 @@ const getProductCache = () => {
// Initialize categories
const initializeCategories = (language = 'en') => {
const productCache = getProductCache();
if (productCache && productCache[`categoryTree_209_${language}`]) {
const cached = productCache[`categoryTree_209_${language}`];
if (cached.categoryTree) {
return processCategoryTree(cached.categoryTree);
}
}
// Only fallback to old cache format if we're looking for German (default language)
// This prevents showing German categories when user wants English categories
if (language === 'de' && productCache && productCache["categoryTree_209"]) {
const cached = productCache["categoryTree_209"];
if (cached.categoryTree) {
return processCategoryTree(cached.categoryTree);
}
}
// Try language-specific cache first, then fallback to default
const cached = productCache?.[`categoryTree_209_${language}`]?.categoryTree ||
(language === 'de' && productCache?.["categoryTree_209"]?.categoryTree);
return [];
return cached ? processCategoryTree(cached) : [];
};
const SharedCarousel = () => {
@@ -92,46 +80,49 @@ const SharedCarousel = () => {
}, [i18n]);
useEffect(() => {
if (
rootCategories.length === 0 &&
typeof window !== "undefined"
) {
console.log("SharedCarousel: Fetching categories by ignoring",window.productCache);
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) {
// Only fetch if we don't have categories
if (rootCategories.length === 0 && typeof window !== "undefined") {
const productCache = getProductCache();
const hasCache = productCache?.[`categoryTree_209_${currentLanguage}`]?.categoryTree ||
(currentLanguage === 'de' && productCache?.["categoryTree_209"]?.categoryTree);
if (!hasCache) {
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
// Store in cache with language-specific key
try {
if (!window.productCache) window.productCache = {};
window.productCache[`categoryTree_209_${currentLanguage}`] = {
categoryTree: categoryTreeToUse,
categoryTree: response.categoryTree,
timestamp: Date.now(),
};
} catch (err) {
console.error(err);
}
const newCategories = categoryTreeToUse.children || [];
const newCategories = response.categoryTree.children || [];
setRootCategories(newCategories);
}
} else if (response && response.categoryTree) {
// Fallback for old response format
// 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 || [];
setRootCategories(newCategories);
}
});
});
}
}
}, [rootCategories.length, currentLanguage]);