From a376b5cd8a92ecdda4aa5a43f8c3d93b426adae7 Mon Sep 17 00:00:00 2001 From: Jonas Hinterdorfer Date: Mon, 16 Jun 2025 09:48:59 +0200 Subject: [PATCH] Implemte behavior wenn options null --- src/components/ProductDetails.tsx | 52 ++++++++++++++------------- src/components/ProductList.tsx | 58 +++++++++++++++++++++++-------- src/data/products.json | 15 ++------ src/types/types.ts | 2 +- 4 files changed, 75 insertions(+), 52 deletions(-) diff --git a/src/components/ProductDetails.tsx b/src/components/ProductDetails.tsx index d534553..9fc618c 100644 --- a/src/components/ProductDetails.tsx +++ b/src/components/ProductDetails.tsx @@ -10,13 +10,17 @@ type ProductDetailsProps = { }; const ProductDetails: React.FC = ({ product, category }) => { - const [selectedOption, setSelectedOption] = useState(product.options[0]); + const [selectedOption, setSelectedOption] = useState( + product.options?.[0] || { name: product.name, price: 0 } + ); const [quantity, setQuantity] = useState(1); const { addToCart } = useCart(); const navigate = useNavigate(); - // Check if all options have the same price - const hasSamePrice = product.options.every(option => option.price === product.options[0].price); + // 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({ @@ -30,29 +34,29 @@ const ProductDetails: React.FC = ({ product, category }) => }; return ( -
+

{product.name}

-
-

Optionen:

- {product.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: {product.options[0].price.toFixed(2)} €
- )} -
+ {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:

diff --git a/src/components/ProductList.tsx b/src/components/ProductList.tsx index af8ccb0..9edd6bf 100644 --- a/src/components/ProductList.tsx +++ b/src/components/ProductList.tsx @@ -1,6 +1,7 @@ import React from 'react'; import { Product } from '../types/types'; import { Link } from 'react-router-dom'; +import { useCart } from '../contexts/CartContext'; import './ProductList.css'; type ProductListProps = { @@ -9,26 +10,55 @@ type ProductListProps = { }; const ProductList: React.FC = ({ products, category }) => { - // Sort products by name alphabetically - const sortedProducts = [...products].sort((a, b) => + const { addToCart } = useCart(); + const sortedProducts = [...products].sort((a, b) => a.name.localeCompare(b.name) ); - + + const handleDirectAddToCart = (product: Product) => { + addToCart({ + productId: product.id, + productName: product.name, + selectedOption: { name: product.name, price: product.basePrice }, + quantity: 1, + category + }); + }; + return (
- {sortedProducts.map(product => ( - -
{product.name}
-
{product.basePrice.toFixed(2)} €
- - ))} + {sortedProducts.map(product => { + const hasOptions = product.options && product.options.length > 0; + const style = product.color ? { backgroundColor: `${product.color}80` } : {}; + if (hasOptions) { + return ( + +
{product.name}
+
{product.basePrice.toFixed(2)} €
+ + ); + } else { + return ( +
handleDirectAddToCart(product)} + > +
{product.name}
+
{product.basePrice.toFixed(2)} €
+
+ ); + } + })}
); }; + export default ProductList; diff --git a/src/data/products.json b/src/data/products.json index a92ed23..1691ae5 100644 --- a/src/data/products.json +++ b/src/data/products.json @@ -15,12 +15,7 @@ "id": "f2", "name": "Schnitzelsemmel", "basePrice": 5.80, - "color": "#FFEFC1", - "options": [ - {"name": "Klassisch", "price": 5.80}, - {"name": "Mit Salat", "price": 6.50, "color": "#E8F5E9"}, - {"name": "Mit Käse", "price": 6.80, "color": "#FFF9C4"} - ] + "color": "#FFEFC1" }, { "id": "f3", @@ -95,13 +90,7 @@ "id": "d3", "name": "Softdrinks", "basePrice": 3.50, - "color": "#FFDAB9", - "options": [ - {"name": "Fanta (0,5L)", "price": 3.50}, - {"name": "Sprite (0,5L)", "price": 3.50}, - {"name": "Almdudler (0,5L)", "price": 3.50}, - {"name": "Eistee (0,5L)", "price": 3.50} - ] + "color": "#FFDAB9" }, { "id": "d4", diff --git a/src/types/types.ts b/src/types/types.ts index bc543ab..288927c 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -8,7 +8,7 @@ export type Product = { id: string; name: string; basePrice: number; - options: ProductOption[]; + options?: ProductOption[]; color?: string; // Optional color code for background highlight };