2025-09-02 17:08:56 -03:00
|
|
|
// src/components/DiputadosWidget.tsx
|
2025-09-02 19:38:04 -03:00
|
|
|
import { useState, useMemo, useEffect } from 'react';
|
2025-09-02 17:08:56 -03:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
2025-09-02 19:38:04 -03:00
|
|
|
import Select from 'react-select';
|
2025-09-03 18:36:54 -03:00
|
|
|
import { getMunicipios, getResultadosPorMunicipio, getConfiguracionPublica, assetBaseUrl } from '../apiService';
|
2025-09-02 19:38:04 -03:00
|
|
|
import type { MunicipioSimple, ResultadoTicker } from '../types/types';
|
2025-09-02 17:08:56 -03:00
|
|
|
import { ImageWithFallback } from './ImageWithFallback';
|
|
|
|
|
import './TickerWidget.css';
|
|
|
|
|
|
|
|
|
|
const formatPercent = (num: number) => `${(num || 0).toFixed(2).replace('.', ',')}%`;
|
2025-09-02 19:38:04 -03:00
|
|
|
const customSelectStyles = {
|
|
|
|
|
control: (base: any) => ({ ...base, minWidth: '220px', border: '1px solid #ced4da' }),
|
2025-09-05 14:10:37 -03:00
|
|
|
menu: (base: any) => ({ ...base, zIndex: 100 }),
|
2025-09-02 19:38:04 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Constante para la categoría de este widget
|
|
|
|
|
const CATEGORIA_ID = 6; // Diputados
|
2025-09-02 17:08:56 -03:00
|
|
|
|
|
|
|
|
export const DiputadosWidget = () => {
|
2025-09-02 19:38:04 -03:00
|
|
|
const [selectedMunicipio, setSelectedMunicipio] = useState<{ value: string; label: string } | null>(null);
|
2025-09-02 17:08:56 -03:00
|
|
|
|
|
|
|
|
const { data: configData } = useQuery({
|
|
|
|
|
queryKey: ['configuracionPublica'],
|
|
|
|
|
queryFn: getConfiguracionPublica,
|
|
|
|
|
staleTime: 0,
|
2025-09-05 13:33:09 -03:00
|
|
|
refetchInterval: 180000,
|
2025-09-02 17:08:56 -03:00
|
|
|
});
|
|
|
|
|
|
2025-09-02 19:38:04 -03:00
|
|
|
// Usamos la clave de configuración del Ticker, ya que es para Senadores/Diputados
|
2025-09-02 17:08:56 -03:00
|
|
|
const cantidadAMostrar = parseInt(configData?.TickerResultadosCantidad || '5', 10);
|
|
|
|
|
|
2025-09-02 19:38:04 -03:00
|
|
|
const { data: municipios = [], isLoading: isLoadingMunicipios } = useQuery<MunicipioSimple[]>({
|
|
|
|
|
queryKey: ['municipios', CATEGORIA_ID], // Key única para la lista de municipios de diputados
|
2025-09-07 20:12:03 -03:00
|
|
|
queryFn: () => getMunicipios(CATEGORIA_ID), // Pide solo los municipios que votan diputados
|
2025-09-02 19:38:04 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// useEffect para establecer "LA PLATA" por defecto
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (municipios.length > 0 && !selectedMunicipio) {
|
|
|
|
|
const laPlata = municipios.find(m => m.nombre.toUpperCase() === 'LA PLATA');
|
|
|
|
|
if (laPlata) {
|
|
|
|
|
setSelectedMunicipio({ value: laPlata.id, label: laPlata.nombre });
|
|
|
|
|
} else if (municipios.length > 0) {
|
|
|
|
|
// Si no está La Plata, seleccionamos el primero de la lista
|
|
|
|
|
setSelectedMunicipio({ value: municipios[0].id, label: municipios[0].nombre });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [municipios, selectedMunicipio]);
|
|
|
|
|
|
|
|
|
|
const municipioOptions = useMemo(() =>
|
|
|
|
|
municipios
|
|
|
|
|
.map(m => ({ value: m.id, label: m.nombre }))
|
|
|
|
|
.sort((a, b) => a.label.localeCompare(b.label)),
|
2025-09-05 11:38:25 -03:00
|
|
|
[municipios]);
|
2025-09-02 17:08:56 -03:00
|
|
|
|
2025-09-02 19:38:04 -03:00
|
|
|
const { data: resultados, isLoading: isLoadingResultados } = useQuery<ResultadoTicker[]>({
|
|
|
|
|
queryKey: ['resultadosMunicipio', selectedMunicipio?.value, CATEGORIA_ID],
|
|
|
|
|
queryFn: () => getResultadosPorMunicipio(selectedMunicipio!.value, CATEGORIA_ID),
|
|
|
|
|
enabled: !!selectedMunicipio,
|
|
|
|
|
});
|
2025-09-02 17:08:56 -03:00
|
|
|
|
2025-09-02 19:38:04 -03:00
|
|
|
// Lógica para "Otros"
|
|
|
|
|
let displayResults: ResultadoTicker[] = resultados || [];
|
|
|
|
|
if (resultados && resultados.length > cantidadAMostrar) {
|
|
|
|
|
const topParties = resultados.slice(0, cantidadAMostrar - 1);
|
|
|
|
|
const otherParties = resultados.slice(cantidadAMostrar - 1);
|
|
|
|
|
const otrosPorcentaje = otherParties.reduce((sum, party) => sum + (party.porcentaje || 0), 0);
|
2025-09-02 17:08:56 -03:00
|
|
|
const otrosEntry: ResultadoTicker = {
|
2025-09-02 19:38:04 -03:00
|
|
|
id: `otros-diputados-${selectedMunicipio?.value}`,
|
2025-09-02 17:08:56 -03:00
|
|
|
nombre: 'Otros',
|
|
|
|
|
nombreCorto: 'Otros',
|
|
|
|
|
color: '#888888',
|
|
|
|
|
logoUrl: null,
|
|
|
|
|
votos: 0,
|
|
|
|
|
porcentaje: otrosPorcentaje,
|
|
|
|
|
};
|
|
|
|
|
displayResults = [...topParties, otrosEntry];
|
2025-09-02 19:38:04 -03:00
|
|
|
} else if (resultados) {
|
|
|
|
|
displayResults = resultados.slice(0, cantidadAMostrar);
|
2025-09-02 17:08:56 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="ticker-card">
|
|
|
|
|
<div className="ticker-header">
|
2025-09-02 19:38:04 -03:00
|
|
|
<h3>DIPUTADOS POR MUNICIPIO</h3>
|
|
|
|
|
<Select
|
|
|
|
|
options={municipioOptions}
|
|
|
|
|
value={selectedMunicipio}
|
|
|
|
|
onChange={(option) => setSelectedMunicipio(option)}
|
|
|
|
|
isLoading={isLoadingMunicipios}
|
|
|
|
|
placeholder="Buscar municipio..."
|
|
|
|
|
isClearable
|
|
|
|
|
styles={customSelectStyles}
|
|
|
|
|
/>
|
2025-09-02 17:08:56 -03:00
|
|
|
</div>
|
|
|
|
|
<div className="ticker-results">
|
2025-09-02 19:38:04 -03:00
|
|
|
{(isLoadingMunicipios || (isLoadingResultados && selectedMunicipio)) && <p>Cargando...</p>}
|
2025-09-05 11:38:25 -03:00
|
|
|
{!selectedMunicipio && !isLoadingMunicipios && <p style={{ textAlign: 'center', color: '#666' }}>Seleccione un municipio.</p>}
|
2025-09-02 17:08:56 -03:00
|
|
|
{displayResults.map(partido => (
|
|
|
|
|
<div key={partido.id} className="ticker-party">
|
|
|
|
|
<div className="party-logo">
|
2025-09-03 18:36:54 -03:00
|
|
|
<ImageWithFallback src={partido.logoUrl || undefined} fallbackSrc={`${assetBaseUrl}/default-avatar.png`} alt={`Logo de ${partido.nombre}`} />
|
2025-09-02 17:08:56 -03:00
|
|
|
</div>
|
|
|
|
|
<div className="party-details">
|
|
|
|
|
<div className="party-info">
|
|
|
|
|
<span className="party-name">{partido.nombreCorto || partido.nombre}</span>
|
|
|
|
|
<span className="party-percent">{formatPercent(partido.porcentaje)}</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="party-bar-background">
|
|
|
|
|
<div className="party-bar-foreground" style={{ width: `${partido.porcentaje}%`, backgroundColor: partido.color || '#888' }}></div>
|
|
|
|
|
</div>
|
2025-09-05 11:38:25 -03:00
|
|
|
<div className="party-candidate-name">
|
|
|
|
|
{partido.nombreCandidato}
|
|
|
|
|
</div>
|
2025-09-02 17:08:56 -03:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|