import React, { Component } from 'react'; import Box from '@mui/material/Box'; import Card from '@mui/material/Card'; import CardContent from '@mui/material/CardContent'; import CardMedia from '@mui/material/CardMedia'; import Typography from '@mui/material/Typography'; import CircularProgress from '@mui/material/CircularProgress'; import IconButton from '@mui/material/IconButton'; import AddToCartButton from './AddToCartButton.js'; import { Link } from 'react-router-dom'; import { withI18n } from '../i18n/withTranslation.js'; import ZoomInIcon from '@mui/icons-material/ZoomIn'; class Product extends Component { constructor(props) { super(props); this._isMounted = false; if (!window.smallPicCache) { window.smallPicCache = {}; } if(this.props.pictureList && this.props.pictureList.length > 0 && this.props.pictureList.split(',').length > 0) { const bildId = this.props.pictureList.split(',')[0]; if(window.smallPicCache[bildId]){ this.state = {image:window.smallPicCache[bildId],loading:false, error: false} }else{ this.state = {image: null, loading: true, error: false}; this.loadImage(bildId); } }else{ this.state = {image: null, loading: false, error: false}; } } componentDidMount() { this._isMounted = true; } loadImage = (bildId) => { console.log('loadImagevisSocket', bildId); window.socketManager.emit('getPic', { bildId, size:'small' }, (res) => { if(res.success){ window.smallPicCache[bildId] = URL.createObjectURL(new Blob([res.imageBuffer], { type: 'image/jpeg' })); if (this._isMounted) { this.setState({image: window.smallPicCache[bildId], loading: false}); } else { this.state.image = window.smallPicCache[bildId]; this.state.loading = false; } }else{ console.log('Fehler beim Laden des Bildes:', res); if (this._isMounted) { this.setState({error: true, loading: false}); } else { this.state.error = true; this.state.loading = false; } } }); } componentWillUnmount() { this._isMounted = false; } handleQuantityChange = (quantity) => { console.log(`Product: ${this.props.name}, Quantity: ${quantity}`); // In a real app, this would update a cart state in a parent component or Redux store } render() { const { id, name, price, available, manufacturer, seoName, currency, vat, cGrundEinheit, fGrundPreis, thc, floweringWeeks,incoming, neu, weight, versandklasse, availableSupplier, komponenten } = this.props; const isNew = neu && (new Date().getTime() - new Date(neu).getTime() < 30 * 24 * 60 * 60 * 1000); const showThcBadge = thc > 0; let thcBadgeColor = '#4caf50'; // Green default if (thc > 30) { thcBadgeColor = '#f44336'; // Red for > 30 } else if (thc > 25) { thcBadgeColor = '#ffeb3b'; // Yellow for > 25 } const showFloweringWeeksBadge = floweringWeeks > 0; let floweringWeeksBadgeColor = '#4caf50'; // Green default if (floweringWeeks > 12) { floweringWeeksBadgeColor = '#f44336'; // Red for > 12 } else if (floweringWeeks > 8) { floweringWeeksBadgeColor = '#ffeb3b'; // Yellow for > 8 } return ( {isNew && (
{/* Background star - slightly larger and rotated */} {/* Middle star - medium size with different rotation */} {/* Foreground star - main star with text */} {/* Text as a separate element to position it at the top */}
{this.props.t ? this.props.t('product.new') : 'NEU'}
)} {showThcBadge && (
25 && thc <= 30 ? '#000000' : '#ffffff', fontWeight: 'bold', padding: '2px 0', width: '80px', textAlign: 'center', zIndex: 999, fontSize: '9px', boxShadow: '0px 2px 4px rgba(0,0,0,0.2)', transform: 'rotate(-45deg) translateX(-40px) translateY(15px)', transformOrigin: 'top left' }} > THC {thc}%
)} {showFloweringWeeksBadge && (
8 && floweringWeeks <= 12 ? '#000000' : '#ffffff', fontWeight: 'bold', padding: '1px 0', width: '100px', textAlign: 'center', zIndex: 999, fontSize: '9px', boxShadow: '0px 2px 4px rgba(0,0,0,0.2)', transform: 'rotate(-45deg) translateX(-50px) translateY(32px)', transformOrigin: 'top left' }} > {floweringWeeks} {this.props.t ? this.props.t('product.weeks') : 'Wochen'}
)} {this.state.loading ? ( ) : this.state.image === null ? ( { // Ensure alt text is always present even on error if (!e.target.alt) { e.target.alt = name || 'Produktbild'; } }} sx={{ objectFit: 'contain', borderTopLeftRadius: '8px', borderTopRightRadius: '8px', width: '100%' }} /> ) : ( { // Ensure alt text is always present even on error if (!e.target.alt) { e.target.alt = name || 'Produktbild'; } }} sx={{ objectFit: 'contain', borderTopLeftRadius: '8px', borderTopRightRadius: '8px', width: '100%' }} /> )} {name} {manufacturer || ''}
{new Intl.NumberFormat('de-DE', {style: 'currency', currency: currency || 'EUR'}).format(price)} ({this.props.t ? this.props.t('product.inclVatFooter', { vat }) : `incl. ${vat}% USt.,*`}) {cGrundEinheit && fGrundPreis && fGrundPreis != price && ( ({new Intl.NumberFormat('de-DE', {style: 'currency', currency: currency || 'EUR'}).format(fGrundPreis)}/{cGrundEinheit}) )}
{/*incoming*/}
); } } export default withI18n()(Product);