import React, { useState } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import {
Box,
Typography,
Grid,
Card,
CardMedia,
Button,
Chip,
Divider,
TextField,
IconButton,
CircularProgress,
Paper,
Table,
TableBody,
TableCell,
TableContainer,
TableRow,
Breadcrumbs,
Link,
useTheme
} from '@mui/material';
import ShoppingCartIcon from '@mui/icons-material/ShoppingCart';
import AddIcon from '@mui/icons-material/Add';
import RemoveIcon from '@mui/icons-material/Remove';
import NavigateNextIcon from '@mui/icons-material/NavigateNext';
import { Rating } from '@mui/material';
import ProductReviews from '@components/ProductReviews';
import { Link as RouterLink } from 'react-router-dom';
import { useProduct, useAddToCart } from '@hooks/apiHooks';
import { useAuth } from '@hooks/reduxHooks';
import imageUtils from '@utils/imageUtils';
import SEOMetaTags from '@components/SEOMetaTags';
import useProductSeo from '@hooks/useProductSeo';
const ProductDetailPage = () => {
const { id } = useParams();
const navigate = useNavigate();
const theme = useTheme();
const { isAuthenticated, user } = useAuth();
const [quantity, setQuantity] = useState(1);
const [selectedImage, setSelectedImage] = useState(0);
// Fetch product data
const { data: products, isLoading, error } = useProduct(id);
const addToCart = useAddToCart();
let product = products?.length > 0 ? products[0] : null
// Get SEO metadata for the product
const {
title,
description,
keywords,
image,
canonical,
structuredData
} = useProductSeo(product);
// Handle quantity changes
const increaseQuantity = () => {
if (product && quantity < product.stock_quantity) {
setQuantity(quantity + 1);
}
};
const decreaseQuantity = () => {
if (quantity > 1) {
setQuantity(quantity - 1);
}
};
const handleQuantityChange = (e) => {
const value = parseInt(e.target.value, 10);
if (isNaN(value) || value < 1) {
setQuantity(1);
} else if (product && value > product.stock_quantity) {
setQuantity(product.stock_quantity);
} else {
setQuantity(value);
}
};
// Handle add to cart
const handleAddToCart = () => {
if (!isAuthenticated) {
navigate('/auth/login', { state: { from: `/products/${id}` } });
return;
}
addToCart.mutate({
userId: user,
productId: id,
quantity
});
};
// Format properties for display
const getProductProperties = () => {
if (!product) return [];
const properties = [];
if (product.category_name) {
properties.push({ name: 'Category', value: product.category_name });
}
if (product.material_type) {
properties.push({ name: 'Material', value: product.material_type });
}
if (product.color) {
properties.push({ name: 'Color', value: product.color });
}
if (product.origin) {
properties.push({ name: 'Origin', value: product.origin });
}
if (product.weight_grams) {
properties.push({ name: 'Weight', value: `${product.weight_grams}g` });
}
if (product.length_cm) {
properties.push({ name: 'Length', value: `${product.length_cm}cm` });
}
if (product.width_cm) {
properties.push({ name: 'Width', value: `${product.width_cm}cm` });
}
if (product.height_cm) {
properties.push({ name: 'Height', value: `${product.height_cm}cm` });
}
if (product.age) {
properties.push({ name: 'Age', value: product.age });
}
return properties;
};
if (isLoading) {
return (
);
}
if (error || !product) {
return (
Error loading product details
);
}
return (
<>
{/* SEO Meta Tags */}
{/* Breadcrumbs navigation */}
}
aria-label="breadcrumb"
sx={{ mb: 3 }}
>
Home
Products
{product.category_name}
{product.name}
{/* Product Images */}
0
?
imageUtils.getImageUrl(product.images[selectedImage]?.path) : "https://placehold.co/600x400/000000/FFFF"
}
alt={product.name}
sx={{
height: 400,
objectFit: 'contain',
bgcolor: 'background.paper'
}}
/>
{/* Thumbnail images */}
{product.images && product.images.length > 1 && (
{product.images.map((image, index) => (
setSelectedImage(index)}
sx={{
width: 80,
height: 80,
cursor: 'pointer',
border: index === selectedImage ? `2px solid ${theme.palette.primary.main}` : '2px solid transparent',
borderRadius: 1,
overflow: 'hidden',
}}
>
))}
)}
{/* Product Details */}
{product.name}
{product.average_rating && (
{product.average_rating} ({product.review_count} {product.review_count === 1 ? 'review' : 'reviews'})
)}
{!product.average_rating && (
No reviews yet
)}
${parseFloat(product.price).toFixed(2)}
{/* Tags */}
{product.tags && product.tags.map((tag, index) => (
))}
{product.description}
{/* Stock information */}
Availability:
0 ? 'In Stock' : 'Out of Stock'}
color={product.stock_quantity > 0 ? 'success' : 'error'}
size="small"
sx={{ ml: 1 }}
/>
{product.stock_quantity > 0 && (
{product.stock_quantity} items available
)}
{/* Add to cart section */}
{product.stock_quantity > 0 ? (
= product.stock_quantity}
size="small"
>
}
onClick={handleAddToCart}
disabled={addToCart.isLoading}
sx={{ flexGrow: 1 }}
>
{addToCart.isLoading ? 'Adding...' : 'Add to Cart'}
) : (
)}
{/* Product properties table */}
Product Details
{getProductProperties().map((prop) => (
{prop.name}
{prop.value}
))}
{/* Reviews Section */}
>
);
};
export default ProductDetailPage;