import { useEffect, useState } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { fetchCategories, fetchProducts, type Product } from '@/lib/api'; import ProductGallery from '@/components/ProductGallery'; const ProductsPage = () => { const [categories, setCategories] = useState(['All']); const [selectedCategory, setSelectedCategory] = useState('All'); const [products, setProducts] = useState([]); const [failed, setFailed] = useState(false); useEffect(() => { fetchCategories().then(setCategories).catch(() => setFailed(true)); }, []); // The filter is applied by the API, not in the browser — one source of truth for what's in a // category. `ignore` drops a slow response that lost the race to a newer click. useEffect(() => { let ignore = false; setFailed(false); fetchProducts(selectedCategory) .then((p) => { if (!ignore) setProducts(p); }) .catch(() => { if (!ignore) setFailed(true); }); return () => { ignore = true; }; }, [selectedCategory]); return (
{/* Page header */}

Our products

{/* Products Section */}
{/* Categories */}
{categories.map((category) => ( setSelectedCategory(category)} className={`px-6 py-2 rounded-full border text-sm uppercase tracking-[0.12em] transition-colors ${ selectedCategory === category ? 'bg-bakery-600 text-white border-bakery-600' : 'bg-white border-bakery-300 text-bakery-700 hover:bg-bakery-100' }`} > {category} ))}
{failed && (

We could not load the menu just now. Please refresh, or call us on{' '} (309) 701-0660.

)} {/* Products Grid */} {products.map((product) => ( {/* Image Container */}
{/* Content */}

{product.name}

{product.category}
))}
); }; export default ProductsPage;