ehsan-dashboards/dashboards/crm/app/page.tsx

151 lines
No EOL
4.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// app/page.tsx
'use client';
import React, { useState, useEffect } from 'react';
import { Navbar } from '@/components/Navbar';
import { DashboardTab } from '@/components/DashboardTab';
import { PipelineTab } from '@/components/PipelineTab';
import { TodayTasksTab } from '@/components/TodayTasksTab';
import { OnboardingTab } from '@/components/OnboardingTab';
import { SettingsTab } from '@/components/SettingsTab';
import { LeadModal } from '@/components/LeadModal';
import { NewLeadModal } from '@/components/NewLeadModal';
export default function CrmApp() {
const [activeTab, setActiveTab] = useState<'dashboard' | 'pipeline' | 'today' | 'onboarding' | 'settings'>('dashboard');
const [loading, setLoading] = useState(true);
// Data states
const [stats, setStats] = useState<any>(null);
const [leads, setLeads] = useState<any[]>([]);
const [todayData, setTodayData] = useState<any>(null);
// Modals state
const [selectedLead, setSelectedLead] = useState<any>(null);
const [isLeadModalOpen, setIsLeadModalOpen] = useState(false);
const [isNewLeadModalOpen, setIsNewLeadModalOpen] = useState(false);
// Refresh data function
const fetchAllData = async () => {
try {
const [statsRes, leadsRes, todayRes] = await Promise.all([
fetch('/api/dashboard/stats'),
fetch('/api/leads'),
fetch('/api/tasks/today')
]);
if (statsRes.ok) setStats(await statsRes.json());
if (leadsRes.ok) setLeads(await leadsRes.json());
if (todayRes.ok) setTodayData(await todayRes.json());
} catch (err) {
console.error('Error fetching CRM data:', err);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchAllData();
}, []);
const handleLeadClick = async (lead: any) => {
// Fetch full lead with rooms and activities
try {
const res = await fetch(`/api/leads/${lead.id}`);
if (res.ok) {
const fullLead = await res.json();
setSelectedLead(fullLead);
} else {
setSelectedLead(lead);
}
} catch {
setSelectedLead(lead);
}
setIsLeadModalOpen(true);
};
const handleExportExcel = () => {
window.open('/api/export', '_blank');
};
return (
<div className="min-h-screen bg-slate-50 flex flex-col font-sans selection:bg-teal-100 selection:text-teal-900">
{/* Top Navigation */}
<Navbar
activeTab={activeTab}
setActiveTab={setActiveTab}
onNewLead={() => setIsNewLeadModalOpen(true)}
onExportExcel={handleExportExcel}
onImportSuccess={fetchAllData}
/>
{/* Main Content Area */}
<main className="flex-1 max-w-7xl w-full mx-auto p-4 md:p-6 pb-16">
{loading ? (
<div className="flex flex-col items-center justify-center py-32 space-y-4">
<div className="w-12 h-12 border-4 border-teal-600 border-t-transparent rounded-full animate-spin"></div>
<p className="text-sm font-semibold text-slate-500">در حال بارگذاری اطلاعات CRM اکیپا...</p>
</div>
) : (
<>
{activeTab === 'dashboard' && (
<DashboardTab
stats={stats}
onSelectLead={handleLeadClick}
onNavigateTab={(tab) => setActiveTab(tab as any)}
/>
)}
{activeTab === 'pipeline' && (
<PipelineTab
leads={leads}
onSelectLead={handleLeadClick}
onRefresh={fetchAllData}
/>
)}
{activeTab === 'today' && (
<TodayTasksTab
data={todayData}
onSelectLead={handleLeadClick}
onRefresh={fetchAllData}
/>
)}
{activeTab === 'onboarding' && (
<OnboardingTab
leads={leads}
onSelectLead={handleLeadClick}
onRefresh={fetchAllData}
/>
)}
{activeTab === 'settings' && (
<SettingsTab
stats={stats}
onRefresh={fetchAllData}
/>
)}
</>
)}
</main>
{/* Modals */}
<LeadModal
lead={selectedLead}
isOpen={isLeadModalOpen}
onClose={() => {
setIsLeadModalOpen(false);
setSelectedLead(null);
}}
onRefresh={fetchAllData}
/>
<NewLeadModal
isOpen={isNewLeadModalOpen}
onClose={() => setIsNewLeadModalOpen(false)}
onSuccess={fetchAllData}
/>
</div>
);
}