diff --git a/app/dashboard/products/page.tsx b/app/dashboard/products/page.tsx index 11c2a5e..c46432d 100644 --- a/app/dashboard/products/page.tsx +++ b/app/dashboard/products/page.tsx @@ -266,6 +266,32 @@ export default function ProductsPage() { } }; + // Handle toggle product enabled status + const handleToggleEnabled = async (productId: string, enabled: boolean) => { + try { + setLoading(true); + + await clientFetch(`/products/${productId}`, { + method: "PATCH", + body: JSON.stringify({ enabled }), + }); + + // Update the local state + setProducts(products.map(product => + product._id === productId + ? { ...product, enabled } + : product + )); + + toast.success(`Product ${enabled ? 'enabled' : 'disabled'} successfully`); + setLoading(false); + } catch (error) { + console.error(error); + toast.error("Failed to update product status"); + setLoading(false); + } + }; + return (
@@ -315,6 +341,7 @@ export default function ProductsPage() { loading={loading} onEdit={handleEditProduct} onDelete={handleDeleteProduct} + onToggleEnabled={handleToggleEnabled} getCategoryNameById={getCategoryNameById} /> diff --git a/components/modals/product-modal.tsx b/components/modals/product-modal.tsx index 37e39b0..af90f46 100644 --- a/components/modals/product-modal.tsx +++ b/components/modals/product-modal.tsx @@ -21,6 +21,7 @@ import { toast } from "sonner"; import type React from "react"; import { Plus } from "lucide-react"; import { apiRequest } from "@/lib/api"; +import { Switch } from "@/components/ui/switch"; type CategorySelectProps = { categories: { _id: string; name: string; parentId?: string }[]; @@ -200,34 +201,54 @@ const ProductBasicInfo: React.FC<{ setProductData: React.Dispatch>; onAddCategory: (newCategory: { _id: string; name: string; parentId?: string }) => void; }> = ({ productData, handleChange, categories, setProductData, onAddCategory }) => ( -
+
- +
- -