Files
cotizador/web/src/pages/app/DashboardPage.tsx
T

207 lines
7.1 KiB
TypeScript

import { useCallback, useEffect, useState } from "react";
import { Link } from "react-router-dom";
import { FileText, Plus, Users, Database } from "lucide-react";
import { formatCurrency, formatDate } from "@/lib/calculators";
import { EstadoBadge } from "@/components/EstadoBadge";
import { LoadingScreen, PageError } from "@/components/ui/LoadingScreen";
interface DashboardCotizacion {
id: string;
numero: string;
fecha: string;
estado: string;
cliente: { nombre: string; empresa: string | null };
servicios: { tipoPago: string; seleccionado: boolean; precio: number }[];
}
interface DashboardData {
totalCotizaciones: number;
totalClientes: number;
totalServicios: number;
cotizacionesRecientes: DashboardCotizacion[];
}
export function DashboardPage() {
const [data, setData] = useState<DashboardData | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const load = useCallback(async () => {
setLoading(true);
setError(null);
try {
const res = await fetch("/api/dashboard", { credentials: "include" });
if (!res.ok) throw new Error("No se pudo cargar el dashboard");
const json: DashboardData = await res.json();
setData(json);
} catch (err) {
setError(err instanceof Error ? err.message : "Error de conexión");
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
load();
}, [load]);
if (loading && !data) return <LoadingScreen />;
if (error || !data) return <PageError message={error || "Error al cargar"} onRetry={load} />;
const stats = [
{
label: "Cotizaciones",
value: data.totalCotizaciones,
icon: FileText,
color: "text-primary",
bg: "bg-primary-light",
},
{
label: "Clientes",
value: data.totalClientes,
icon: Users,
color: "text-secondary",
bg: "bg-slate-100",
},
{
label: "Servicios Activos",
value: data.totalServicios,
icon: Database,
color: "text-accent",
bg: "bg-cyan-100",
},
];
return (
<div>
<div className="flex items-center justify-between mb-8">
<div>
<h1 className="text-2xl font-bold text-foreground">Dashboard</h1>
<p className="text-muted text-sm mt-1">
Bienvenido al Cotizador — Uriel Jareth Consulting
</p>
</div>
<Link
to="/cotizaciones/nueva"
className="flex items-center gap-2 px-4 py-2.5 bg-primary text-white rounded-lg hover:bg-primary-dark transition-colors font-medium text-sm"
>
<Plus className="w-4 h-4" />
Nueva Cotizacion
</Link>
</div>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 mb-8">
{stats.map((stat) => (
<div
key={stat.label}
className="bg-card-bg rounded-xl border border-border p-5"
>
<div className="flex items-center gap-3">
<div className={`${stat.bg} p-2.5 rounded-lg`}>
<stat.icon className={`w-5 h-5 ${stat.color}`} />
</div>
<div>
<p className="text-2xl font-bold">{stat.value}</p>
<p className="text-muted text-sm">{stat.label}</p>
</div>
</div>
</div>
))}
</div>
<div className="bg-card-bg rounded-xl border border-border">
<div className="px-5 py-4 border-b border-border">
<h2 className="font-semibold text-lg">Cotizaciones Recientes</h2>
</div>
{data.cotizacionesRecientes.length === 0 ? (
<div className="p-8 text-center">
<FileText className="w-12 h-12 text-muted mx-auto mb-3" />
<p className="text-muted">
No hay cotizaciones. Crea la primera.
</p>
<Link
to="/cotizaciones/nueva"
className="inline-flex items-center gap-2 mt-4 px-4 py-2 bg-primary text-white rounded-lg text-sm font-medium hover:bg-primary-dark"
>
<Plus className="w-4 h-4" />
Nueva Cotizacion
</Link>
</div>
) : (
<div className="overflow-x-auto">
<table className="w-full text-sm">
<thead>
<tr className="border-b border-border">
<th className="text-left px-5 py-3 font-medium text-muted">
No.
</th>
<th className="text-left px-5 py-3 font-medium text-muted">
Cliente
</th>
<th className="text-left px-5 py-3 font-medium text-muted">
Fecha
</th>
<th className="text-left px-5 py-3 font-medium text-muted">
Estado
</th>
<th className="text-right px-5 py-3 font-medium text-muted">
Servicios
</th>
</tr>
</thead>
<tbody>
{data.cotizacionesRecientes.map((cot) => {
const totalUnico = cot.servicios
.filter((s) => s.tipoPago === "unico" && s.seleccionado)
.reduce((sum, s) => sum + s.precio, 0);
const totalMensual = cot.servicios
.filter(
(s) => s.tipoPago === "mensual" && s.seleccionado
)
.reduce((sum, s) => sum + s.precio, 0);
return (
<tr
key={cot.id}
className="border-b border-border hover:bg-gray-50 transition-colors"
>
<td className="px-5 py-3">
<Link
to={`/cotizaciones/${cot.id}`}
className="text-primary font-medium hover:underline"
>
{cot.numero}
</Link>
</td>
<td className="px-5 py-3">
{cot.cliente.empresa || cot.cliente.nombre}
</td>
<td className="px-5 py-3 text-muted">
{formatDate(new Date(cot.fecha))}
</td>
<td className="px-5 py-3">
<EstadoBadge estado={cot.estado} />
</td>
<td className="px-5 py-3 text-right font-medium">
{totalUnico > 0 && (
<span className="block text-xs text-muted">
Unico: {formatCurrency(totalUnico)}
</span>
)}
{totalMensual > 0 && (
<span className="block text-xs text-muted">
Mensual: {formatCurrency(totalMensual)}
</span>
)}
</td>
</tr>
);
})}
</tbody>
</table>
</div>
)}
</div>
</div>
);
}