"use client"; import { useState } from "react"; import Image from "next/image"; import { motion } from "framer-motion"; import type { Product } from "@/types/saleor"; import { useSaleorCheckoutStore } from "@/stores/saleorCheckoutStore"; import { getProductPrice, getProductImage, getLocalizedProduct } from "@/lib/saleor"; import ProductCard from "@/components/product/ProductCard"; interface ProductDetailProps { product: Product; relatedProducts: Product[]; locale?: string; } export default function ProductDetail({ product, relatedProducts, locale = "SR" }: ProductDetailProps) { const [selectedImage, setSelectedImage] = useState(0); const [quantity, setQuantity] = useState(1); const [isAdding, setIsAdding] = useState(false); const { addLine, openCart } = useSaleorCheckoutStore(); const localized = getLocalizedProduct(product, locale); const variant = product.variants?.[0]; const images = product.media?.length > 0 ? product.media : [{ id: "0", url: "/placeholder-product.jpg", alt: localized.name, type: "IMAGE" }]; const handleAddToCart = async () => { if (!variant?.id) return; setIsAdding(true); try { await addLine(variant.id, quantity); openCart(); } finally { setIsAdding(false); } }; const stripHtml = (html: string) => { if (!html) return ""; return html.replace(/<[^>]*>/g, ""); }; const isAvailable = variant?.quantityAvailable > 0; const price = getProductPrice(product); return ( <> {/* Product Images */} {images[selectedImage] && ( )} {images.length > 1 && ( {images.map((image, index) => ( setSelectedImage(index)} className={`relative w-20 h-20 flex-shrink-0 ${ selectedImage === index ? "ring-2 ring-foreground" : "" }`} > ))} )} {/* Product Info */} {localized.name} {price || (locale === "EN" ? "Contact for price" : "Kontaktirajte za cenu")} {/* Short Description */} {stripHtml(localized.description).slice(0, 200)}... {/* Add to Cart */} {isAvailable ? ( {/* Quantity Selector */} setQuantity(Math.max(1, quantity - 1))} className="px-4 py-3 hover:bg-gray-50" > - {quantity} setQuantity(quantity + 1)} className="px-4 py-3 hover:bg-gray-50" > + {/* Add to Cart Button */} {isAdding ? (locale === "EN" ? "Adding..." : "Dodavanje...") : (locale === "EN" ? "Add to Cart" : "Dodaj u korpu") } ) : ( {locale === "EN" ? "Out of Stock" : "Nema na stanju"} )} {/* SKU */} {variant?.sku && ( SKU: {variant.sku} )} {/* Full Description */} {localized.description && ( {locale === "EN" ? "Description" : "Opis"} )} {/* Related Products */} {relatedProducts.length > 0 && ( {locale === "EN" ? "You May Also Like" : "Možda će vam se svideti"} {relatedProducts.map((relatedProduct, index) => ( ))} )} > ); }
{price || (locale === "EN" ? "Contact for price" : "Kontaktirajte za cenu")}
{stripHtml(localized.description).slice(0, 200)}...
SKU: {variant.sku}