/* * Card component for showing a product in the store grid. * * Author: Shiv * Date: April 2026 */ "use client"; import Link from "next/link"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { useAuth } from "@/context/AuthContext"; import { useCart } from "@/context/CartContext"; //Shared CSS class for the quantity plus and minus buttons 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"; //Card shown in the products grid, includes quantity selector and add to cart button export default function ProductCard({ prodId, prodName, categoryName, prodPrice, imageUrl }) { const { user } = useAuth(); const { addItem, selectedStoreId } = useCart(); const router = useRouter(); const [quantity, setQuantity] = useState(1); const [adding, setAdding] = useState(false); const [feedback, setFeedback] = useState(null); //Adds the selected quantity to the cart, redirects to login if not logged in async function handleAddToCart(e) { e.preventDefault(); if (!user) { router.push("/login"); return; } if (!selectedStoreId) { setFeedback("Please select a store first"); setTimeout(() => setFeedback(null), 2500); return; } setAdding(true); setFeedback(null); try { await addItem(prodId, quantity); setFeedback("Added!"); setTimeout(() => setFeedback(null), 1500); } catch (err) { setFeedback(err.message || "Failed to add"); setTimeout(() => setFeedback(null), 2500); } finally { setAdding(false); } } return (
{categoryName}
{prodPrice != null && ( ${parseFloat(prodPrice).toFixed(2)} )}{feedback}
}