balls
This commit is contained in:
@@ -16,6 +16,7 @@ import { ProductModal } from "@/components/modals/product-modal";
|
|||||||
import ProductTable from "@/components/tables/product-table";
|
import ProductTable from "@/components/tables/product-table";
|
||||||
import { Category } from "@/models/categories";
|
import { Category } from "@/models/categories";
|
||||||
import ImportProductsModal from "@/components/modals/import-products-modal";
|
import ImportProductsModal from "@/components/modals/import-products-modal";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
export default function ProductsPage() {
|
export default function ProductsPage() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@@ -106,51 +107,76 @@ export default function ProductsPage() {
|
|||||||
|
|
||||||
|
|
||||||
const handleSaveProduct = async (data: Product, file?: File | null) => {
|
const handleSaveProduct = async (data: Product, file?: File | null) => {
|
||||||
console.log("handleSaveProduct:", data, file);
|
const authToken = document.cookie
|
||||||
|
.split("; ")
|
||||||
const adjustedPricing = data.pricing.map((tier) => ({
|
.find((row) => row.startsWith("Authorization="))
|
||||||
minQuantity: tier.minQuantity,
|
?.split("=")[1];
|
||||||
pricePerUnit:
|
|
||||||
typeof tier.pricePerUnit === "string"
|
if (!authToken) {
|
||||||
? parseFloat(tier.pricePerUnit)
|
router.push("/login");
|
||||||
: tier.pricePerUnit,
|
return;
|
||||||
}));
|
}
|
||||||
|
|
||||||
const productToSave: Product = {
|
setLoading(true);
|
||||||
...data,
|
|
||||||
pricing: adjustedPricing,
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const authToken = document.cookie.split("Authorization=")[1];
|
const url = editing
|
||||||
const apiUrl = editing
|
? `/api/products/${data._id}`
|
||||||
? `${process.env.NEXT_PUBLIC_API_URL}/products/${data._id}`
|
: "/api/products";
|
||||||
: `${process.env.NEXT_PUBLIC_API_URL}/products`;
|
|
||||||
|
// Save product data
|
||||||
const savedProduct = await saveProductData(
|
const savedProduct = await saveProductData(
|
||||||
apiUrl,
|
url,
|
||||||
productToSave,
|
{
|
||||||
|
name: data.name,
|
||||||
|
description: data.description,
|
||||||
|
unitType: data.unitType,
|
||||||
|
category: data.category,
|
||||||
|
pricing: data.pricing,
|
||||||
|
stockTracking: data.stockTracking,
|
||||||
|
currentStock: data.currentStock,
|
||||||
|
lowStockThreshold: data.lowStockThreshold
|
||||||
|
},
|
||||||
authToken,
|
authToken,
|
||||||
editing ? "PUT" : "POST"
|
editing ? "PUT" : "POST"
|
||||||
);
|
);
|
||||||
|
|
||||||
if (file) {
|
if (file) {
|
||||||
await saveProductImage(`${process.env.NEXT_PUBLIC_API_URL}/products/${savedProduct._id}/image`, file, authToken);
|
await saveProductImage(
|
||||||
|
`/api/products/${savedProduct._id}/image`,
|
||||||
|
file,
|
||||||
|
authToken
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
setProducts((prevProducts) => {
|
// If editing and stock values were updated, update stock in the dedicated endpoint
|
||||||
if (editing) {
|
if (editing && data.stockTracking !== undefined) {
|
||||||
return prevProducts.map((product) =>
|
await saveProductData(
|
||||||
product._id === savedProduct._id ? savedProduct : product
|
`/api/stock/${data._id}`,
|
||||||
);
|
{
|
||||||
} else {
|
stockTracking: data.stockTracking,
|
||||||
return [...prevProducts, savedProduct];
|
currentStock: data.currentStock || 0,
|
||||||
}
|
lowStockThreshold: data.lowStockThreshold || 10
|
||||||
});
|
},
|
||||||
|
authToken,
|
||||||
|
"PUT"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Refresh products list
|
||||||
|
const fetchedProducts = await fetchProductData("/api/products", authToken);
|
||||||
|
setProducts(fetchedProducts);
|
||||||
|
|
||||||
setModalOpen(false);
|
setModalOpen(false);
|
||||||
|
setLoading(false);
|
||||||
|
|
||||||
|
toast.success(
|
||||||
|
editing ? "Product updated successfully" : "Product added successfully"
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error saving product:", error);
|
console.error(error);
|
||||||
|
setLoading(false);
|
||||||
|
toast.error("Failed to save product");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -194,11 +220,14 @@ export default function ProductsPage() {
|
|||||||
description: "",
|
description: "",
|
||||||
unitType: "pcs",
|
unitType: "pcs",
|
||||||
category: "",
|
category: "",
|
||||||
|
stockTracking: true,
|
||||||
|
currentStock: 0,
|
||||||
|
lowStockThreshold: 10,
|
||||||
pricing: [{ minQuantity: 1, pricePerUnit: 0 }],
|
pricing: [{ minQuantity: 1, pricePerUnit: 0 }],
|
||||||
image: null,
|
image: null,
|
||||||
});
|
});
|
||||||
setEditing(false);
|
setEditing(false);
|
||||||
setAddProductOpen(true);
|
setModalOpen(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get category name by ID
|
// Get category name by ID
|
||||||
|
|||||||
294
app/dashboard/stock/page.tsx
Normal file
294
app/dashboard/stock/page.tsx
Normal file
@@ -0,0 +1,294 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState, useEffect } from "react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
import Layout from "@/components/layout/layout";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Table, TableHeader, TableRow, TableHead, TableBody, TableCell } from "@/components/ui/table";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Product } from "@/models/products";
|
||||||
|
import { Package, RefreshCw } from "lucide-react";
|
||||||
|
import { fetchProductData, updateProductStock } from "@/lib/productData";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
export default function StockManagementPage() {
|
||||||
|
const router = useRouter();
|
||||||
|
const [products, setProducts] = useState<Product[]>([]);
|
||||||
|
const [loading, setLoading] = useState<boolean>(true);
|
||||||
|
const [editingStock, setEditingStock] = useState<Record<string, boolean>>({});
|
||||||
|
const [stockValues, setStockValues] = useState<Record<string, number>>({});
|
||||||
|
const [searchTerm, setSearchTerm] = useState<string>("");
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const authToken = document.cookie
|
||||||
|
.split("; ")
|
||||||
|
.find((row) => row.startsWith("Authorization="))
|
||||||
|
?.split("=")[1];
|
||||||
|
|
||||||
|
if (!authToken) {
|
||||||
|
router.push("/login");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetchDataAsync = async () => {
|
||||||
|
try {
|
||||||
|
const fetchedProducts = await fetchProductData("/api/products", authToken);
|
||||||
|
setProducts(fetchedProducts);
|
||||||
|
|
||||||
|
// Initialize stock values
|
||||||
|
const initialStockValues: Record<string, number> = {};
|
||||||
|
fetchedProducts.forEach((product: Product) => {
|
||||||
|
if (product._id) {
|
||||||
|
initialStockValues[product._id] = product.currentStock || 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
setStockValues(initialStockValues);
|
||||||
|
|
||||||
|
setLoading(false);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching products:", error);
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchDataAsync();
|
||||||
|
}, [router]);
|
||||||
|
|
||||||
|
const handleEditStock = (productId: string) => {
|
||||||
|
setEditingStock({
|
||||||
|
...editingStock,
|
||||||
|
[productId]: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSaveStock = async (product: Product) => {
|
||||||
|
if (!product._id) return;
|
||||||
|
|
||||||
|
const authToken = document.cookie
|
||||||
|
.split("; ")
|
||||||
|
.find((row) => row.startsWith("Authorization="))
|
||||||
|
?.split("=")[1];
|
||||||
|
|
||||||
|
if (!authToken) {
|
||||||
|
router.push("/login");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const newStockValue = stockValues[product._id] || 0;
|
||||||
|
|
||||||
|
await updateProductStock(
|
||||||
|
product._id,
|
||||||
|
{
|
||||||
|
currentStock: newStockValue,
|
||||||
|
stockTracking: product.stockTracking
|
||||||
|
},
|
||||||
|
authToken
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update local products state
|
||||||
|
setProducts(products.map(p => {
|
||||||
|
if (p._id === product._id) {
|
||||||
|
return {
|
||||||
|
...p,
|
||||||
|
currentStock: newStockValue
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return p;
|
||||||
|
}));
|
||||||
|
|
||||||
|
setEditingStock({
|
||||||
|
...editingStock,
|
||||||
|
[product._id]: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
toast.success("Stock updated successfully");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error updating stock:", error);
|
||||||
|
toast.error("Failed to update stock");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleStockChange = (productId: string, value: number) => {
|
||||||
|
setStockValues({
|
||||||
|
...stockValues,
|
||||||
|
[productId]: value,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const getStockStatus = (product: Product) => {
|
||||||
|
if (!product.stockTracking) return "Not Tracked";
|
||||||
|
|
||||||
|
if (!product.currentStock || product.currentStock <= 0) {
|
||||||
|
return "Out of Stock";
|
||||||
|
} else if (product.lowStockThreshold && product.currentStock <= product.lowStockThreshold) {
|
||||||
|
return "Low Stock";
|
||||||
|
} else {
|
||||||
|
return "In Stock";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const filteredProducts = products.filter(product => {
|
||||||
|
const matchesSearch =
|
||||||
|
!searchTerm ||
|
||||||
|
product.name.toLowerCase().includes(searchTerm.toLowerCase());
|
||||||
|
|
||||||
|
return matchesSearch;
|
||||||
|
});
|
||||||
|
|
||||||
|
const statsData = {
|
||||||
|
total: products.length,
|
||||||
|
inStock: products.filter(p => p.stockTracking && p.currentStock && p.currentStock > 0 && (!p.lowStockThreshold || p.currentStock > p.lowStockThreshold)).length,
|
||||||
|
lowStock: products.filter(p => p.stockTracking && p.lowStockThreshold && p.currentStock && p.currentStock <= p.lowStockThreshold && p.currentStock > 0).length,
|
||||||
|
outOfStock: products.filter(p => p.stockTracking && (!p.currentStock || p.currentStock <= 0)).length,
|
||||||
|
notTracked: products.filter(p => p.stockTracking === false).length
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Layout>
|
||||||
|
<div className="container mx-auto">
|
||||||
|
<div className="flex justify-between items-center mb-8">
|
||||||
|
<h1 className="text-2xl font-bold">Stock Management</h1>
|
||||||
|
<Button onClick={() => router.push("/dashboard/products")}>
|
||||||
|
<Package className="mr-2 h-4 w-4" />
|
||||||
|
Manage Products
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-5 gap-4 mb-8">
|
||||||
|
<div className="bg-background rounded-lg border border-border p-4">
|
||||||
|
<div className="text-3xl font-bold">{statsData.total}</div>
|
||||||
|
<div className="text-sm text-muted-foreground">Total Products</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="bg-background rounded-lg border border-border p-4">
|
||||||
|
<div className="text-3xl font-bold text-green-500">{statsData.inStock}</div>
|
||||||
|
<div className="text-sm text-muted-foreground">In Stock</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="bg-background rounded-lg border border-border p-4">
|
||||||
|
<div className="text-3xl font-bold text-amber-500">{statsData.lowStock}</div>
|
||||||
|
<div className="text-sm text-muted-foreground">Low Stock</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="bg-background rounded-lg border border-border p-4">
|
||||||
|
<div className="text-3xl font-bold text-red-500">{statsData.outOfStock}</div>
|
||||||
|
<div className="text-sm text-muted-foreground">Out of Stock</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="bg-background rounded-lg border border-border p-4">
|
||||||
|
<div className="text-3xl font-bold text-gray-500">{statsData.notTracked}</div>
|
||||||
|
<div className="text-sm text-muted-foreground">Not Tracked</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="bg-background rounded-lg border border-border">
|
||||||
|
<div className="p-4 border-b border-border flex justify-between items-center">
|
||||||
|
<h2 className="text-xl font-semibold">Inventory List</h2>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Input
|
||||||
|
placeholder="Search products..."
|
||||||
|
value={searchTerm}
|
||||||
|
onChange={(e) => setSearchTerm(e.target.value)}
|
||||||
|
className="w-64"
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="icon"
|
||||||
|
onClick={() => setSearchTerm("")}
|
||||||
|
>
|
||||||
|
<RefreshCw className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="overflow-x-auto">
|
||||||
|
<Table>
|
||||||
|
<TableHeader>
|
||||||
|
<TableRow>
|
||||||
|
<TableHead>Product</TableHead>
|
||||||
|
<TableHead>Unit</TableHead>
|
||||||
|
<TableHead>Status</TableHead>
|
||||||
|
<TableHead>Current Stock</TableHead>
|
||||||
|
<TableHead>Low Stock Threshold</TableHead>
|
||||||
|
<TableHead className="text-right">Actions</TableHead>
|
||||||
|
</TableRow>
|
||||||
|
</TableHeader>
|
||||||
|
<TableBody>
|
||||||
|
{loading ? (
|
||||||
|
<TableRow>
|
||||||
|
<TableCell colSpan={6} className="h-24 text-center">
|
||||||
|
Loading...
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
) : filteredProducts.length > 0 ? (
|
||||||
|
filteredProducts.map((product) => (
|
||||||
|
<TableRow key={product._id}>
|
||||||
|
<TableCell className="font-medium">{product.name}</TableCell>
|
||||||
|
<TableCell>{product.unitType}</TableCell>
|
||||||
|
<TableCell className={
|
||||||
|
product.stockTracking === false ? "text-gray-500" :
|
||||||
|
!product.currentStock || product.currentStock <= 0 ? "text-red-500" :
|
||||||
|
product.lowStockThreshold && product.currentStock <= product.lowStockThreshold ? "text-amber-500" :
|
||||||
|
"text-green-500"
|
||||||
|
}>
|
||||||
|
{getStockStatus(product)}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
{editingStock[product._id as string] ? (
|
||||||
|
<Input
|
||||||
|
type="number"
|
||||||
|
min="0"
|
||||||
|
step={product.unitType === "gr" ? "0.1" : "1"}
|
||||||
|
value={stockValues[product._id as string]}
|
||||||
|
onChange={(e) => handleStockChange(product._id as string, parseFloat(e.target.value))}
|
||||||
|
className="max-w-[100px]"
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
product.stockTracking ?
|
||||||
|
`${product.currentStock || 0} ${product.unitType}` :
|
||||||
|
"N/A"
|
||||||
|
)}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
{product.stockTracking ?
|
||||||
|
`${product.lowStockThreshold || 0} ${product.unitType}` :
|
||||||
|
"N/A"}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell className="text-right">
|
||||||
|
{product.stockTracking && (
|
||||||
|
editingStock[product._id as string] ? (
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
onClick={() => handleSaveStock(product)}
|
||||||
|
>
|
||||||
|
Save
|
||||||
|
</Button>
|
||||||
|
) : (
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => handleEditStock(product._id as string)}
|
||||||
|
>
|
||||||
|
Update Stock
|
||||||
|
</Button>
|
||||||
|
)
|
||||||
|
)}
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
))
|
||||||
|
) : (
|
||||||
|
<TableRow>
|
||||||
|
<TableCell colSpan={6} className="h-24 text-center">
|
||||||
|
No products found.
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
)}
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Layout>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -102,15 +102,32 @@ export const ProductModal: React.FC<ProductModalProps> = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
if (!productData.category) {
|
try {
|
||||||
toast.error("Please select or add a category");
|
// Validate required fields
|
||||||
return;
|
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 }) => {
|
const handleAddCategory = (newCategory: { _id: string; name: string; parentId?: string }) => {
|
||||||
@@ -183,42 +200,111 @@ const ProductBasicInfo: React.FC<{
|
|||||||
setProductData: React.Dispatch<React.SetStateAction<ProductData>>;
|
setProductData: React.Dispatch<React.SetStateAction<ProductData>>;
|
||||||
onAddCategory: (newCategory: { _id: string; name: string; parentId?: string }) => void;
|
onAddCategory: (newCategory: { _id: string; name: string; parentId?: string }) => void;
|
||||||
}> = ({ productData, handleChange, categories, setProductData, onAddCategory }) => (
|
}> = ({ productData, handleChange, categories, setProductData, onAddCategory }) => (
|
||||||
<div className="space-y-4">
|
<div className="grid gap-4 mb-4">
|
||||||
<div>
|
<div>
|
||||||
<label className="text-sm font-medium">Product Name</label>
|
<label htmlFor="name" className="text-sm font-medium">
|
||||||
|
Name
|
||||||
|
</label>
|
||||||
<Input
|
<Input
|
||||||
|
id="name"
|
||||||
name="name"
|
name="name"
|
||||||
placeholder="Product Name"
|
required
|
||||||
value={productData.name}
|
value={productData.name || ""}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
className="text-sm h-9"
|
placeholder="Product name"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label className="text-sm font-medium">Description</label>
|
<label className="text-sm font-medium">Description</label>
|
||||||
<Textarea
|
<Textarea
|
||||||
|
id="description"
|
||||||
name="description"
|
name="description"
|
||||||
placeholder="Product Description"
|
rows={3}
|
||||||
value={productData.description}
|
value={productData.description || ""}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
rows={4}
|
placeholder="Product description"
|
||||||
className="text-sm h-24"
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<CategorySelect
|
{/* Stock Management Section */}
|
||||||
categories={categories}
|
<div className="bg-background rounded-lg border border-border p-4">
|
||||||
value={productData.category}
|
<h3 className="text-sm font-medium mb-4">Stock Management</h3>
|
||||||
setProductData={setProductData}
|
|
||||||
onAddCategory={onAddCategory}
|
<div className="flex items-center space-x-2 mb-4">
|
||||||
/>
|
<input
|
||||||
|
id="stockTracking"
|
||||||
<UnitTypeSelect
|
name="stockTracking"
|
||||||
value={productData.unitType}
|
type="checkbox"
|
||||||
setProductData={setProductData}
|
className="h-4 w-4 rounded border-gray-300"
|
||||||
categories={categories}
|
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>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
||||||
import { Edit, Trash } from "lucide-react";
|
import { Edit, Trash, AlertTriangle, CheckCircle, AlertCircle } from "lucide-react";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Product } from "@/models/products";
|
import { Product } from "@/models/products";
|
||||||
|
import { Badge } from "@/components/ui/badge";
|
||||||
|
|
||||||
interface ProductTableProps {
|
interface ProductTableProps {
|
||||||
products: Product[];
|
products: Product[];
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
onEdit: (product: Product) => void;
|
onEdit: (product: Product) => void;
|
||||||
onDelete: (productId: string) => void; // Added onDelete prop
|
onDelete: (productId: string) => void;
|
||||||
getCategoryNameById: (categoryId: string) => string;
|
getCategoryNameById: (categoryId: string) => string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,6 +25,19 @@ const ProductTable = ({
|
|||||||
const categoryNameB = getCategoryNameById(b.category);
|
const categoryNameB = getCategoryNameById(b.category);
|
||||||
return categoryNameA.localeCompare(categoryNameB);
|
return categoryNameA.localeCompare(categoryNameB);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const getStockIcon = (product: Product) => {
|
||||||
|
if (!product.stockTracking) return null;
|
||||||
|
|
||||||
|
if (product.stockStatus === 'out_of_stock') {
|
||||||
|
return <AlertTriangle className="h-4 w-4 text-red-500" />;
|
||||||
|
} else if (product.stockStatus === 'low_stock') {
|
||||||
|
return <AlertCircle className="h-4 w-4 text-amber-500" />;
|
||||||
|
} else {
|
||||||
|
return <CheckCircle className="h-4 w-4 text-green-500" />;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="rounded-lg border dark:border-zinc-700 shadow-sm overflow-hidden">
|
<div className="rounded-lg border dark:border-zinc-700 shadow-sm overflow-hidden">
|
||||||
<Table className="relative">
|
<Table className="relative">
|
||||||
@@ -32,6 +46,7 @@ const ProductTable = ({
|
|||||||
<TableHead className="w-[200px]">Product</TableHead>
|
<TableHead className="w-[200px]">Product</TableHead>
|
||||||
<TableHead className="text-center">Category</TableHead>
|
<TableHead className="text-center">Category</TableHead>
|
||||||
<TableHead className="text-center">Unit</TableHead>
|
<TableHead className="text-center">Unit</TableHead>
|
||||||
|
<TableHead className="text-center">Stock</TableHead>
|
||||||
<TableHead className="text-right">Actions</TableHead>
|
<TableHead className="text-right">Actions</TableHead>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHeader>
|
</TableHeader>
|
||||||
@@ -43,31 +58,47 @@ const ProductTable = ({
|
|||||||
<TableCell>Loading...</TableCell>
|
<TableCell>Loading...</TableCell>
|
||||||
<TableCell>Loading...</TableCell>
|
<TableCell>Loading...</TableCell>
|
||||||
<TableCell>Loading...</TableCell>
|
<TableCell>Loading...</TableCell>
|
||||||
|
<TableCell>Loading...</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
))
|
))
|
||||||
) : sortedProducts.length > 0 ? (
|
) : sortedProducts.length > 0 ? (
|
||||||
sortedProducts.map((product) => (
|
sortedProducts.map((product) => (
|
||||||
<TableRow key={product._id} className="transition-colors hover:bg-gray-50 dark:hover:bg-zinc-800/70">
|
<TableRow key={product._id} className="transition-colors hover:bg-gray-50 dark:hover:bg-zinc-800/70">
|
||||||
<TableCell className="font-medium">{product.name}</TableCell>
|
<TableCell>
|
||||||
<TableCell className="text-center">
|
<div className="font-medium truncate max-w-[180px]">{product.name}</div>
|
||||||
{getCategoryNameById(product.category)} {/* Display category name */}
|
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell className="text-center uppercase text-sm">{product.unitType}</TableCell>
|
<TableCell className="text-center">{getCategoryNameById(product.category)}</TableCell>
|
||||||
<TableCell className="text-right">
|
<TableCell className="text-center">{product.unitType}</TableCell>
|
||||||
<div className="flex justify-end space-x-2">
|
<TableCell className="text-center">
|
||||||
<Button size="sm" variant="ghost" onClick={() => onEdit(product)}>
|
{product.stockTracking ? (
|
||||||
<Edit className="h-4 w-4" />
|
<div className="flex items-center justify-center gap-1">
|
||||||
</Button>
|
{getStockIcon(product)}
|
||||||
<Button size="sm" variant="ghost" className="text-red-600 hover:text-red-700 dark:text-red-400" onClick={() => onDelete(product._id ?? "")}>
|
<span className="text-sm">
|
||||||
<Trash className="h-4 w-4" />
|
{product.currentStock !== undefined ? product.currentStock : 0} {product.unitType}
|
||||||
</Button>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
) : (
|
||||||
|
<Badge variant="outline" className="text-xs">Not Tracked</Badge>
|
||||||
|
)}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell className="text-right flex justify-end space-x-1">
|
||||||
|
<Button variant="ghost" size="sm" onClick={() => onEdit(product)}>
|
||||||
|
<Edit className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => onDelete(product._id as string)}
|
||||||
|
className="text-red-500 hover:text-red-600 hover:bg-red-50 dark:hover:bg-red-950/20"
|
||||||
|
>
|
||||||
|
<Trash className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
))
|
))
|
||||||
) : (
|
) : (
|
||||||
<TableRow>
|
<TableRow>
|
||||||
<TableCell colSpan={4} className="h-24 text-center">
|
<TableCell colSpan={5} className="h-24 text-center">
|
||||||
No products found.
|
No products found.
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Home, Package, Box, Truck, Settings, FolderTree, MessageCircle } from "lucide-react"
|
import { Home, Package, Box, Truck, Settings, FolderTree, MessageCircle, BarChart3 } from "lucide-react"
|
||||||
|
|
||||||
export const sidebarConfig = [
|
export const sidebarConfig = [
|
||||||
{
|
{
|
||||||
@@ -12,6 +12,7 @@ export const sidebarConfig = [
|
|||||||
title: "Management",
|
title: "Management",
|
||||||
items: [
|
items: [
|
||||||
{ name: "Products", href: "/dashboard/products", icon: Box },
|
{ name: "Products", href: "/dashboard/products", icon: Box },
|
||||||
|
{ name: "Stock", href: "/dashboard/stock", icon: BarChart3 },
|
||||||
{ name: "Categories", href: "/dashboard/categories", icon: FolderTree},
|
{ name: "Categories", href: "/dashboard/categories", icon: FolderTree},
|
||||||
{ name: "Shipping", href: "/dashboard/shipping", icon: Truck },
|
{ name: "Shipping", href: "/dashboard/shipping", icon: Truck },
|
||||||
{ name: "Storefront", href: "/dashboard/storefront", icon: Settings },
|
{ name: "Storefront", href: "/dashboard/storefront", icon: Settings },
|
||||||
|
|||||||
@@ -67,4 +67,42 @@ export const deleteProductData = async (url: string, authToken: string) => {
|
|||||||
console.error("Error deleting product data:", error);
|
console.error("Error deleting product data:", error);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Stock management functions
|
||||||
|
export const fetchStockData = async (authToken: string) => {
|
||||||
|
try {
|
||||||
|
return await fetchData('/api/stock', {
|
||||||
|
headers: { Authorization: `Bearer ${authToken}` },
|
||||||
|
credentials: "include",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching stock data:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const updateProductStock = async (
|
||||||
|
productId: string,
|
||||||
|
stockData: {
|
||||||
|
currentStock: number;
|
||||||
|
stockTracking?: boolean;
|
||||||
|
lowStockThreshold?: number;
|
||||||
|
},
|
||||||
|
authToken: string
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
return await fetchData(`/api/stock/${productId}`, {
|
||||||
|
method: "PUT",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${authToken}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
credentials: "include",
|
||||||
|
body: JSON.stringify(stockData),
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error updating product stock:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -38,6 +38,10 @@ export interface Product {
|
|||||||
name: string
|
name: string
|
||||||
description: string
|
description: string
|
||||||
stock?: number
|
stock?: number
|
||||||
|
stockTracking?: boolean
|
||||||
|
currentStock?: number
|
||||||
|
lowStockThreshold?: number
|
||||||
|
stockStatus?: 'in_stock' | 'low_stock' | 'out_of_stock'
|
||||||
unitType: string
|
unitType: string
|
||||||
category: string
|
category: string
|
||||||
pricing: PricingTier[]
|
pricing: PricingTier[]
|
||||||
|
|||||||
@@ -4,6 +4,11 @@ export interface Product {
|
|||||||
description: string;
|
description: string;
|
||||||
unitType: string;
|
unitType: string;
|
||||||
category: string;
|
category: string;
|
||||||
|
// Stock management fields
|
||||||
|
stockTracking?: boolean;
|
||||||
|
currentStock?: number;
|
||||||
|
lowStockThreshold?: number;
|
||||||
|
stockStatus?: 'in_stock' | 'low_stock' | 'out_of_stock';
|
||||||
pricing: Array<{
|
pricing: Array<{
|
||||||
minQuantity: number;
|
minQuantity: number;
|
||||||
pricePerUnit: number;
|
pricePerUnit: number;
|
||||||
|
|||||||
Reference in New Issue
Block a user