Template
Next.js 16 + React 19 storefront template with Shopify Storefront API integration, Tailwind v4, and shadcn/ui components. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0111qSr3KopyGRsJ6LznR1xZ
239 lines
6.9 KiB
TypeScript
239 lines
6.9 KiB
TypeScript
'use client';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
import ProductCard from './product-card';
|
|
import { getProducts } from '@/hooks/use-shopify-products';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Loader } from '@/app/components/ui/loader';
|
|
|
|
interface ProductImage {
|
|
url: string;
|
|
altText?: string;
|
|
}
|
|
|
|
interface ProductPrice {
|
|
amount: string;
|
|
currencyCode: string;
|
|
}
|
|
|
|
interface ProductVariant {
|
|
id: string;
|
|
title: string;
|
|
price: ProductPrice;
|
|
availableForSale: boolean;
|
|
}
|
|
|
|
interface Product {
|
|
id: string;
|
|
title: string;
|
|
description?: string;
|
|
handle: string;
|
|
images: {
|
|
edges: Array<{
|
|
node: ProductImage;
|
|
}>;
|
|
};
|
|
priceRange: {
|
|
minVariantPrice: ProductPrice;
|
|
};
|
|
compareAtPriceRange?: {
|
|
minVariantPrice: ProductPrice;
|
|
};
|
|
variants: {
|
|
edges: Array<{
|
|
node: ProductVariant;
|
|
}>;
|
|
};
|
|
}
|
|
|
|
interface ProductsProps {
|
|
title?: string;
|
|
limit?: number;
|
|
showLoadMore?: boolean;
|
|
}
|
|
|
|
const Products: React.FC<ProductsProps> = ({
|
|
title = 'Our Products',
|
|
limit = 12,
|
|
showLoadMore = true,
|
|
}) => {
|
|
const [products, setProducts] = useState<Product[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [loadingMore, setLoadingMore] = useState(false);
|
|
const [hasMoreProducts, setHasMoreProducts] = useState(true);
|
|
|
|
const fetchProducts = async (
|
|
currentProducts: Product[] = [],
|
|
loadMore = false
|
|
) => {
|
|
try {
|
|
if (loadMore) {
|
|
setLoadingMore(true);
|
|
} else {
|
|
setLoading(true);
|
|
setError(null);
|
|
}
|
|
|
|
const newProducts = await getProducts({
|
|
first: limit,
|
|
sortKey: 'CREATED_AT',
|
|
reverse: true,
|
|
});
|
|
|
|
if (loadMore) {
|
|
const existingIds = new Set(currentProducts.map((p) => p.id));
|
|
const uniqueNewProducts = newProducts.filter(
|
|
(p) => !existingIds.has(p.id)
|
|
);
|
|
|
|
if (uniqueNewProducts.length === 0) {
|
|
setHasMoreProducts(false);
|
|
} else {
|
|
setProducts((prev) => [...prev, ...uniqueNewProducts]);
|
|
}
|
|
} else {
|
|
setProducts(newProducts);
|
|
setHasMoreProducts(newProducts.length === limit);
|
|
}
|
|
} catch (err) {
|
|
console.error('Error fetching products:', err);
|
|
setError(err instanceof Error ? err.message : 'Failed to load products');
|
|
} finally {
|
|
setLoading(false);
|
|
setLoadingMore(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
fetchProducts();
|
|
}, [limit]);
|
|
|
|
const handleAddToCart = async (product: Product) => {
|
|
console.log('Adding to cart:', product);
|
|
};
|
|
|
|
const handleLoadMore = () => {
|
|
if (!loadingMore && hasMoreProducts) {
|
|
fetchProducts(products, true);
|
|
}
|
|
};
|
|
|
|
if (loading) {
|
|
return (
|
|
<div className="py-20">
|
|
<div className="max-w-screen-2xl mx-auto px-8">
|
|
<div className="flex justify-center mb-6">
|
|
<div className="inline px-6 py-2 bg-white border rounded-3xl text-xs font-mono tracking-widest text-muted-foreground">
|
|
DISCOVER
|
|
</div>
|
|
</div>
|
|
<h2 className="text-center text-6xl font-bold tracking-tighter font-heading mb-5 text-foreground">
|
|
{title}
|
|
</h2>
|
|
<p className="text-center text-muted-foreground max-w-md mx-auto mb-16">
|
|
Loading our finest selection...
|
|
</p>
|
|
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-8">
|
|
{Array.from({ length: 8 }).map((_, index) => (
|
|
<div
|
|
key={index}
|
|
className="bg-white rounded-3xl overflow-hidden animate-pulse border border-border h-[460px]"
|
|
>
|
|
<div className="h-[280px] bg-zinc-100"></div>
|
|
<div className="p-8 space-y-6">
|
|
<div className="h-6 bg-zinc-200 rounded-full w-4/5"></div>
|
|
<div className="h-4 bg-zinc-200 rounded w-1/3"></div>
|
|
<div className="pt-4 flex gap-3">
|
|
<div className="h-11 flex-1 bg-zinc-200 rounded-2xl"></div>
|
|
<div className="h-11 flex-1 bg-zinc-200 rounded-2xl"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (error || products.length === 0) {
|
|
return (
|
|
<div className="py-20 bg-zinc-50">
|
|
<div className="max-w-screen-2xl mx-auto px-8 text-center">
|
|
<h2 className="text-6xl font-bold tracking-tighter font-heading mb-8 text-foreground">
|
|
{title}
|
|
</h2>
|
|
<div className="mx-auto max-w-sm bg-white border border-border rounded-3xl p-12">
|
|
<i className="ri-inbox-line text-6xl text-muted-foreground mb-6 block"></i>
|
|
<h3 className="font-semibold text-2xl mb-3 tracking-tight">
|
|
{error ? 'Connection Error' : 'Coming Soon'}
|
|
</h3>
|
|
<p className="text-muted-foreground mb-8 text-base">
|
|
{error ||
|
|
'Our curated collection is being prepared. Please check back shortly.'}
|
|
</p>
|
|
{error && (
|
|
<Button onClick={() => fetchProducts()} className="btn-modern">
|
|
Try Again
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="py-20 bg-white">
|
|
<div className="max-w-screen-2xl mx-auto px-8">
|
|
<div className="flex justify-center mb-6">
|
|
<div className="inline px-6 py-2 bg-zinc-100 rounded-3xl text-xs font-mono tracking-[1.5px] text-muted-foreground">
|
|
CURATED SELECTION
|
|
</div>
|
|
</div>
|
|
|
|
<h2 className="text-center text-6xl font-bold tracking-tighter font-heading mb-5 text-foreground">
|
|
{title}
|
|
</h2>
|
|
<p className="text-center text-muted-foreground max-w-md mx-auto mb-16 text-lg">
|
|
Beautifully designed objects for everyday life
|
|
</p>
|
|
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-x-8 gap-y-16 mb-20">
|
|
{products.map((product) => (
|
|
<ProductCard
|
|
key={product.id}
|
|
product={product}
|
|
onAddToCart={handleAddToCart}
|
|
/>
|
|
))}
|
|
</div>
|
|
|
|
{showLoadMore && hasMoreProducts && (
|
|
<div className="flex justify-center">
|
|
<Button
|
|
onClick={handleLoadMore}
|
|
disabled={loadingMore}
|
|
size="lg"
|
|
className="btn-modern rounded-2xl px-14 py-7 text-sm tracking-widest font-medium border border-border"
|
|
>
|
|
{loadingMore ? (
|
|
<>
|
|
<Loader size={18} className="mr-3" />
|
|
LOADING MORE
|
|
</>
|
|
) : (
|
|
'LOAD MORE PRODUCTS'
|
|
)}
|
|
</Button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default Products;
|