- 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
352 lines
14 KiB
TypeScript
352 lines
14 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
import { crmTaskCalendarService, CrmCalendarEvent } from '@/services/api/crmTaskCalendarService';
|
|
import { systemConfigService, CrmLocation } from '@/services/api/systemConfigService';
|
|
import { Calendar as CalendarIcon, Plus, ChevronLeft, ChevronRight, Clock, Store, X, CheckCircle2, Trash2 } from 'lucide-react';
|
|
import { toast } from 'sonner';
|
|
|
|
export default function CrmCalendarPage() {
|
|
const [events, setEvents] = useState<CrmCalendarEvent[]>([]);
|
|
const [locations, setLocations] = useState<CrmLocation[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
// Month navigation
|
|
const [currentDate, setCurrentDate] = useState(new Date());
|
|
|
|
// Modal Create State
|
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
const [title, setTitle] = useState('');
|
|
const [description, setDescription] = useState('');
|
|
const [locationId, setLocationId] = useState('');
|
|
const [startTime, setStartTime] = useState('');
|
|
const [endTime, setEndTime] = useState('');
|
|
const [isAllDay, setIsAllDay] = useState(false);
|
|
const [sourceType, setSourceType] = useState('MANUAL');
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
useEffect(() => {
|
|
loadData();
|
|
}, []);
|
|
|
|
const loadData = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const [evtData, locData] = await Promise.all([
|
|
crmTaskCalendarService.getCalendarEvents(),
|
|
systemConfigService.getLocations(),
|
|
]);
|
|
setEvents(Array.isArray(evtData) ? evtData : []);
|
|
setLocations(Array.isArray(locData) ? locData : []);
|
|
} catch {
|
|
toast.error('Failed to load CRM calendar events');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleCreateEvent = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!title.trim() || !startTime || !endTime) {
|
|
toast.error('Title, start time, and end time are required');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setIsSubmitting(true);
|
|
await crmTaskCalendarService.createCalendarEvent({
|
|
title: title.trim(),
|
|
description: description || undefined,
|
|
location_id: locationId || undefined,
|
|
start_time: new Date(startTime).toISOString(),
|
|
end_time: new Date(endTime).toISOString(),
|
|
is_all_day: isAllDay,
|
|
source_type: sourceType,
|
|
status: 'SCHEDULED',
|
|
});
|
|
toast.success('Calendar event scheduled!');
|
|
setIsModalOpen(false);
|
|
setTitle('');
|
|
setDescription('');
|
|
setLocationId('');
|
|
setStartTime('');
|
|
setEndTime('');
|
|
setIsAllDay(false);
|
|
loadData();
|
|
} catch (err: any) {
|
|
toast.error(err?.message || 'Failed to schedule calendar event');
|
|
} finally {
|
|
setIsSubmitting(false);
|
|
}
|
|
};
|
|
|
|
const handleDeleteEvent = async (eventId: string) => {
|
|
if (!confirm('Are you sure you want to delete this event?')) return;
|
|
try {
|
|
await crmTaskCalendarService.deleteCalendarEvent(eventId);
|
|
toast.success('Calendar event removed');
|
|
loadData();
|
|
} catch {
|
|
toast.error('Failed to delete event');
|
|
}
|
|
};
|
|
|
|
const handlePrevMonth = () => {
|
|
setCurrentDate(new Date(currentDate.getFullYear(), currentDate.getMonth() - 1, 1));
|
|
};
|
|
|
|
const handleNextMonth = () => {
|
|
setCurrentDate(new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 1));
|
|
};
|
|
|
|
const year = currentDate.getFullYear();
|
|
const month = currentDate.getMonth();
|
|
const monthName = currentDate.toLocaleString('default', { month: 'long' });
|
|
|
|
const firstDayOfMonth = new Date(year, month, 1).getDay();
|
|
const daysInMonth = new Date(year, month + 1, 0).getDate();
|
|
|
|
const daysArray = Array.from({ length: daysInMonth }, (_, i) => i + 1);
|
|
|
|
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">
|
|
<CalendarIcon className="w-6 h-6 text-primary" />
|
|
CRM Interactive Calendar Engine
|
|
</h1>
|
|
<p className="text-sm text-muted-foreground">
|
|
Schedule customer service appointments, repair pick-ups, team meetings, and quotation follow-ups.
|
|
</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" />
|
|
Schedule Event
|
|
</button>
|
|
</div>
|
|
|
|
{/* Calendar Header Navigation */}
|
|
<div className="flex items-center justify-between bg-card border border-border p-4 rounded-xl shadow-xs">
|
|
<div className="flex items-center gap-3">
|
|
<h2 className="text-lg font-bold text-foreground">
|
|
{monthName} {year}
|
|
</h2>
|
|
<span className="text-xs font-semibold px-2.5 py-1 rounded-md bg-primary/10 text-primary">
|
|
{events.length} Events Scheduled
|
|
</span>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-2">
|
|
<button
|
|
onClick={handlePrevMonth}
|
|
className="p-2 rounded-lg border border-border bg-card hover:bg-muted text-foreground cursor-pointer"
|
|
>
|
|
<ChevronLeft className="w-4 h-4" />
|
|
</button>
|
|
<button
|
|
onClick={() => setCurrentDate(new Date())}
|
|
className="px-3 py-1.5 rounded-lg border border-border text-xs font-bold bg-card hover:bg-muted text-foreground cursor-pointer"
|
|
>
|
|
Today
|
|
</button>
|
|
<button
|
|
onClick={handleNextMonth}
|
|
className="p-2 rounded-lg border border-border bg-card hover:bg-muted text-foreground cursor-pointer"
|
|
>
|
|
<ChevronRight className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Month View Grid */}
|
|
<div className="bg-card border border-border rounded-xl shadow-xs overflow-hidden">
|
|
{/* Days of week header */}
|
|
<div className="grid grid-cols-7 border-b border-border bg-muted/40 text-center text-xs font-bold text-muted-foreground py-2.5">
|
|
<div>SUN</div>
|
|
<div>MON</div>
|
|
<div>TUE</div>
|
|
<div>WED</div>
|
|
<div>THU</div>
|
|
<div>FRI</div>
|
|
<div>SAT</div>
|
|
</div>
|
|
|
|
{/* Calendar Cells */}
|
|
<div className="grid grid-cols-7 auto-rows-fr gap-px bg-border">
|
|
{/* Empty cells before month start */}
|
|
{Array.from({ length: firstDayOfMonth }).map((_, idx) => (
|
|
<div key={`empty-${idx}`} className="bg-muted/10 min-h-[110px] p-2" />
|
|
))}
|
|
|
|
{/* Month Day Cells */}
|
|
{daysArray.map((dayNum) => {
|
|
const cellDateStr = `${year}-${String(month + 1).padStart(2, '0')}-${String(dayNum).padStart(2, '0')}`;
|
|
const isToday = new Date().toDateString() === new Date(year, month, dayNum).toDateString();
|
|
|
|
const dayEvents = events.filter((e) => {
|
|
if (!e.start_time) return false;
|
|
const d = new Date(e.start_time);
|
|
return d.getFullYear() === year && d.getMonth() === month && d.getDate() === dayNum;
|
|
});
|
|
|
|
return (
|
|
<div key={`day-${dayNum}`} className={`bg-card min-h-[110px] p-2 space-y-1.5 transition-colors hover:bg-muted/20 ${isToday ? 'bg-primary/5' : ''}`}>
|
|
<div className="flex items-center justify-between">
|
|
<span className={`text-xs font-bold w-6 h-6 rounded-full flex items-center justify-center ${isToday ? 'bg-primary text-white' : 'text-foreground'}`}>
|
|
{dayNum}
|
|
</span>
|
|
{dayEvents.length > 0 && (
|
|
<span className="text-[10px] font-bold text-muted-foreground">{dayEvents.length}</span>
|
|
)}
|
|
</div>
|
|
|
|
<div className="space-y-1 overflow-y-auto max-h-[80px]">
|
|
{dayEvents.map((evt) => {
|
|
const locName = locations.find((l) => l.location_id === evt.location_id)?.name;
|
|
return (
|
|
<div
|
|
key={evt.event_id}
|
|
className="p-1.5 rounded-md bg-primary/10 border border-primary/20 text-[11px] font-medium text-foreground flex items-center justify-between gap-1 group"
|
|
>
|
|
<div className="min-w-0">
|
|
<p className="font-semibold truncate">{evt.title}</p>
|
|
<p className="text-[9px] text-muted-foreground truncate">
|
|
{new Date(evt.start_time).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
|
|
{locName ? ` · ${locName}` : ''}
|
|
</p>
|
|
</div>
|
|
<button
|
|
onClick={() => handleDeleteEvent(evt.event_id)}
|
|
className="opacity-0 group-hover:opacity-100 text-rose-600 hover:text-rose-700 transition-opacity p-0.5"
|
|
title="Delete event"
|
|
>
|
|
<Trash2 className="w-3 h-3" />
|
|
</button>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Modal: Schedule Event */}
|
|
{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">
|
|
<CalendarIcon className="w-5 h-5 text-primary" />
|
|
Schedule Calendar Event
|
|
</h3>
|
|
<button onClick={() => setIsModalOpen(false)} className="text-muted-foreground hover:text-foreground">
|
|
<X className="w-5 h-5" />
|
|
</button>
|
|
</div>
|
|
|
|
<form onSubmit={handleCreateEvent} className="space-y-4 text-xs">
|
|
<div>
|
|
<label className="block font-medium mb-1">Event Title *</label>
|
|
<input
|
|
type="text"
|
|
required
|
|
value={title}
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
placeholder="e.g. VIP Customer Quotation Consultation"
|
|
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="Event agenda and customer notes..."
|
|
className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden h-20"
|
|
/>
|
|
</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 className="grid grid-cols-2 gap-3">
|
|
<div>
|
|
<label className="block font-medium mb-1">Start Time *</label>
|
|
<input
|
|
type="datetime-local"
|
|
required
|
|
value={startTime}
|
|
onChange={(e) => setStartTime(e.target.value)}
|
|
className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block font-medium mb-1">End Time *</label>
|
|
<input
|
|
type="datetime-local"
|
|
required
|
|
value={endTime}
|
|
onChange={(e) => setEndTime(e.target.value)}
|
|
className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-2">
|
|
<input
|
|
type="checkbox"
|
|
id="allDayCheck"
|
|
checked={isAllDay}
|
|
onChange={(e) => setIsAllDay(e.target.checked)}
|
|
className="rounded border-border"
|
|
/>
|
|
<label htmlFor="allDayCheck" className="text-foreground cursor-pointer">
|
|
All-day Event
|
|
</label>
|
|
</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 ? 'Scheduling...' : 'Schedule Event'}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|