refactor: unify category data fetching across components to support language context and improve state management

This commit is contained in:
sebseb7
2025-07-31 08:26:10 +02:00
parent 9b38ed6f2a
commit 42fa46f2f9
5 changed files with 103 additions and 47 deletions

View File

@@ -26,36 +26,35 @@ class SharedCarousel extends React.Component {
constructor(props) {
super(props);
const { i18n } = props;
const categories = window.categoryService.getSync(209);
// Don't load categories in constructor - will be loaded in componentDidMount with correct language
this.state = {
categories: categories && categories.children && categories.children.length > 0 ? [...categories.children, ...categories.children] : [],
categories: [],
currentLanguage: (i18n && i18n.language) || 'de',
showScrollbar: false,
};
if(categories && categories.children && categories.children.length > 0) {
this.originalCategories = categories.children;
this.categories = [...categories.children, ...categories.children];
this.startAutoScroll();
}
this.carouselTrackRef = React.createRef();
}
componentDidMount() {
this._isMounted = true;
if (!this.state.categories || this.state.categories.length === 0) {
window.categoryService.get(209,this.props.languageContext?.currentLanguage || this.props.i18n.language).then((response) => {
if (this._isMounted && response.children && response.children.length > 0) {
this.originalCategories = response.children;
// Duplicate for seamless looping
this.categories = [...response.children, ...response.children];
this.setState({ categories: this.categories });
this.startAutoScroll();
}
});
}
const currentLanguage = this.props.languageContext?.currentLanguage || this.props.i18n.language;
// ALWAYS reload categories to ensure correct language
console.log("SharedCarousel componentDidMount: ALWAYS RELOADING categories for language", currentLanguage);
window.categoryService.get(209, currentLanguage).then((response) => {
console.log("SharedCarousel categoryService.get response for language '" + currentLanguage + "':", response);
if (this._isMounted && response.children && response.children.length > 0) {
console.log("SharedCarousel: Setting categories with", response.children.length, "items");
console.log("SharedCarousel: First category name:", response.children[0]?.name);
this.originalCategories = response.children;
// Duplicate for seamless looping
this.categories = [...response.children, ...response.children];
this.setState({ categories: this.categories });
this.startAutoScroll();
}
});
}
componentDidUpdate(prevProps) {