ifixkart-admin/New admin panel/ikixkart-dashboard/app/(admin)/crm-todos/page.tsx
Adithiyan bbd3a3e959 feat: initial commit — full platform snapshot
- 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
2026-10-13 12:59:47 +05:30

397 lines
16 KiB
TypeScript

'use client';
import { useEffect, useState } from 'react';
import { crmTaskCalendarService, CrmTask } from '@/services/api/crmTaskCalendarService';
import { systemConfigService, CrmLocation } from '@/services/api/systemConfigService';
import { CheckSquare, Plus, Filter, Calendar as CalendarIcon, Clock, CheckCircle2, Circle, AlertCircle, X, Trash2 } from 'lucide-react';
import { toast } from 'sonner';
export default function CrmTodosPage() {
const [tasks, setTasks] = useState<CrmTask[]>([]);
const [locations, setLocations] = useState<CrmLocation[]>([]);
const [loading, setLoading] = useState(true);
// Filters
const [filterStatus, setFilterStatus] = useState<string>('ALL');
const [filterPriority, setFilterPriority] = useState<string>('ALL');
const [filterLocationId, setFilterLocationId] = useState<string>('ALL');
// Modal Create State
const [isModalOpen, setIsModalOpen] = useState(false);
const [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [locationId, setLocationId] = useState<string>('');
const [priority, setPriority] = useState<string>('MEDIUM');
const [entityType, setEntityType] = useState<string>('');
const [entityId, setEntityId] = useState<string>('');
const [dueAt, setDueAt] = useState<string>('');
const [isSubmitting, setIsSubmitting] = useState(false);
useEffect(() => {
loadData();
}, []);
const loadData = async () => {
try {
setLoading(true);
const [taskData, locData] = await Promise.all([
crmTaskCalendarService.getTasks(),
systemConfigService.getLocations(),
]);
setTasks(Array.isArray(taskData) ? taskData : []);
setLocations(Array.isArray(locData) ? locData : []);
} catch {
toast.error('Failed to load CRM tasks');
} finally {
setLoading(false);
}
};
const handleCreateTask = async (e: React.FormEvent) => {
e.preventDefault();
if (!title.trim()) {
toast.error('Task title is required');
return;
}
try {
setIsSubmitting(true);
await crmTaskCalendarService.createTask({
title: title.trim(),
description: description || undefined,
location_id: locationId || undefined,
priority: priority,
status: 'PENDING',
entity_type: entityType || undefined,
entity_id: entityId || undefined,
due_at: dueAt ? new Date(dueAt).toISOString() : undefined,
});
toast.success('CRM Task created successfully!');
setIsModalOpen(false);
setTitle('');
setDescription('');
setLocationId('');
setPriority('MEDIUM');
setEntityType('');
setEntityId('');
setDueAt('');
loadData();
} catch (err: any) {
toast.error(err?.message || 'Failed to create task');
} finally {
setIsSubmitting(false);
}
};
const handleToggleStatus = async (task: CrmTask) => {
const newStatus = task.status === 'COMPLETED' ? 'PENDING' : 'COMPLETED';
try {
await crmTaskCalendarService.updateTask(task.task_id, { status: newStatus });
toast.success(`Task marked as ${newStatus}`);
loadData();
} catch {
toast.error('Failed to update task status');
}
};
const handleDeleteTask = async (taskId: string) => {
if (!confirm('Are you sure you want to delete this task?')) return;
try {
await crmTaskCalendarService.deleteTask(taskId);
toast.success('Task deleted');
loadData();
} catch {
toast.error('Failed to delete task');
}
};
const filteredTasks = tasks.filter((t) => {
if (filterStatus !== 'ALL' && t.status !== filterStatus) return false;
if (filterPriority !== 'ALL' && t.priority !== filterPriority) return false;
if (filterLocationId !== 'ALL' && t.location_id !== filterLocationId) return false;
return true;
});
return (
<div className="p-6 space-y-6">
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4">
<div>
<h1 className="text-2xl font-bold tracking-tight text-foreground flex items-center gap-2">
<CheckSquare className="w-6 h-6 text-primary" />
CRM Actionable To-Do & Task Engine
</h1>
<p className="text-sm text-muted-foreground">
Manage operational follow-ups, repair ticket tasks, customer lead calls, and store assignments.
</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 cursor-pointer"
>
<Plus className="w-4 h-4" />
Create New Task
</button>
</div>
{/* Filter Bar */}
<div className="flex flex-wrap items-center gap-3 bg-card border border-border p-3.5 rounded-xl shadow-xs">
<div className="flex items-center gap-1.5 text-xs text-muted-foreground font-semibold uppercase mr-2">
<Filter className="w-3.5 h-3.5" /> Filters:
</div>
<div>
<select
value={filterStatus}
onChange={(e) => setFilterStatus(e.target.value)}
className="bg-muted border border-border rounded-lg px-3 py-1.5 text-xs font-semibold text-foreground focus:outline-hidden"
>
<option value="ALL">All Statuses</option>
<option value="PENDING">Pending Tasks</option>
<option value="COMPLETED">Completed Tasks</option>
</select>
</div>
<div>
<select
value={filterPriority}
onChange={(e) => setFilterPriority(e.target.value)}
className="bg-muted border border-border rounded-lg px-3 py-1.5 text-xs font-semibold text-foreground focus:outline-hidden"
>
<option value="ALL">All Priorities</option>
<option value="HIGH">High Priority</option>
<option value="MEDIUM">Medium Priority</option>
<option value="LOW">Low Priority</option>
</select>
</div>
<div>
<select
value={filterLocationId}
onChange={(e) => setFilterLocationId(e.target.value)}
className="bg-muted border border-border rounded-lg px-3 py-1.5 text-xs font-semibold text-foreground focus:outline-hidden"
>
<option value="ALL">All Store Locations</option>
{locations.map((loc) => (
<option key={loc.location_id} value={loc.location_id}>
{loc.name}
</option>
))}
</select>
</div>
</div>
{/* Task Cards List */}
{loading ? (
<div className="py-20 text-center text-muted-foreground animate-pulse">Loading CRM Tasks...</div>
) : filteredTasks.length === 0 ? (
<div className="py-16 text-center bg-card border border-border rounded-xl">
<CheckSquare className="w-10 h-10 text-muted-foreground mx-auto mb-2 opacity-50" />
<h3 className="text-sm font-semibold text-foreground">No matching CRM tasks found</h3>
<p className="text-xs text-muted-foreground mt-1">Click "+ Create New Task" to schedule operational action items.</p>
</div>
) : (
<div className="space-y-3">
{filteredTasks.map((task) => {
const locName = locations.find((l) => l.location_id === task.location_id)?.name;
const isDone = task.status === 'COMPLETED';
return (
<div
key={task.task_id}
className={`p-4 border rounded-xl bg-card transition-all flex items-start justify-between gap-4 ${
isDone ? 'border-border opacity-70 bg-muted/20' : 'border-border hover:border-primary/50 shadow-xs'
}`}
>
<div className="flex items-start gap-3">
<button
onClick={() => handleToggleStatus(task)}
className="mt-0.5 text-muted-foreground hover:text-primary transition-colors cursor-pointer"
>
{isDone ? <CheckCircle2 className="w-5 h-5 text-emerald-600" /> : <Circle className="w-5 h-5" />}
</button>
<div className="space-y-1">
<div className="flex flex-wrap items-center gap-2">
<h4 className={`text-sm font-semibold ${isDone ? 'line-through text-muted-foreground' : 'text-foreground'}`}>
{task.title}
</h4>
<span
className={`px-2 py-0.5 rounded-full text-[10px] font-bold uppercase ${
task.priority === 'HIGH'
? 'bg-rose-500/10 text-rose-600'
: task.priority === 'MEDIUM'
? 'bg-amber-500/10 text-amber-600'
: 'bg-blue-500/10 text-blue-600'
}`}
>
{task.priority} Priority
</span>
{locName && (
<span className="px-2 py-0.5 rounded-full text-[10px] font-medium bg-muted text-muted-foreground">
📍 {locName}
</span>
)}
{task.entity_type && (
<span className="px-2 py-0.5 rounded-full text-[10px] font-mono bg-primary/10 text-primary">
{task.entity_type}: {task.entity_id || 'Linked'}
</span>
)}
</div>
{task.description && <p className="text-xs text-muted-foreground">{task.description}</p>}
{task.due_at && (
<div className="flex items-center gap-1 text-[11px] text-muted-foreground pt-1">
<Clock className="w-3.5 h-3.5 text-amber-500" />
<span>Due: {new Date(task.due_at).toLocaleString()}</span>
</div>
)}
</div>
</div>
<button
onClick={() => handleDeleteTask(task.task_id)}
className="text-muted-foreground hover:text-rose-600 transition-colors p-1.5 cursor-pointer"
title="Delete task"
>
<Trash2 className="w-4 h-4" />
</button>
</div>
);
})}
</div>
)}
{/* Modal: Create Task */}
{isModalOpen && (
<div className="fixed inset-0 z-50 bg-black/50 backdrop-blur-xs flex items-center justify-center p-4">
<div className="bg-card border border-border rounded-xl shadow-xl max-w-md w-full p-6 space-y-4">
<div className="flex items-center justify-between">
<h3 className="text-lg font-bold text-foreground flex items-center gap-2">
<CheckSquare className="w-5 h-5 text-primary" />
Create CRM Task
</h3>
<button onClick={() => setIsModalOpen(false)} className="text-muted-foreground hover:text-foreground">
<X className="w-5 h-5" />
</button>
</div>
<form onSubmit={handleCreateTask} className="space-y-4 text-xs">
<div>
<label className="block font-medium mb-1">Task Title *</label>
<input
type="text"
required
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="e.g. Call customer for laptop screen replacement approval"
className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden font-semibold"
/>
</div>
<div>
<label className="block font-medium mb-1">Description</label>
<textarea
value={description}
onChange={(e) => setDescription(e.target.value)}
placeholder="Task instructions and detailed background..."
className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden h-20"
/>
</div>
<div className="grid grid-cols-2 gap-3">
<div>
<label className="block font-medium mb-1">Priority</label>
<select
value={priority}
onChange={(e) => setPriority(e.target.value)}
className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden"
>
<option value="LOW">Low</option>
<option value="MEDIUM">Medium</option>
<option value="HIGH">High</option>
</select>
</div>
<div>
<label className="block font-medium mb-1">Store Location</label>
<select
value={locationId}
onChange={(e) => setLocationId(e.target.value)}
className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden"
>
<option value="">-- Universal (All Stores) --</option>
{locations.map((loc) => (
<option key={loc.location_id} value={loc.location_id}>
{loc.name}
</option>
))}
</select>
</div>
</div>
<div className="grid grid-cols-2 gap-3">
<div>
<label className="block font-medium mb-1">Linked Entity Type</label>
<select
value={entityType}
onChange={(e) => setEntityType(e.target.value)}
className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden"
>
<option value="">None</option>
<option value="REPAIR_TICKET">Repair Ticket</option>
<option value="LEAD">CRM Lead</option>
<option value="CUSTOMER">Customer</option>
<option value="QUOTATION">Quotation</option>
</select>
</div>
<div>
<label className="block font-medium mb-1">Entity ID</label>
<input
type="text"
value={entityId}
onChange={(e) => setEntityId(e.target.value)}
placeholder="e.g. REP-1002"
className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden font-mono"
/>
</div>
</div>
<div>
<label className="block font-medium mb-1">Due Date & Time</label>
<input
type="datetime-local"
value={dueAt}
onChange={(e) => setDueAt(e.target.value)}
className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden"
/>
</div>
<div className="pt-2 flex justify-end gap-2">
<button
type="button"
onClick={() => setIsModalOpen(false)}
className="px-4 py-2 rounded-lg border border-border font-medium hover:bg-muted"
>
Cancel
</button>
<button
type="submit"
disabled={isSubmitting}
className="px-4 py-2 rounded-lg bg-primary text-white font-bold hover:bg-primary/90 flex items-center gap-1.5 cursor-pointer"
>
{isSubmitting ? 'Creating...' : 'Create Task'}
</button>
</div>
</form>
</div>
</div>
)}
</div>
);
}