Preparación Legislativas Nacionales 2025
This commit is contained in:
		| @@ -0,0 +1,121 @@ | ||||
| // src/features/legislativas/provinciales/DiputadosWidget.tsx | ||||
| import { useState, useMemo, useEffect } from 'react'; | ||||
| import { useQuery } from '@tanstack/react-query'; | ||||
| import Select from 'react-select'; | ||||
| import { getMunicipios, getResultadosPorMunicipio, getConfiguracionPublica, assetBaseUrl } from '../../../apiService'; | ||||
| import type { MunicipioSimple, ResultadoTicker } from '../../../types/types'; | ||||
| import { ImageWithFallback } from '../../../components/common/ImageWithFallback'; | ||||
| import './TickerWidget.css'; | ||||
|  | ||||
| const formatPercent = (num: number) => `${(num || 0).toFixed(2).replace('.', ',')}%`; | ||||
| const customSelectStyles = { | ||||
|   control: (base: any) => ({ ...base, minWidth: '220px', border: '1px solid #ced4da' }), | ||||
|   menu: (base: any) => ({ ...base, zIndex: 100 }), | ||||
| }; | ||||
|  | ||||
| // Constante para la categoría de este widget | ||||
| const CATEGORIA_ID = 6; // Diputados | ||||
|  | ||||
| export const DiputadosWidget = () => { | ||||
|   const [selectedMunicipio, setSelectedMunicipio] = useState<{ value: string; label: string } | null>(null); | ||||
|  | ||||
|   const { data: configData } = useQuery({ | ||||
|     queryKey: ['configuracionPublica'], | ||||
|     queryFn: getConfiguracionPublica, | ||||
|     staleTime: 0, | ||||
|     refetchInterval: 180000, | ||||
|   }); | ||||
|  | ||||
|   // Usamos la clave de configuración del Ticker, ya que es para Senadores/Diputados | ||||
|   const cantidadAMostrar = parseInt(configData?.TickerResultadosCantidad || '5', 10); | ||||
|  | ||||
|   const { data: municipios = [], isLoading: isLoadingMunicipios } = useQuery<MunicipioSimple[]>({ | ||||
|     queryKey: ['municipios', CATEGORIA_ID], // Key única para la lista de municipios de diputados | ||||
|     queryFn: () => getMunicipios(CATEGORIA_ID), // Pide solo los municipios que votan diputados | ||||
|   }); | ||||
|  | ||||
|   // 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)), | ||||
|     [municipios]); | ||||
|  | ||||
|   const { data: resultados, isLoading: isLoadingResultados } = useQuery<ResultadoTicker[]>({ | ||||
|     queryKey: ['resultadosMunicipio', selectedMunicipio?.value, CATEGORIA_ID], | ||||
|     queryFn: () => getResultadosPorMunicipio(selectedMunicipio!.value, CATEGORIA_ID), | ||||
|     enabled: !!selectedMunicipio, | ||||
|   }); | ||||
|  | ||||
|   // 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); | ||||
|     const otrosEntry: ResultadoTicker = { | ||||
|       id: `otros-diputados-${selectedMunicipio?.value}`, | ||||
|       nombre: 'Otros', | ||||
|       nombreCorto: 'Otros', | ||||
|       color: '#888888', | ||||
|       logoUrl: null, | ||||
|       votos: 0, | ||||
|       porcentaje: otrosPorcentaje, | ||||
|     }; | ||||
|     displayResults = [...topParties, otrosEntry]; | ||||
|   } else if (resultados) { | ||||
|     displayResults = resultados.slice(0, cantidadAMostrar); | ||||
|   } | ||||
|  | ||||
|   return ( | ||||
|     <div className="ticker-card"> | ||||
|       <div className="ticker-header"> | ||||
|         <h3>DIPUTADOS POR MUNICIPIO</h3> | ||||
|         <Select | ||||
|           options={municipioOptions} | ||||
|           value={selectedMunicipio} | ||||
|           onChange={(option) => setSelectedMunicipio(option)} | ||||
|           isLoading={isLoadingMunicipios} | ||||
|           placeholder="Buscar municipio..." | ||||
|           isClearable | ||||
|           styles={customSelectStyles} | ||||
|         /> | ||||
|       </div> | ||||
|       <div className="ticker-results"> | ||||
|         {(isLoadingMunicipios || (isLoadingResultados && selectedMunicipio)) && <p>Cargando...</p>} | ||||
|         {!selectedMunicipio && !isLoadingMunicipios && <p style={{ textAlign: 'center', color: '#666' }}>Seleccione un municipio.</p>} | ||||
|         {displayResults.map(partido => ( | ||||
|           <div key={partido.id} className="ticker-party"> | ||||
|             <div className="party-logo"> | ||||
|               <ImageWithFallback src={partido.logoUrl || undefined} fallbackSrc={`${assetBaseUrl}/default-avatar.png`} alt={`Logo de ${partido.nombre}`} /> | ||||
|             </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> | ||||
|               <div className="party-candidate-name"> | ||||
|                 {partido.nombreCandidato} | ||||
|               </div> | ||||
|             </div> | ||||
|           </div> | ||||
|         ))} | ||||
|       </div> | ||||
|     </div> | ||||
|   ); | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user