| 
									
										
										
										
											2025-09-02 17:08:56 -03:00
										 |  |  | // src/components/SenadoresWidget.tsx
 | 
					
						
							| 
									
										
										
										
											2025-09-02 19:38:04 -03:00
										 |  |  | import { useState, useEffect, useMemo } 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'; // Importamos react-select
 | 
					
						
							| 
									
										
										
										
											2025-09-03 18:36:54 -03:00
										 |  |  | import { getMunicipios, getResultadosPorMunicipio, getConfiguracionPublica, assetBaseUrl } from '../apiService'; // Usamos las funciones genéricas
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | // Estilos para el selector, podemos moverlos a un archivo común más adelante
 | 
					
						
							|  |  |  | const customSelectStyles = { | 
					
						
							|  |  |  |   control: (base: any) => ({ ...base, minWidth: '220px', border: '1px solid #ced4da' }), | 
					
						
							|  |  |  |   menu: (base: any) => ({ ...base, zIndex: 10 }), | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Constante para la categoría de este widget
 | 
					
						
							|  |  |  | const CATEGORIA_ID = 5; // Senadores
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-02 17:08:56 -03:00
										 |  |  | export const SenadoresWidget = () => { | 
					
						
							| 
									
										
										
										
											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-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 caché
 | 
					
						
							|  |  |  |     queryFn: () => getMunicipios(CATEGORIA_ID), // Pasamos el ID de la categoría
 | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2025-09-02 17:08:56 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-03 13:49:35 -03:00
										 |  |  |   // useEffect para establecer "ALBERTI" por defecto
 | 
					
						
							| 
									
										
										
										
											2025-09-02 19:38:04 -03:00
										 |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     if (municipios.length > 0 && !selectedMunicipio) { | 
					
						
							| 
									
										
										
										
											2025-09-03 13:49:35 -03:00
										 |  |  |       const laPlata = municipios.find(m => m.nombre.toUpperCase() === 'ALBERTI'); | 
					
						
							| 
									
										
										
										
											2025-09-02 19:38:04 -03:00
										 |  |  |       if (laPlata) { | 
					
						
							|  |  |  |         setSelectedMunicipio({ value: laPlata.id, label: laPlata.nombre }); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, [municipios, selectedMunicipio]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const municipioOptions = useMemo(() => | 
					
						
							|  |  |  |     municipios | 
					
						
							|  |  |  |       .map(m => ({ value: m.id, label: m.nombre })) | 
					
						
							|  |  |  |       .sort((a, b) => a.label.localeCompare(b.label)), | 
					
						
							|  |  |  |     [municipios]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   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-senadores-${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>SENADORES 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>} | 
					
						
							|  |  |  |         {!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> | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | }; |