This commit is contained in:
NotII
2025-03-06 12:49:01 +00:00
parent e26069abfa
commit bc9f8ebbf9
8 changed files with 573 additions and 85 deletions

View File

@@ -102,15 +102,32 @@ export const ProductModal: React.FC<ProductModalProps> = ({
};
const handleSave = async () => {
if (!productData.category) {
toast.error("Please select or add a category");
return;
try {
// Validate required fields
if (!productData.name || !productData.category || !productData.unitType) {
toast.error("Please fill in all required fields");
return;
}
// Validate pricing tiers
if (!productData.pricing || productData.pricing.length === 0) {
toast.error("At least one pricing tier is required");
return;
}
// Make sure stock values are numbers
let stockData = { ...productData };
if (stockData.stockTracking) {
stockData.currentStock = Number(stockData.currentStock) || 0;
stockData.lowStockThreshold = Number(stockData.lowStockThreshold) || 10;
}
await onSave(stockData, selectedFile);
onClose();
} catch (error) {
console.error("Error saving product:", error);
toast.error("Failed to save product");
}
onSave(productData, selectedFile);
toast.success(editing ? "Product updated!" : "Product added!");
onClose();
};
const handleAddCategory = (newCategory: { _id: string; name: string; parentId?: string }) => {
@@ -183,42 +200,111 @@ const ProductBasicInfo: React.FC<{
setProductData: React.Dispatch<React.SetStateAction<ProductData>>;
onAddCategory: (newCategory: { _id: string; name: string; parentId?: string }) => void;
}> = ({ productData, handleChange, categories, setProductData, onAddCategory }) => (
<div className="space-y-4">
<div className="grid gap-4 mb-4">
<div>
<label className="text-sm font-medium">Product Name</label>
<label htmlFor="name" className="text-sm font-medium">
Name
</label>
<Input
id="name"
name="name"
placeholder="Product Name"
value={productData.name}
required
value={productData.name || ""}
onChange={handleChange}
className="text-sm h-9"
placeholder="Product name"
/>
</div>
<div>
<label className="text-sm font-medium">Description</label>
<Textarea
id="description"
name="description"
placeholder="Product Description"
value={productData.description}
rows={3}
value={productData.description || ""}
onChange={handleChange}
rows={4}
className="text-sm h-24"
placeholder="Product description"
/>
</div>
<CategorySelect
categories={categories}
value={productData.category}
setProductData={setProductData}
onAddCategory={onAddCategory}
/>
<UnitTypeSelect
value={productData.unitType}
setProductData={setProductData}
categories={categories}
/>
{/* Stock Management Section */}
<div className="bg-background rounded-lg border border-border p-4">
<h3 className="text-sm font-medium mb-4">Stock Management</h3>
<div className="flex items-center space-x-2 mb-4">
<input
id="stockTracking"
name="stockTracking"
type="checkbox"
className="h-4 w-4 rounded border-gray-300"
checked={productData.stockTracking !== false}
onChange={(e) => {
setProductData({
...productData,
stockTracking: e.target.checked
});
}}
/>
<label htmlFor="stockTracking" className="text-sm">
Enable Stock Tracking
</label>
</div>
{productData.stockTracking !== false && (
<div className="grid grid-cols-2 gap-4">
<div>
<label htmlFor="lowStockThreshold" className="text-sm font-medium">
Low Stock Threshold
</label>
<Input
id="lowStockThreshold"
name="lowStockThreshold"
type="number"
min="1"
step={productData.unitType === 'gr' ? '0.1' : '1'}
value={productData.lowStockThreshold || 10}
onChange={handleChange}
placeholder="10"
/>
</div>
<div>
<label htmlFor="currentStock" className="text-sm font-medium">
Current Stock
</label>
<Input
id="currentStock"
name="currentStock"
type="number"
min="0"
step={productData.unitType === 'gr' ? '0.1' : '1'}
value={productData.currentStock || 0}
onChange={handleChange}
placeholder="0"
/>
</div>
</div>
)}
</div>
<div className="grid grid-cols-2 gap-4">
<div>
<label className="text-sm font-medium">Category</label>
<CategorySelect
categories={categories}
value={productData.category}
setProductData={setProductData}
onAddCategory={onAddCategory}
/>
</div>
<div>
<label className="text-sm font-medium">Unit Type</label>
<UnitTypeSelect
value={productData.unitType}
setProductData={setProductData}
categories={categories}
/>
</div>
</div>
</div>
);