import React, { useState } from 'react'; import { Product, ProductOption } from '../types/types'; import { useCart } from '../contexts/CartContext'; import { useNavigate } from 'react-router-dom'; import './ProductDetails.css'; type ProductDetailsProps = { product: Product; category: 'food' | 'drinks'; }; const ProductDetails: React.FC = ({ product, category }) => { const [selectedOption, setSelectedOption] = useState( product.options?.[0] || { name: product.name, price: 0 } ); const [quantity, setQuantity] = useState(1); const { addToCart } = useCart(); const navigate = useNavigate(); // Use a safer approach for options const options = product.options || []; const hasSamePrice = options.length > 0 && options.every(option => option.price === options[0].price); const handleAddToCart = () => { addToCart({ productId: product.id, productName: product.name, selectedOption, quantity, category }); navigate('/'); }; return (

{product.name}

{options.length > 0 && (

Optionen:

{options.map(option => (
setSelectedOption(option)} style={(option.color || product.color) ? { backgroundColor: `${option.color || product.color}60` } : {}} > {option.name} {!hasSamePrice && {option.price.toFixed(2)} €}
))} {hasSamePrice && (
Preis: {options[0].price.toFixed(2)} €
)}
)}

Menge:

{quantity}
Gesamt: {(selectedOption.price * quantity).toFixed(2)} €
); }; export default ProductDetails;