- Backend: FastAPI app with all models, services, schemas, routes - Admin Panel: Next.js dashboard with all modules (CRM, POS, repairs, warranties, billing, notifications, etc.) - Ecommerce: storefront (submodule) + product catalog - AI Agents: .agents/ skill library for Antigravity IDE - Docs: architecture, billing, notification gateway markdown - Submodules: Backend/OpenWA, Ecommerce/File 2/storefront
217 lines
8.5 KiB
TypeScript
217 lines
8.5 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
import { useRouter } from 'next/navigation';
|
|
import { crmService, CrmDeal } from '@/services/api/crmService';
|
|
import { DollarSign, Plus, Kanban as KanbanIcon, ArrowRight } from 'lucide-react';
|
|
|
|
const STAGES = ['NEW', 'CONTACTED', 'PROPOSAL_SENT', 'NEGOTIATING', 'CLOSED_WON', 'CLOSED_LOST'];
|
|
|
|
const STAGE_ROUTE_MAP: Record<string, string> = {
|
|
NEW: '/crm-leads/new',
|
|
CONTACTED: '/crm-leads/contacted',
|
|
PROPOSAL_SENT: '/crm-leads/quote-sent',
|
|
QUALIFIED: '/crm-leads/qualified',
|
|
NEGOTIATING: '/crm-leads/qualified',
|
|
CLOSED_WON: '/crm-leads/converted',
|
|
CLOSED_LOST: '/crm-leads/converted',
|
|
};
|
|
|
|
export default function CrmDealsPage() {
|
|
const router = useRouter();
|
|
const [deals, setDeals] = useState<CrmDeal[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState('');
|
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
const [newTitle, setNewTitle] = useState('');
|
|
const [newValue, setNewValue] = useState(5000);
|
|
const [newStage, setNewStage] = useState('NEW');
|
|
|
|
useEffect(() => {
|
|
loadDeals();
|
|
}, []);
|
|
|
|
const loadDeals = async () => {
|
|
try {
|
|
setLoading(true);
|
|
setError('');
|
|
const data = await crmService.getDeals();
|
|
setDeals(Array.isArray(data) ? data : []);
|
|
} catch (err: any) {
|
|
console.error('Failed to fetch deals:', err);
|
|
setError(err.message || 'Failed to load deals');
|
|
setDeals([]);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleCreateDeal = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
try {
|
|
setError('');
|
|
await crmService.createDeal({
|
|
title: newTitle,
|
|
value: Number(newValue),
|
|
estimated_value: Number(newValue),
|
|
stage: newStage,
|
|
});
|
|
setIsModalOpen(false);
|
|
setNewTitle('');
|
|
loadDeals();
|
|
} catch (err: any) {
|
|
setError(err.message || 'Failed to create deal');
|
|
}
|
|
};
|
|
|
|
const getDealVal = (d: CrmDeal) => Number(d.value ?? d.estimated_value ?? 0);
|
|
|
|
const totalPipelineValue = deals.reduce((sum, d) => sum + getDealVal(d), 0);
|
|
|
|
return (
|
|
<div className="p-6 space-y-6">
|
|
<div className="flex items-center justify-between">
|
|
<div>
|
|
<h1 className="text-2xl font-bold tracking-tight text-foreground flex items-center gap-2">
|
|
<KanbanIcon className="w-6 h-6 text-primary" />
|
|
CRM Deals Kanban Board
|
|
</h1>
|
|
<p className="text-sm text-muted-foreground">
|
|
Manage wholesale deals, B2B quotes, and pipeline progress
|
|
</p>
|
|
</div>
|
|
<div className="flex items-center gap-4">
|
|
<div className="bg-primary/10 border border-primary/20 rounded-lg px-4 py-2 text-right">
|
|
<p className="text-xs text-muted-foreground">Total Pipeline Value</p>
|
|
<p className="text-lg font-bold text-primary">₹{totalPipelineValue.toLocaleString()}</p>
|
|
</div>
|
|
<button
|
|
onClick={() => setIsModalOpen(true)}
|
|
className="flex items-center gap-2 bg-primary text-white px-4 py-2.5 rounded-lg text-sm font-semibold hover:bg-primary/90 transition-colors shadow-sm"
|
|
>
|
|
<Plus className="w-4 h-4" />
|
|
New Deal
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{loading ? (
|
|
<div className="py-20 text-center text-muted-foreground animate-pulse">Loading Deals Pipeline...</div>
|
|
) : (
|
|
<div className="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-6 gap-4 overflow-x-auto pb-4">
|
|
{STAGES.map((stage) => {
|
|
const stageDeals = deals.filter((d) => d.stage === stage);
|
|
const stageTotal = stageDeals.reduce((sum, d) => sum + getDealVal(d), 0);
|
|
|
|
return (
|
|
<div key={stage} className="bg-card border border-border rounded-xl p-3 flex flex-col min-w-[200px] shadow-xs">
|
|
<div className="flex items-center justify-between border-b border-border pb-2 mb-3">
|
|
<span className="text-xs font-bold uppercase tracking-wider text-foreground">
|
|
{stage.replace('_', ' ')}
|
|
</span>
|
|
<span className="text-xs bg-muted text-muted-foreground px-2 py-0.5 rounded-full font-semibold">
|
|
{stageDeals.length}
|
|
</span>
|
|
</div>
|
|
|
|
<div className="text-[11px] text-muted-foreground mb-3 font-semibold">
|
|
Total: ₹{stageTotal.toLocaleString()}
|
|
</div>
|
|
|
|
<div className="flex-1 space-y-2.5 overflow-y-auto max-h-[600px]">
|
|
{stageDeals.map((deal) => (
|
|
<div
|
|
key={deal.deal_id}
|
|
onClick={() => router.push(STAGE_ROUTE_MAP[deal.stage] || '/crm-leads/new')}
|
|
className="p-3 bg-background border border-border rounded-lg shadow-2xs hover:border-primary transition-all cursor-pointer group"
|
|
>
|
|
<h4 className="text-xs font-semibold text-foreground line-clamp-2 group-hover:text-primary transition-colors">
|
|
{deal.title}
|
|
</h4>
|
|
<div className="mt-2 flex items-center justify-between">
|
|
<span className="text-xs font-bold text-emerald-600">
|
|
₹{getDealVal(deal).toLocaleString()}
|
|
</span>
|
|
<span className="text-[10px] text-muted-foreground">
|
|
{new Date(deal.created_at).toLocaleDateString()}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
))}
|
|
{stageDeals.length === 0 && (
|
|
<div className="py-8 text-center text-xs text-muted-foreground border border-dashed border-border rounded-lg">
|
|
No Deals
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
|
|
{isModalOpen && (
|
|
<div className="fixed inset-0 bg-black/50 backdrop-blur-xs flex items-center justify-center z-50">
|
|
<div className="bg-card border border-border rounded-xl p-6 w-full max-w-md shadow-xl">
|
|
<h3 className="text-lg font-bold mb-4 text-foreground">Create New Deal</h3>
|
|
<form onSubmit={handleCreateDeal} className="space-y-4">
|
|
<div>
|
|
<label className="text-xs font-semibold text-foreground">Deal Title</label>
|
|
<input
|
|
type="text"
|
|
required
|
|
value={newTitle}
|
|
onChange={(e) => setNewTitle(e.target.value)}
|
|
className="w-full mt-1 p-2 bg-background border border-border rounded-lg text-sm"
|
|
placeholder="e.g. Bulk iPhone 14 Repair Contract"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="text-xs font-semibold text-foreground">Estimated Deal Value (₹)</label>
|
|
<input
|
|
type="number"
|
|
required
|
|
value={newValue}
|
|
onChange={(e) => setNewValue(Number(e.target.value))}
|
|
className="w-full mt-1 p-2 bg-background border border-border rounded-lg text-sm"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="text-xs font-semibold text-foreground">Stage</label>
|
|
<select
|
|
value={newStage}
|
|
onChange={(e) => setNewStage(e.target.value)}
|
|
className="w-full mt-1 p-2 bg-background border border-border rounded-lg text-sm"
|
|
>
|
|
{STAGES.map((s) => (
|
|
<option key={s} value={s}>
|
|
{s.replace('_', ' ')}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
|
|
<div className="flex justify-end gap-3 pt-4 border-t border-border">
|
|
<button
|
|
type="button"
|
|
onClick={() => setIsModalOpen(false)}
|
|
className="px-4 py-2 border border-border rounded-lg text-xs font-semibold hover:bg-muted"
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
className="px-4 py-2 bg-primary text-white rounded-lg text-xs font-semibold hover:bg-primary/90"
|
|
>
|
|
Save Deal
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|