This commit is contained in:
NotII
2025-07-30 12:25:46 +02:00
parent 1b51f29c24
commit 4d1c37de92
3 changed files with 175 additions and 3 deletions

View File

@@ -0,0 +1,125 @@
"use client";
import { useState, useEffect } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Checkbox } from "@/components/ui/checkbox";
import { ScrollArea } from "@/components/ui/scroll-area";
import { Search, Package } from "lucide-react";
import { apiRequest } from "@/lib/api";
interface Product {
_id: string;
name: string;
description?: string;
unitType: string;
pricing: Array<{ minQuantity: number; pricePerUnit: number }>;
image?: string;
}
interface ProductSelectorProps {
selectedProducts: string[];
onSelectionChange: (productIds: string[]) => void;
}
export default function ProductSelector({ selectedProducts, onSelectionChange }: ProductSelectorProps) {
const [products, setProducts] = useState<Product[]>([]);
const [loading, setLoading] = useState(true);
const [searchTerm, setSearchTerm] = useState("");
useEffect(() => {
const fetchProducts = async () => {
try {
const fetchedProducts = await apiRequest('/products/for-selection', 'GET');
setProducts(fetchedProducts);
} catch (error) {
console.error('Error fetching products:', error);
} finally {
setLoading(false);
}
};
fetchProducts();
}, []);
const filteredProducts = products.filter(product =>
product.name.toLowerCase().includes(searchTerm.toLowerCase())
);
const handleProductToggle = (productId: string) => {
const newSelection = selectedProducts.includes(productId)
? selectedProducts.filter(id => id !== productId)
: [...selectedProducts, productId];
onSelectionChange(newSelection);
};
const getMinPrice = (product: Product) => {
if (!product.pricing || product.pricing.length === 0) return 0;
const minTier = product.pricing.reduce((min, tier) =>
tier.pricePerUnit < min.pricePerUnit ? tier : min
);
return minTier.pricePerUnit;
};
if (loading) {
return <div className="text-center py-4">Loading products...</div>;
}
return (
<div className="space-y-4">
<div className="relative">
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input
placeholder="Search products..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="pl-10"
/>
</div>
<ScrollArea className="h-64">
<div className="space-y-2">
{filteredProducts.length === 0 ? (
<div className="text-center py-8 text-muted-foreground">
<Package className="h-8 w-8 mx-auto mb-2" />
{searchTerm ? "No products found" : "No products available"}
</div>
) : (
filteredProducts.map((product) => (
<div
key={product._id}
className="flex items-center space-x-3 p-3 border rounded-lg hover:bg-muted/50"
>
<Checkbox
checked={selectedProducts.includes(product._id)}
onCheckedChange={() => handleProductToggle(product._id)}
/>
<div className="flex-1 min-w-0">
<div className="flex items-center justify-between">
<div className="flex-1 min-w-0">
<p className="font-medium truncate">{product.name}</p>
{product.description && (
<p className="text-sm text-muted-foreground truncate">
{product.description}
</p>
)}
</div>
<div className="text-sm text-muted-foreground ml-2">
£{getMinPrice(product).toFixed(2)}
</div>
</div>
</div>
</div>
))
)}
</div>
</ScrollArea>
{selectedProducts.length > 0 && (
<div className="text-sm text-muted-foreground">
{selectedProducts.length} product(s) selected
</div>
)}
</div>
);
}