import { useState } from "react";
import { useParams, useLocation } from "wouter";
import { useQuery } from "@tanstack/react-query";
import { useAuth } from "@/hooks/use-auth";
import { useCart } from "@/hooks/use-cart";
import { productsApi } from "@/lib/api";
import Header from "@/components/layout/header";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Separator } from "@/components/ui/separator";
import { Skeleton } from "@/components/ui/skeleton";
import { Card, CardContent } from "@/components/ui/card";
import { Star, Store, ShoppingCart, Plus, Minus, ArrowLeft } from "lucide-react";
import { useToast } from "@/hooks/use-toast";
export default function ProductDetail() {
const { id } = useParams();
const [, setLocation] = useLocation();
const { user } = useAuth();
const { addItem } = useCart();
const { toast } = useToast();
const [selectedImageIndex, setSelectedImageIndex] = useState(0);
const [quantity, setQuantity] = useState(1);
const [searchQuery, setSearchQuery] = useState("");
const { data: product, isLoading, error } = useQuery({
queryKey: ['/api/products', id],
queryFn: () => productsApi.getById(id!),
enabled: !!id,
});
if (isLoading) {
return (
{[...Array(4)].map((_, i) => (
))}
);
}
if (error || !product) {
return (
Product Not Found
);
}
const handleAddToCart = async () => {
if (!user) {
toast({
variant: "destructive",
title: "Sign in required",
description: "Please sign in to add items to your cart.",
});
setLocation('/auth');
return;
}
try {
await addItem(product.id, quantity);
toast({
title: "Added to cart",
description: `${quantity} ${quantity === 1 ? 'item' : 'items'} added to your cart.`,
});
} catch (error) {
toast({
variant: "destructive",
title: "Error",
description: "Failed to add item to cart.",
});
}
};
const handleBuyNow = async () => {
if (!user) {
toast({
variant: "destructive",
title: "Sign in required",
description: "Please sign in to make a purchase.",
});
setLocation('/auth');
return;
}
await handleAddToCart();
setLocation('/checkout');
};
const discountPercentage = product.originalPrice
? Math.round((1 - parseFloat(product.price) / parseFloat(product.originalPrice)) * 100)
: 0;
const images = product.images && product.images.length > 0 ? product.images : ['/api/placeholder/400/400'];
return (
{/* Product Images */}
{/* Main Image */}
{/* Thumbnail Images */}
{images.map((image: string, index: number) => (
))}
{/* Product Info */}
{/* Seller Info */}
{product.store && (
{product.store.name}
{[...Array(5)].map((_, i) => (
))}
(4.9/5)
)}
{product.title}
{/* Rating and Reviews */}
{[...Array(5)].map((_, i) => (
))}
4.9
(124 reviews)
{/* Price */}
₹{parseFloat(product.price).toFixed(2)}
{product.originalPrice && (
₹{parseFloat(product.originalPrice).toFixed(2)}
)}
{discountPercentage > 0 && (
{discountPercentage}% OFF
)}
{/* Quantity Selector */}
{quantity}
{/* Stock Info */}
{product.stock > 0 ? (
{product.stock} in stock
) : (
Out of stock
)}
{/* Action Buttons */}
{/* Product Description */}
Product Description
{product.description}
{/* Category */}
{product.category && (
Category
{product.category.name}
)}
);
}