66 lines
2.4 KiB
TypeScript
66 lines
2.4 KiB
TypeScript
import { useApp } from '../context/AppContext';
|
|
import { useProducts } from '../hooks/useProducts';
|
|
import { CartItem } from '../types';
|
|
|
|
export function Products() {
|
|
const { products, refetch } = useProducts();
|
|
const { addToCart } = useApp();
|
|
|
|
return (
|
|
<div>
|
|
<div className="flex justify-between items-center mb-8">
|
|
<h1 className="text-3xl font-bold text-white">Products</h1>
|
|
<button
|
|
onClick={() => refetch()}
|
|
className="bg-gray-700 hover:bg-gray-600 text-white px-4 py-2 rounded-lg transition-colors"
|
|
>
|
|
Refresh
|
|
</button>
|
|
</div>
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
|
|
{products.map((product) => (
|
|
<div
|
|
key={product.id}
|
|
className="bg-gray-800 rounded-lg overflow-hidden border border-gray-700 hover:border-blue-500 transition-colors"
|
|
>
|
|
{product.image_url && (
|
|
<img
|
|
src={product.image_url}
|
|
alt={product.name}
|
|
className="w-full h-48 object-cover"
|
|
/>
|
|
)}
|
|
<div className="p-4">
|
|
<h3 className="text-lg font-semibold text-white mb-2">{product.name}</h3>
|
|
<p className="text-gray-400 text-sm mb-3 line-clamp-2">{product.description}</p>
|
|
<div className="flex items-center justify-between">
|
|
<span className="text-xl font-bold text-blue-400">${product.price}</span>
|
|
<span className="text-sm text-gray-400">Stock: {product.stock}</span>
|
|
</div>
|
|
<button
|
|
onClick={() => {
|
|
const cartItem: CartItem = {
|
|
id: parseInt(product.id!),
|
|
name: product.name,
|
|
price: product.price,
|
|
quantity: 1,
|
|
image_url: product.image_url,
|
|
};
|
|
addToCart(cartItem);
|
|
}}
|
|
className="mt-4 w-full bg-blue-600 hover:bg-blue-700 text-white py-2 px-4 rounded-lg transition-colors"
|
|
>
|
|
Add to Cart
|
|
</button>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
{products.length === 0 && (
|
|
<div className="text-center py-12">
|
|
<p className="text-gray-400">No products available</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|