feat: implement dashboard screens for carnet and termination tracking with navigation and charts
This commit is contained in:
@@ -0,0 +1,298 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { supabase } from '../services/supabaseClient';
|
||||
import { PieChart, Pie, Cell, Tooltip, Legend, ResponsiveContainer } from 'recharts';
|
||||
import NavBar from '../components/NavBar';
|
||||
|
||||
const GLM_COLORS = { positivo: '#6CC24A', neutro: '#F0C75E', negativo: '#FF6A13' };
|
||||
const CHART_PALETTE = ['#6CC24A', '#4F758B', '#A4D65E', '#5B7F95', '#C4D600', '#6B8FA3'];
|
||||
|
||||
export default function Dashboard() {
|
||||
const [rawData, setRawData] = useState([]);
|
||||
const [filteredData, setFilteredData] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
const [posiciones, setPosiciones] = useState([]);
|
||||
const [homologaciones, setHomologaciones] = useState([]);
|
||||
const [paises, setPaises] = useState([]);
|
||||
|
||||
const [filtroPosicion, setFiltroPosicion] = useState('');
|
||||
const [filtroHomologacion, setFiltroHomologacion] = useState('');
|
||||
const [filtroPais, setFiltroPais] = useState('');
|
||||
|
||||
const [paginaActual, setPaginaActual] = useState(1);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
aplicarFiltros();
|
||||
setPaginaActual(1);
|
||||
}, [filtroPosicion, filtroHomologacion, filtroPais, rawData]);
|
||||
|
||||
const fetchData = async () => {
|
||||
setLoading(true);
|
||||
const { data, error } = await supabase
|
||||
.from('encuesta_terminacion_glm')
|
||||
.select('*')
|
||||
.order('timestamp', { ascending: false });
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching data:', error);
|
||||
} else {
|
||||
setRawData(data);
|
||||
const uniquePosiciones = [...new Set(data.map(item => item.posicion).filter(Boolean))];
|
||||
const uniqueHomologaciones = [...new Set(data.map(item => item.homologacion_ia).filter(Boolean))];
|
||||
const uniquePaises = [...new Set(data.map(item => item.pais).filter(Boolean))];
|
||||
setPosiciones(uniquePosiciones);
|
||||
setHomologaciones(uniqueHomologaciones);
|
||||
setPaises(uniquePaises);
|
||||
}
|
||||
setLoading(false);
|
||||
};
|
||||
|
||||
const aplicarFiltros = () => {
|
||||
let result = rawData;
|
||||
if (filtroPosicion) result = result.filter(item => item.posicion === filtroPosicion);
|
||||
if (filtroHomologacion) result = result.filter(item => item.homologacion_ia === filtroHomologacion);
|
||||
if (filtroPais) result = result.filter(item => item.pais === filtroPais);
|
||||
|
||||
// Ordenar por fecha de más reciente a más vieja
|
||||
result = [...result].sort((a, b) => {
|
||||
const dateA = a.timestamp ? new Date(a.timestamp) : 0;
|
||||
const dateB = b.timestamp ? new Date(b.timestamp) : 0;
|
||||
return dateB - dateA;
|
||||
});
|
||||
|
||||
setFilteredData(result);
|
||||
};
|
||||
|
||||
const filasPorPagina = 20;
|
||||
const totalPaginas = Math.ceil(filteredData.length / filasPorPagina) || 1;
|
||||
const indiceUltimoItem = paginaActual * filasPorPagina;
|
||||
const indicePrimerItem = indiceUltimoItem - filasPorPagina;
|
||||
const datosPaginados = filteredData.slice(indicePrimerItem, indiceUltimoItem);
|
||||
|
||||
const totalRegistros = filteredData.length;
|
||||
const regresarian = filteredData.filter(d => d.considerar_regresar).length;
|
||||
const recomendarian = filteredData.filter(d => d.siento_comodo_recomendando).length;
|
||||
|
||||
const sentimientoCount = filteredData.reduce((acc, curr) => {
|
||||
const sent = curr.sentimiento_ia || 'neutro';
|
||||
acc[sent] = (acc[sent] || 0) + 1;
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const chartDataSentimiento = Object.keys(sentimientoCount).map(key => ({
|
||||
name: key,
|
||||
value: sentimientoCount[key]
|
||||
}));
|
||||
|
||||
if (loading) return <div className="p-10 text-center text-xl font-semibold text-azul-glm">Cargando datos...</div>;
|
||||
|
||||
return (
|
||||
<div className="p-6 max-w-7xl mx-auto min-h-screen">
|
||||
|
||||
{/* HEADER GLM */}
|
||||
<header className="flex flex-col sm:flex-row items-center gap-4 mb-8 bg-white p-4 rounded-xl border border-gris-medio/30 shadow-sm">
|
||||
<div className="flex-shrink-0 bg-verde-bg p-2 rounded-lg">
|
||||
<img
|
||||
src="https://dbit.digitalcompass.agency/storage/v1/object/public/public-assets/GLM_completo.png"
|
||||
alt="Logo GLM"
|
||||
className="h-12 w-auto object-contain max-w-[180px]"
|
||||
/>
|
||||
</div>
|
||||
<div className="text-center sm:text-left flex-1">
|
||||
<h1 className="text-2xl font-bold text-azul-glm tracking-tight">Dashboard de Estadísticas</h1>
|
||||
<p className="text-xs text-acero italic">Métricas analíticas de entrevistas de salida</p>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
{/* NAVEGACIÓN GLM */}
|
||||
<NavBar />
|
||||
|
||||
{/* FILTROS GLM */}
|
||||
<div className="flex flex-col md:flex-row gap-4 mb-8 bg-white p-5 rounded-xl border border-gris-medio/30 shadow-sm">
|
||||
<div className="flex-1">
|
||||
<label className="block text-xs font-bold text-azul-glm uppercase tracking-wider mb-1">Filtrar por Posición</label>
|
||||
<select
|
||||
className="w-full rounded-lg p-2.5 border text-sm text-gris-oscuro bg-amarillo-input focus:bg-white focus:ring-2 focus:ring-verde-glm focus:outline-none"
|
||||
value={filtroPosicion}
|
||||
onChange={(e) => setFiltroPosicion(e.target.value)}
|
||||
>
|
||||
<option value="">Todas las posiciones</option>
|
||||
{posiciones.map((pos, idx) => (
|
||||
<option key={idx} value={pos}>{pos}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div className="flex-1">
|
||||
<label className="block text-xs font-bold text-azul-glm uppercase tracking-wider mb-1">Filtrar por Motivo</label>
|
||||
<select
|
||||
className="w-full rounded-lg p-2.5 border text-sm text-gris-oscuro bg-amarillo-input focus:bg-white focus:ring-2 focus:ring-verde-glm focus:outline-none"
|
||||
value={filtroHomologacion}
|
||||
onChange={(e) => setFiltroHomologacion(e.target.value)}
|
||||
>
|
||||
<option value="">Todos los motivos</option>
|
||||
{homologaciones.map((hom, idx) => (
|
||||
<option key={idx} value={hom}>{hom}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div className="flex-1">
|
||||
<label className="block text-xs font-bold text-azul-glm uppercase tracking-wider mb-1">Filtrar por País</label>
|
||||
<select
|
||||
className="w-full rounded-lg p-2.5 border text-sm text-gris-oscuro bg-amarillo-input focus:bg-white focus:ring-2 focus:ring-verde-glm focus:outline-none"
|
||||
value={filtroPais}
|
||||
onChange={(e) => setFiltroPais(e.target.value)}
|
||||
>
|
||||
<option value="">Todos los países</option>
|
||||
{paises.map((pais, idx) => (
|
||||
<option key={idx} value={pais}>{pais}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div className="flex items-end">
|
||||
<button
|
||||
onClick={() => { setFiltroPosicion(''); setFiltroHomologacion(''); setFiltroPais(''); }}
|
||||
className="w-full md:w-auto bg-verde-bg hover:bg-verde-glm hover:text-white text-azul-glm font-semibold text-sm py-2.5 px-5 rounded-lg border border-verde-glm/40 hover:border-verde-glm transition-colors"
|
||||
>
|
||||
Limpiar Filtros
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* KPIs GLM */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
|
||||
<div className="bg-white p-6 rounded-xl border border-gris-medio/30 shadow-sm border-l-[6px] border-azul-glm">
|
||||
<h2 className="text-xs text-acero uppercase font-bold tracking-wider">Total Registros</h2>
|
||||
<p className="text-3xl font-extrabold text-azul-glm mt-1">{totalRegistros}</p>
|
||||
</div>
|
||||
<div className="bg-white p-6 rounded-xl border border-gris-medio/30 shadow-sm border-l-[6px] border-verde-glm">
|
||||
<h2 className="text-xs text-acero uppercase font-bold tracking-wider">Consideraría Regresar</h2>
|
||||
<p className="text-3xl font-extrabold text-verde-glm mt-1">
|
||||
{totalRegistros > 0 ? Math.round((regresarian / totalRegistros) * 100) : 0}%
|
||||
</p>
|
||||
<p className="text-xs text-acero mt-1">{regresarian} empleados interesados</p>
|
||||
</div>
|
||||
<div className="bg-white p-6 rounded-xl border border-gris-medio/30 shadow-sm border-l-[6px] border-azul-medio">
|
||||
<h2 className="text-xs text-acero uppercase font-bold tracking-wider">Recomendaría la Empresa</h2>
|
||||
<p className="text-3xl font-extrabold text-azul-medio mt-1">
|
||||
{totalRegistros > 0 ? Math.round((recomendarian / totalRegistros) * 100) : 0}%
|
||||
</p>
|
||||
<p className="text-xs text-acero mt-1">{recomendarian} empleados satisfechos</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* GRÁFICA GLM */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
|
||||
<div className="bg-white p-6 rounded-xl border border-gris-medio/30 shadow-sm">
|
||||
<h3 className="text-base font-bold text-azul-glm mb-4 pb-2 border-b-2 border-verde-glm">Sentimiento del Motivo</h3>
|
||||
<div className="h-64">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<PieChart>
|
||||
<Pie
|
||||
data={chartDataSentimiento}
|
||||
cx="50%"
|
||||
cy="50%"
|
||||
outerRadius={80}
|
||||
fill="#8884d8"
|
||||
dataKey="value"
|
||||
label={({ name, percent }) => `${name} ${(percent * 100).toFixed(0)}%`}
|
||||
>
|
||||
{chartDataSentimiento.map((entry, index) => (
|
||||
<Cell key={`cell-${index}`} fill={GLM_COLORS[entry.name.toLowerCase()] || CHART_PALETTE[index % CHART_PALETTE.length]} />
|
||||
))}
|
||||
</Pie>
|
||||
<Tooltip />
|
||||
<Legend />
|
||||
</PieChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* TABLA GLM */}
|
||||
<div className="bg-white rounded-xl border border-gris-medio/30 shadow-sm overflow-hidden">
|
||||
<div className="px-6 py-4 border-b border-gris-medio/30 bg-verde-bg">
|
||||
<h3 className="text-base font-bold text-azul-glm">Detalle de Registros</h3>
|
||||
</div>
|
||||
<div className="overflow-x-auto">
|
||||
<table className="min-w-full divide-y divide-gris-medio/50 text-sm">
|
||||
<thead className="bg-azul-glm">
|
||||
<tr>
|
||||
<th className="px-6 py-3 text-left text-xs font-bold text-white uppercase tracking-wider">Fecha</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-bold text-white uppercase tracking-wider">País</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-bold text-white uppercase tracking-wider">Posición</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-bold text-white uppercase tracking-wider">Motivo</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-bold text-white uppercase tracking-wider">Sentimiento</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="bg-white divide-y divide-gris-medio/30">
|
||||
{datosPaginados.map((item, idx) => (
|
||||
<tr key={idx} className={`${idx % 2 === 0 ? 'bg-white' : 'bg-gris-claro'} hover:bg-verde-bg/50 transition-colors`}>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-acero">
|
||||
{new Date(item.timestamp).toLocaleDateString()}
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap font-semibold text-gris-oscuro">{item.pais}</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-gris-oscuro">{item.posicion}</td>
|
||||
<td className="px-6 py-4 text-gris-oscuro max-w-xs truncate">{item.homologacion_ia}</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap">
|
||||
{(() => {
|
||||
const sent = String(item.sentimiento_ia || '').toLowerCase().trim();
|
||||
let classes = 'bg-gray-100 text-gray-800';
|
||||
if (sent === 'positivo') {
|
||||
classes = 'bg-verde-ok text-green-700';
|
||||
} else if (sent === 'negativo') {
|
||||
classes = 'bg-rojo-suave text-red-600';
|
||||
} else if (sent === 'neutro') {
|
||||
classes = 'bg-amarillo-medio text-yellow-700';
|
||||
}
|
||||
return (
|
||||
<span className={`px-2.5 py-1 inline-flex text-xs leading-5 font-bold rounded-full ${classes}`}>
|
||||
{item.sentimiento_ia}
|
||||
</span>
|
||||
);
|
||||
})()}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{/* Controles de Pagina */}
|
||||
<div className="flex flex-col sm:flex-row items-center justify-between gap-4 px-6 py-4 border-t border-gris-medio/30 bg-gris-claro/50">
|
||||
<div className="text-xs text-acero font-medium">
|
||||
{totalRegistros > 0
|
||||
? `Mostrando registros ${indicePrimerItem + 1} - ${Math.min(indiceUltimoItem, totalRegistros)} de ${totalRegistros}`
|
||||
: 'No hay registros para mostrar'
|
||||
}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
onClick={() => setPaginaActual(prev => Math.max(prev - 1, 1))}
|
||||
disabled={paginaActual === 1}
|
||||
className="px-3 py-1.5 rounded-lg border border-gris-medio text-xs font-semibold text-azul-glm bg-white hover:bg-gris-claro disabled:opacity-50 disabled:cursor-not-allowed transition-all cursor-pointer"
|
||||
>
|
||||
Atrás
|
||||
</button>
|
||||
<span className="text-xs text-gris-oscuro font-bold">
|
||||
Página {paginaActual} de {totalPaginas}
|
||||
</span>
|
||||
<button
|
||||
onClick={() => setPaginaActual(prev => Math.min(prev + 1, totalPaginas))}
|
||||
disabled={paginaActual === totalPaginas}
|
||||
className="px-3 py-1.5 rounded-lg border border-gris-medio text-xs font-semibold text-azul-glm bg-white hover:bg-gris-claro disabled:opacity-50 disabled:cursor-not-allowed transition-all cursor-pointer"
|
||||
>
|
||||
Siguiente
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user