From 745aa549e2906113fa13442d25967370f4f54a5f Mon Sep 17 00:00:00 2001 From: FlorianSpeicher Date: Wed, 21 May 2025 23:55:40 +0200 Subject: [PATCH] Added Product not found --- 01-frontend/src/pages/Product.tsx | 43 ++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/01-frontend/src/pages/Product.tsx b/01-frontend/src/pages/Product.tsx index f714d32..c6caebf 100644 --- a/01-frontend/src/pages/Product.tsx +++ b/01-frontend/src/pages/Product.tsx @@ -15,18 +15,43 @@ import { } from '@mui/material'; import { useState } from 'react'; import Item from '../components/Item'; -import { useLocation } from 'react-router-dom'; +import { useLocation, useNavigate } from 'react-router-dom'; import { useBasket } from '../helper/BasketProvider'; export default function Product() { const [quantity, setQuantity] = useState(1); + const [imageDimensions, setImageDimensions] = useState({ width: 0, height: 0 }); const location = useLocation(); const item = location.state?.item as Item; const { addToBasket } = useBasket(); + const navigate = useNavigate(); + const handleGoHome = () => { + navigate("/"); + }; + + // Redirect immediately if item is not found if (!item) { - return
Item not found
; + return ( + + + Product not found + + + It seems you are locking for a product that doesn't exist or has been removed. + + + + ); } @@ -37,6 +62,11 @@ export default function Product() { const discountedPrice = item.price * (1 - item.discount / 100); + const handleImageLoad = (event: React.SyntheticEvent) => { + const { naturalWidth, naturalHeight } = event.currentTarget; + setImageDimensions({ width: naturalWidth, height: naturalHeight }); + }; + return (
@@ -48,7 +78,14 @@ export default function Product() { component="img" src="/src/assets/HTW.jpg" alt={item.name} - sx={{ width: '100%', height: 'auto' }} + onLoad={handleImageLoad} // Event-Handler zum Ermitteln der Bildgröße + sx={{ + maxWidth: imageDimensions.width > imageDimensions.height ? "100%" : "auto", + maxHeight: imageDimensions.height >= imageDimensions.width ? 400 : "auto", + width: "auto", + height: "auto", + objectFit: "contain", + }} />