feat(Context): integrate Product and Category context providers into App
- Wrapped AppContent with ProductContextProvider and CategoryContextProvider to manage product and category states. - Added TitleUpdater component for dynamic title management. - Enhanced Content and ProductDetailPage components to utilize the new context for setting and clearing current product and category states. - Updated ProductDetailWithSocket to pass setCurrentProduct function from context.
This commit is contained in:
@@ -201,6 +201,17 @@ class ProductDetailPage extends Component {
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
// Update context with cached product if available
|
||||
if (this.state.product && this.props.setCurrentProduct) {
|
||||
console.log('ProductDetailPage: Setting product context from cache', this.state.product.name);
|
||||
this.props.setCurrentProduct({
|
||||
name: this.state.product.name,
|
||||
categoryId: this.state.product.kategorien ? this.state.product.kategorien.split(',')[0] : undefined
|
||||
});
|
||||
} else if (this.state.product) {
|
||||
console.warn('ProductDetailPage: setCurrentProduct prop is missing despite having product');
|
||||
}
|
||||
|
||||
// Load product data if we have no product or if we're in upgrading state
|
||||
if (!this.state.product || this.state.upgrading) {
|
||||
this.loadProductData();
|
||||
@@ -221,6 +232,11 @@ class ProductDetailPage extends Component {
|
||||
componentDidUpdate(prevProps) {
|
||||
// Check for seoName changes
|
||||
if (prevProps.seoName !== this.props.seoName) {
|
||||
// Clear context when navigating to new product
|
||||
if (this.props.setCurrentProduct) {
|
||||
this.props.setCurrentProduct(null);
|
||||
}
|
||||
|
||||
this.setState(
|
||||
{ product: null, loading: true, upgrading: false, error: null, imageDialogOpen: false, similarProducts: [] },
|
||||
this.loadProductData
|
||||
@@ -617,6 +633,17 @@ class ProductDetailPage extends Component {
|
||||
komponentenLoaded: komponenten.length === 0, // If no komponenten, mark as loaded
|
||||
similarProducts: res.similarProducts || []
|
||||
}, () => {
|
||||
// Update context
|
||||
if (this.props.setCurrentProduct) {
|
||||
console.log('ProductDetailPage: Setting product context from fetch', productData.name);
|
||||
this.props.setCurrentProduct({
|
||||
name: productData.name,
|
||||
categoryId: productData.kategorien ? productData.kategorien.split(',')[0] : undefined
|
||||
});
|
||||
} else {
|
||||
console.warn('ProductDetailPage: setCurrentProduct prop is missing after fetch');
|
||||
}
|
||||
|
||||
if(komponenten.length > 0) {
|
||||
for(const komponent of komponenten) {
|
||||
this.loadKomponent(komponent.id, komponent.count);
|
||||
@@ -1034,7 +1061,13 @@ class ProductDetailPage extends Component {
|
||||
});
|
||||
};
|
||||
|
||||
render() {
|
||||
componentWillUnmount() {
|
||||
if (this.props.setCurrentProduct) {
|
||||
this.props.setCurrentProduct(null);
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const { product, loading, upgrading, error, attributeImages, isSteckling, attributes, komponentenLoaded, komponentenData, komponentenImages, totalKomponentenPrice, totalSavings, shareAnchorEl, sharePopperOpen, snackbarOpen, snackbarMessage, snackbarSeverity } =
|
||||
this.state;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user