"use client"; import { useState } from "react"; import { useAuth } from "@/context/AuthContext"; import { useCart } from "@/context/CartContext"; const fieldRowCls = "flex items-center px-5 py-[0.85rem] border-b border-[#eee] last:border-b-0"; const fieldLabelCls = "w-[140px] text-[0.9rem] font-semibold text-[#888] uppercase tracking-[0.04em] shrink-0"; const fieldValueCls = "text-base text-[#333]"; const qtyBtnCls = "w-7 h-7 border border-[#ddd] rounded-md bg-white text-base cursor-pointer flex items-center justify-center transition-colors hover:border-[#e68672] disabled:opacity-50"; //Full detail view for a single product, shown on the product detail page export default function ProductProfile({ prodId, prodName, categoryName, prodDesc, prodPrice, imageUrl }) { const { user } = useAuth(); const { addItem, selectedStoreId } = useCart(); const [quantity, setQuantity] = useState(1); const [adding, setAdding] = useState(false); const [feedback, setFeedback] = useState(null); //Increments or decrements quantity, minimum of 1 function changeQty(delta) { setQuantity((q) => Math.max(1, q + delta)); } //Adds the chosen quantity to the cart and shows a success or error message async function handleAddToCart() { setAdding(true); setFeedback(null); try { await addItem(prodId, quantity); setFeedback({ type: "success", message: `${quantity} × ${prodName} added to cart!` }); setQuantity(1); setTimeout(() => setFeedback(null), 1000); } catch (err) { setFeedback({ type: "error", message: err.message ?? "Failed to add to cart." }); } finally { setAdding(false); } } return (
{prodName} { e.currentTarget.onerror = null; e.currentTarget.src = "/images/pet-placeholder.png"; }} />

{prodName}

Category {categoryName ?? "-"}
Price {prodPrice != null ? `$${parseFloat(prodPrice).toFixed(2)}` : "-"}
Description {prodDesc ?? "-"}
{!user ? (

Log in to purchase this item.

) : !selectedStoreId ? (

Select a store from the navigation bar to add items to your cart.

) : ( <>
Quantity
{quantity}
{feedback?.type === "error" && (

{feedback.message}

)} )}
); }