"use client" import { useState, useEffect, ChangeEvent } from "react" import Link from "next/link" import { motion } from "framer-motion" import { PlusCircle, Truck, BarChart3, MessageSquare, } from "lucide-react" import { Card, CardContent } from "@/components/ui/card" import dynamic from "next/dynamic" import { Product } from "@/models/products" import { Category } from "@/models/categories" import { clientFetch } from "@/lib/api" import { toast } from "sonner" const ProductModal = dynamic(() => import("@/components/modals/product-modal").then(mod => ({ default: mod.ProductModal })), { loading: () => null }); const actions = [ { title: "Add Product", icon: PlusCircle, href: "/dashboard/products/new", // Fallback text color: "bg-blue-500/10 text-blue-500", description: "Create a new listing", action: "modal" }, { title: "Process Orders", icon: Truck, href: "/dashboard/orders?status=paid", color: "bg-emerald-500/10 text-emerald-500", description: "Ship pending orders" }, { title: "Analytics", icon: BarChart3, href: "/dashboard/analytics", color: "bg-purple-500/10 text-purple-500", description: "View sales performance" }, { title: "Messages", icon: MessageSquare, href: "/dashboard/chats", color: "bg-amber-500/10 text-amber-500", description: "Chat with customers" } ] export default function QuickActions() { const [modalOpen, setModalOpen] = useState(false); const [loading, setLoading] = useState(false); const [categories, setCategories] = useState([]); const [productData, setProductData] = useState({ name: "", description: "", unitType: "pcs", category: "", pricing: [{ minQuantity: 1, pricePerUnit: 0 }], image: null, costPerUnit: 0, }); // Fetch categories on mount useEffect(() => { const fetchCategories = async () => { try { const data = await clientFetch('/categories'); setCategories(data); } catch (error) { console.error("Failed to fetch categories:", error); } }; fetchCategories(); }, []); const handleChange = (e: ChangeEvent) => { setProductData({ ...productData, [e.target.name]: e.target.value }); }; const handleTieredPricingChange = (e: ChangeEvent, index: number) => { const updatedPricing = [...productData.pricing]; const name = e.target.name as "minQuantity" | "pricePerUnit"; updatedPricing[index][name] = e.target.valueAsNumber || 0; setProductData({ ...productData, pricing: updatedPricing }); }; const handleAddTier = () => { setProductData((prev) => ({ ...prev, pricing: [...prev.pricing, { minQuantity: 1, pricePerUnit: 0 }], })); }; const handleRemoveTier = (index: number) => { setProductData((prev) => ({ ...prev, pricing: prev.pricing.filter((_, i) => i !== index), })); }; const handleSaveProduct = async (data: Product, file?: File | null) => { try { setLoading(true); // Prepare the product data const payload = { ...data, stockTracking: data.stockTracking ?? true, currentStock: data.currentStock ?? 0, lowStockThreshold: data.lowStockThreshold ?? 10, stockStatus: data.stockStatus ?? 'out_of_stock' }; const productResponse = await clientFetch("/products", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }); if (file) { const formData = new FormData(); formData.append("file", file); await fetch(`${process.env.NEXT_PUBLIC_API_URL}/products/${productResponse._id}/image`, { method: "PUT", headers: { Authorization: `Bearer ${document.cookie.split("; ").find((row) => row.startsWith("Authorization="))?.split("=")[1]}`, }, body: formData, }); } setModalOpen(false); setProductData({ name: "", description: "", unitType: "pcs", category: "", pricing: [{ minQuantity: 1, pricePerUnit: 0 }], image: null, costPerUnit: 0, }); toast.success("Product added successfully"); // Optional: trigger a refresh of products or stats if needed // currently just closing modal } catch (error) { console.error(error); toast.error("Failed to save product"); } finally { setLoading(false); } }; return ( <>
{actions.map((action, index) => { const isModalAction = action.action === "modal"; const CardContentWrapper = () => (

{action.title}

{action.description}

); return ( {isModalAction ? (
setModalOpen(true)} className="cursor-pointer h-full">
) : ( )}
); })}
setModalOpen(false)} onSave={handleSaveProduct} productData={productData} categories={categories} editing={false} handleChange={handleChange} handleTieredPricingChange={handleTieredPricingChange} handleAddTier={handleAddTier} handleRemoveTier={handleRemoveTier} setProductData={setProductData} /> ) }