kanban-app/frontend/src/pages/Products.tsx

74 lines
2.5 KiB
TypeScript
Raw Normal View History

2026-02-24 08:52:57 +00:00
import { useApp } from '../context/AppContext'
2026-02-24 11:03:23 +00:00
import { useProducts } from '../hooks/useProducts'
import { CartItem } from '../types'
export function Products() {
2026-02-24 11:03:23 +00:00
const { products, refetch } = useProducts()
const { addToCart } = useApp()
return (
<div>
2026-02-24 11:03:23 +00:00
<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
2026-02-24 08:52:57 +00:00
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>
)
}