import { useState, useMemo, useEffect } from 'react'; import { useQuery } from '@tanstack/react-query'; import Select from 'react-select'; import { getSeccionesElectorales, getRankingMunicipiosPorSeccion } from '../apiService'; import type { MunicipioSimple, ApiResponseRankingMunicipio } from '../types/types'; import './ResultadosTablaSeccionWidget.css'; // Reutilizamos el mismo estilo const customSelectStyles = { control: (base: any) => ({ ...base, minWidth: '200px', border: '1px solid #ced4da', boxShadow: 'none', '&:hover': { borderColor: '#86b7fe' } }), menu: (base: any) => ({ ...base, zIndex: 10 }), }; const formatPercent = (porcentaje: number) => `${porcentaje.toFixed(2).replace('.', ',')}%`; export const ResultadosRankingMunicipioWidget = () => { const [secciones, setSecciones] = useState([]); const [selectedSeccion, setSelectedSeccion] = useState<{ value: string; label: string } | null>(null); useEffect(() => { const fetchSecciones = async () => { 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); if (!selectedSeccion) { setSelectedSeccion({ value: seccionesData[0].id, label: seccionesData[0].nombre }); } } }; fetchSecciones(); }, [selectedSeccion]); const seccionOptions = useMemo(() => secciones.map(s => ({ value: s.id, label: s.nombre })), [secciones]); const { data: rankingData, isLoading } = useQuery({ queryKey: ['rankingMunicipiosPorSeccion', selectedSeccion?.value], queryFn: () => getRankingMunicipiosPorSeccion(selectedSeccion!.value), enabled: !!selectedSeccion, }); return (

Resultados por Municipio