2025-08-15 17:31:51 -03:00
|
|
|
// src/components/BancasWidget.tsx
|
|
|
|
|
import { useState, useEffect } from 'react';
|
2025-09-02 19:38:04 -03:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
2025-08-25 10:25:54 -03:00
|
|
|
import { ResponsiveBar } from '@nivo/bar';
|
|
|
|
|
import { getBancasPorSeccion, getSeccionesElectorales } from '../apiService';
|
|
|
|
|
import type { ProyeccionBancas, MunicipioSimple } from '../types/types';
|
2025-08-22 21:55:03 -03:00
|
|
|
import './BancasWidget.css';
|
2025-08-15 17:31:51 -03:00
|
|
|
|
2025-08-22 21:55:03 -03:00
|
|
|
const NIVO_COLORS = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"];
|
2025-08-15 17:31:51 -03:00
|
|
|
|
2025-08-22 21:55:03 -03:00
|
|
|
export const BancasWidget = () => {
|
2025-08-25 10:25:54 -03:00
|
|
|
const [secciones, setSecciones] = useState<MunicipioSimple[]>([]);
|
|
|
|
|
const [seccionActual, setSeccionActual] = useState<string>('');
|
2025-08-15 17:31:51 -03:00
|
|
|
|
2025-09-02 19:38:04 -03:00
|
|
|
// useEffect para cargar la lista de secciones una sola vez (esto está bien)
|
2025-08-22 21:55:03 -03:00
|
|
|
useEffect(() => {
|
2025-08-25 10:25:54 -03:00
|
|
|
const fetchSecciones = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const seccionesData = await getSeccionesElectorales();
|
|
|
|
|
if (seccionesData && seccionesData.length > 0) {
|
|
|
|
|
const orden = new Map([
|
|
|
|
|
['Capital', 0], ['Primera', 1], ['Segunda', 2], ['Tercera', 3],
|
|
|
|
|
['Cuarta', 4], ['Quinta', 5], ['Sexta', 6], ['Séptima', 7]
|
|
|
|
|
]);
|
|
|
|
|
const getOrden = (nombre: string) => {
|
|
|
|
|
const match = nombre.match(/Capital|Primera|Segunda|Tercera|Cuarta|Quinta|Sexta|Séptima/);
|
|
|
|
|
return match ? orden.get(match[0]) ?? 99 : 99;
|
|
|
|
|
};
|
|
|
|
|
seccionesData.sort((a, b) => getOrden(a.nombre) - getOrden(b.nombre));
|
|
|
|
|
setSecciones(seccionesData);
|
|
|
|
|
setSeccionActual(seccionesData[0].id);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error cargando secciones electorales:", err);
|
2025-09-02 19:38:04 -03:00
|
|
|
// Opcional: manejar el error en la UI si la carga de secciones falla
|
2025-08-25 10:25:54 -03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
fetchSecciones();
|
|
|
|
|
}, []);
|
|
|
|
|
|
2025-09-02 19:38:04 -03:00
|
|
|
// --- CÓDIGO REFACTORIZADO ---
|
|
|
|
|
// Eliminamos los useState para data, loading y error
|
2025-08-25 10:25:54 -03:00
|
|
|
|
2025-09-02 19:38:04 -03:00
|
|
|
// useQuery ahora es la única fuente de verdad para los datos de bancas
|
|
|
|
|
const {
|
|
|
|
|
data,
|
|
|
|
|
isLoading,
|
|
|
|
|
error
|
|
|
|
|
} = useQuery<ProyeccionBancas, Error>({
|
|
|
|
|
queryKey: ['bancasPorSeccion', seccionActual],
|
|
|
|
|
queryFn: () => getBancasPorSeccion(seccionActual),
|
|
|
|
|
enabled: !!seccionActual,
|
|
|
|
|
retry: (failureCount, error: any) => {
|
|
|
|
|
if (error.response?.status === 404) return false;
|
|
|
|
|
return failureCount < 3;
|
|
|
|
|
},
|
|
|
|
|
});
|
2025-08-22 21:55:03 -03:00
|
|
|
|
2025-08-25 10:25:54 -03:00
|
|
|
const barData = data ? [...data.proyeccion].reverse() : [];
|
2025-08-22 21:55:03 -03:00
|
|
|
|
2025-09-02 19:38:04 -03:00
|
|
|
const getErrorMessage = () => {
|
|
|
|
|
if (error) {
|
|
|
|
|
if ((error as any).response?.status === 404) {
|
|
|
|
|
return "La proyección de bancas para esta sección aún no está disponible.";
|
|
|
|
|
}
|
|
|
|
|
return "No se pudo conectar para obtener los datos de bancas.";
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const errorMessage = getErrorMessage();
|
|
|
|
|
|
2025-08-22 21:55:03 -03:00
|
|
|
return (
|
|
|
|
|
<div className="bancas-widget-container">
|
|
|
|
|
<div className="bancas-header">
|
|
|
|
|
<h4>Distribución de Bancas</h4>
|
2025-08-25 10:25:54 -03:00
|
|
|
<select value={seccionActual} onChange={e => setSeccionActual(e.target.value)} disabled={secciones.length === 0}>
|
2025-08-22 21:55:03 -03:00
|
|
|
{secciones.map(s => <option key={s.id} value={s.id}>{s.nombre}</option>)}
|
|
|
|
|
</select>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="waffle-chart-container">
|
2025-09-02 19:38:04 -03:00
|
|
|
{isLoading ? <p>Cargando...</p> : errorMessage ? <p>{errorMessage}</p> :
|
2025-08-25 10:25:54 -03:00
|
|
|
<ResponsiveBar
|
|
|
|
|
data={barData}
|
|
|
|
|
keys={['bancas']}
|
|
|
|
|
indexBy="agrupacionNombre"
|
|
|
|
|
layout="horizontal"
|
|
|
|
|
margin={{ top: 10, right: 30, bottom: 25, left: 160 }}
|
|
|
|
|
padding={0.3}
|
|
|
|
|
valueScale={{ type: 'linear' }}
|
|
|
|
|
indexScale={{ type: 'band', round: true }}
|
|
|
|
|
colors={({ index }) => NIVO_COLORS[index % NIVO_COLORS.length]}
|
|
|
|
|
borderColor={{ from: 'color', modifiers: [['darker', 1.6]] }}
|
|
|
|
|
axisTop={null}
|
|
|
|
|
axisRight={null}
|
|
|
|
|
axisBottom={{
|
|
|
|
|
tickSize: 5,
|
|
|
|
|
tickPadding: 5,
|
|
|
|
|
tickRotation: 0,
|
|
|
|
|
legend: 'Cantidad de Bancas',
|
|
|
|
|
legendPosition: 'middle',
|
|
|
|
|
legendOffset: 20,
|
|
|
|
|
format: (value) => Math.floor(value) === value ? value : ''
|
|
|
|
|
}}
|
|
|
|
|
axisLeft={{
|
|
|
|
|
tickSize: 5,
|
|
|
|
|
tickPadding: 5,
|
|
|
|
|
tickRotation: 0,
|
|
|
|
|
}}
|
|
|
|
|
labelSkipWidth={12}
|
|
|
|
|
labelSkipHeight={12}
|
|
|
|
|
labelTextColor={{ from: 'color', modifiers: [['darker', 3]] }}
|
2025-08-22 21:55:03 -03:00
|
|
|
animate={true}
|
2025-08-25 10:25:54 -03:00
|
|
|
legends={[]}
|
2025-08-22 21:55:03 -03:00
|
|
|
/>}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2025-08-15 17:31:51 -03:00
|
|
|
};
|