2025-10-04 20:41:23 -03:00
|
|
|
|
// src/features/legislativas/nacionales/PanelNacionalWidget.tsx
|
|
|
|
|
|
|
2025-10-03 13:26:20 -03:00
|
|
|
|
import { useMemo, useState, Suspense, useEffect } from 'react';
|
2025-09-19 17:19:10 -03:00
|
|
|
|
import { useSuspenseQuery } from '@tanstack/react-query';
|
2025-09-17 11:31:17 -03:00
|
|
|
|
import { getPanelElectoral } from '../../../apiService';
|
|
|
|
|
|
import { MapaNacional } from './components/MapaNacional';
|
|
|
|
|
|
import { PanelResultados } from './components/PanelResultados';
|
|
|
|
|
|
import { Breadcrumbs } from './components/Breadcrumbs';
|
2025-10-03 13:26:20 -03:00
|
|
|
|
import { MunicipioSearch } from './components/MunicipioSearch';
|
2025-10-04 20:41:23 -03:00
|
|
|
|
// 1. La importación de CSS ahora se hace como un módulo
|
|
|
|
|
|
import styles from './PanelNacional.module.css';
|
2025-09-17 11:31:17 -03:00
|
|
|
|
import Select from 'react-select';
|
2025-10-03 13:26:20 -03:00
|
|
|
|
import type { PanelElectoralDto, ResultadoTicker } from '../../../types/types';
|
|
|
|
|
|
import { FiMap, FiList, FiChevronDown, FiChevronUp } from 'react-icons/fi';
|
2025-09-20 22:31:11 -03:00
|
|
|
|
import { useMediaQuery } from './hooks/useMediaQuery';
|
2025-10-17 13:55:38 -03:00
|
|
|
|
import toast, { Toaster } from 'react-hot-toast';
|
2025-10-03 13:26:20 -03:00
|
|
|
|
import { ImageWithFallback } from '../../../components/common/ImageWithFallback';
|
|
|
|
|
|
import { assetBaseUrl } from '../../../apiService';
|
2025-10-17 13:55:38 -03:00
|
|
|
|
import { useQueryClient } from '@tanstack/react-query';
|
2025-09-17 11:31:17 -03:00
|
|
|
|
|
2025-10-03 13:26:20 -03:00
|
|
|
|
// --- COMPONENTE INTERNO PARA LA TARJETA DE RESULTADOS EN MÓVIL ---
|
|
|
|
|
|
interface MobileResultsCardProps {
|
|
|
|
|
|
eleccionId: number;
|
|
|
|
|
|
ambitoId: string | null;
|
|
|
|
|
|
categoriaId: number;
|
|
|
|
|
|
ambitoNombre: string;
|
|
|
|
|
|
ambitoNivel: 'pais' | 'provincia' | 'municipio';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const formatPercent = (num: number) => `${(num || 0).toFixed(2).replace('.', ',')}%`;
|
|
|
|
|
|
|
|
|
|
|
|
// --- SUB-COMPONENTE PARA UNA FILA DE RESULTADO ---
|
2025-10-04 20:41:23 -03:00
|
|
|
|
// 2. Todas las props 'className' ahora usan el objeto 'styles'
|
2025-10-03 13:26:20 -03:00
|
|
|
|
const ResultRow = ({ partido }: { partido: ResultadoTicker }) => (
|
2025-10-17 13:55:38 -03:00
|
|
|
|
<div className={styles.mobileResultRow} style={{ borderLeftColor: partido.color || '#ccc' }}>
|
|
|
|
|
|
<div className={styles.mobileResultLogo} style={{ backgroundColor: partido.color || '#e9ecef' }}>
|
|
|
|
|
|
<ImageWithFallback src={partido.logoUrl || undefined} fallbackSrc={`${assetBaseUrl}/default-avatar.png`} alt={partido.nombre} />
|
2025-10-03 13:26:20 -03:00
|
|
|
|
</div>
|
2025-10-17 13:55:38 -03:00
|
|
|
|
<div className={styles.mobileResultInfo}>
|
|
|
|
|
|
{partido.nombreCandidato ? (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<span className={styles.mobileResultPartyName}>{partido.nombreCandidato}</span>
|
|
|
|
|
|
<span className={styles.mobileResultCandidateName}>{partido.nombreCorto || partido.nombre}</span>
|
|
|
|
|
|
</>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<span className={styles.mobileResultPartyName}>{partido.nombreCorto || partido.nombre}</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className={styles.mobileResultStats}>
|
|
|
|
|
|
<strong>{formatPercent(partido.porcentaje)}</strong>
|
|
|
|
|
|
<span>{partido.votos.toLocaleString('es-AR')}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2025-10-03 13:26:20 -03:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// --- COMPONENTE REFACTORIZADO PARA LA TARJETA MÓVIL ---
|
|
|
|
|
|
interface MobileResultsCardProps {
|
|
|
|
|
|
eleccionId: number;
|
|
|
|
|
|
ambitoId: string | null;
|
|
|
|
|
|
categoriaId: number;
|
|
|
|
|
|
ambitoNombre: string;
|
|
|
|
|
|
ambitoNivel: 'pais' | 'provincia' | 'municipio';
|
|
|
|
|
|
mobileView: 'mapa' | 'resultados';
|
|
|
|
|
|
setMobileView: (view: 'mapa' | 'resultados') => void;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-17 13:55:38 -03:00
|
|
|
|
const MobileResultsCard = ({
|
|
|
|
|
|
eleccionId, ambitoId, categoriaId, ambitoNombre, ambitoNivel, mobileView, setMobileView
|
2025-10-03 13:26:20 -03:00
|
|
|
|
}: MobileResultsCardProps) => {
|
|
|
|
|
|
|
2025-10-17 13:55:38 -03:00
|
|
|
|
const [isExpanded, setIsExpanded] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
const { data } = useSuspenseQuery<PanelElectoralDto>({
|
|
|
|
|
|
queryKey: ['panelElectoral', eleccionId, ambitoId, categoriaId],
|
|
|
|
|
|
queryFn: () => getPanelElectoral(eleccionId, ambitoId, categoriaId),
|
|
|
|
|
|
});
|
2025-10-03 13:26:20 -03:00
|
|
|
|
|
2025-10-17 13:55:38 -03:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
setIsExpanded(ambitoNivel === 'municipio');
|
|
|
|
|
|
}, [ambitoNivel]);
|
2025-10-03 13:26:20 -03:00
|
|
|
|
|
2025-10-17 13:55:38 -03:00
|
|
|
|
const topResults = data.resultadosPanel.slice(0, 3);
|
2025-10-03 13:26:20 -03:00
|
|
|
|
|
2025-10-17 13:55:38 -03:00
|
|
|
|
if (topResults.length === 0 && ambitoNivel === 'pais') {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2025-10-04 20:41:23 -03:00
|
|
|
|
|
2025-10-17 13:55:38 -03:00
|
|
|
|
// 3. Clases condicionales también se construyen con el objeto 'styles'
|
|
|
|
|
|
const cardClasses = [
|
|
|
|
|
|
styles.mobileResultsCardContainer,
|
|
|
|
|
|
isExpanded ? styles.expanded : '',
|
|
|
|
|
|
styles[`view-${mobileView}`]
|
|
|
|
|
|
].join(' ');
|
2025-10-03 13:26:20 -03:00
|
|
|
|
|
2025-10-17 13:55:38 -03:00
|
|
|
|
return (
|
|
|
|
|
|
<div className={cardClasses}>
|
|
|
|
|
|
{/* Sección Colapsable con Resultados */}
|
|
|
|
|
|
<div className={styles.collapsibleSection}>
|
|
|
|
|
|
<div className={styles.mobileResultsHeader} onClick={() => setIsExpanded(!isExpanded)}>
|
|
|
|
|
|
<div className={styles.headerInfo}>
|
|
|
|
|
|
<h4>{ambitoNombre}</h4>
|
|
|
|
|
|
<span className={styles.headerActionText}>{isExpanded ? 'Ocultar resultados' : 'Ver top 3'}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className={styles.headerToggleIcon}>
|
|
|
|
|
|
{isExpanded ? <FiChevronDown /> : <FiChevronUp />}
|
|
|
|
|
|
</div>
|
2025-10-03 13:26:20 -03:00
|
|
|
|
</div>
|
2025-10-17 13:55:38 -03:00
|
|
|
|
<div className={styles.mobileResultsContent}>
|
|
|
|
|
|
{topResults.length > 0 ? (
|
|
|
|
|
|
topResults.map(partido => <ResultRow key={partido.id} partido={partido} />)
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<p className={styles.noResultsText}>No hay resultados para esta selección.</p>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* Footer Fijo con Botones de Navegación */}
|
|
|
|
|
|
<div className={styles.mobileCardViewToggle}>
|
|
|
|
|
|
<button
|
|
|
|
|
|
className={`${styles.toggleBtn} ${mobileView === 'mapa' ? styles.active : ''}`}
|
|
|
|
|
|
onClick={() => setMobileView('mapa')}
|
|
|
|
|
|
>
|
|
|
|
|
|
<FiMap />
|
|
|
|
|
|
<span>Mapa</span>
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button
|
|
|
|
|
|
className={`${styles.toggleBtn} ${mobileView === 'resultados' ? styles.active : ''}`}
|
|
|
|
|
|
onClick={() => setMobileView('resultados')}
|
|
|
|
|
|
>
|
|
|
|
|
|
<FiList />
|
|
|
|
|
|
<span>Detalles</span>
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
2025-10-03 13:26:20 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// --- WIDGET PRINCIPAL ---
|
2025-09-17 11:31:17 -03:00
|
|
|
|
interface PanelNacionalWidgetProps {
|
|
|
|
|
|
eleccionId: number;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type AmbitoState = {
|
|
|
|
|
|
id: string | null;
|
|
|
|
|
|
nivel: 'pais' | 'provincia' | 'municipio';
|
|
|
|
|
|
nombre: string;
|
|
|
|
|
|
provinciaNombre?: string;
|
|
|
|
|
|
provinciaDistritoId?: string | null;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const CATEGORIAS_NACIONALES = [
|
2025-10-17 14:05:44 -03:00
|
|
|
|
{ value: 3, label: 'Diputados Nacionales' },
|
|
|
|
|
|
{ value: 2, label: 'Senadores Nacionales' },
|
2025-09-17 11:31:17 -03:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
const PanelContenido = ({ eleccionId, ambitoActual, categoriaId }: { eleccionId: number, ambitoActual: AmbitoState, categoriaId: number }) => {
|
|
|
|
|
|
const { data } = useSuspenseQuery<PanelElectoralDto>({
|
|
|
|
|
|
queryKey: ['panelElectoral', eleccionId, ambitoActual.id, categoriaId],
|
|
|
|
|
|
queryFn: () => getPanelElectoral(eleccionId, ambitoActual.id, categoriaId),
|
|
|
|
|
|
});
|
2025-10-17 13:55:38 -03:00
|
|
|
|
// Si la API devolvió la bandera 'sinDatos', mostramos un mensaje.
|
|
|
|
|
|
if (data.sinDatos) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div style={{ padding: '2rem', textAlign: 'center', color: '#666' }}>
|
|
|
|
|
|
<h4>Sin Resultados Detallados</h4>
|
|
|
|
|
|
<p>Aún no hay datos disponibles para esta selección.</p>
|
|
|
|
|
|
<p>Por favor, intente de nuevo más tarde.</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
// Si no, renderizamos los resultados.
|
2025-09-19 17:19:10 -03:00
|
|
|
|
return <PanelResultados resultados={data.resultadosPanel} estadoRecuento={data.estadoRecuento} />;
|
2025-09-17 11:31:17 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const PanelNacionalWidget = ({ eleccionId }: PanelNacionalWidgetProps) => {
|
2025-10-17 13:55:38 -03:00
|
|
|
|
const queryClient = useQueryClient();
|
2025-09-17 11:31:17 -03:00
|
|
|
|
const [ambitoActual, setAmbitoActual] = useState<AmbitoState>({ id: null, nivel: 'pais', nombre: 'Argentina', provinciaDistritoId: null });
|
2025-10-17 14:05:44 -03:00
|
|
|
|
const [categoriaId, setCategoriaId] = useState<number>(3);
|
2025-09-17 11:31:17 -03:00
|
|
|
|
const [isPanelOpen, setIsPanelOpen] = useState(true);
|
2025-09-20 22:31:11 -03:00
|
|
|
|
const [mobileView, setMobileView] = useState<'mapa' | 'resultados'>('mapa');
|
|
|
|
|
|
const isMobile = useMediaQuery('(max-width: 800px)');
|
2025-09-17 11:31:17 -03:00
|
|
|
|
|
|
|
|
|
|
const handleAmbitoSelect = (nuevoAmbitoId: string, nuevoNivel: 'provincia' | 'municipio', nuevoNombre: string) => {
|
2025-10-17 13:55:38 -03:00
|
|
|
|
if (nuevoNivel === 'municipio') {
|
|
|
|
|
|
toast.promise(
|
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: ['panelElectoral', eleccionId, nuevoAmbitoId, categoriaId] }),
|
|
|
|
|
|
{
|
|
|
|
|
|
loading: `Cargando datos de ${nuevoNombre}...`,
|
|
|
|
|
|
error: <b>No se pudieron cargar los datos.</b>,
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2025-09-17 11:31:17 -03:00
|
|
|
|
setAmbitoActual(prev => ({
|
|
|
|
|
|
id: nuevoAmbitoId,
|
|
|
|
|
|
nivel: nuevoNivel,
|
|
|
|
|
|
nombre: nuevoNombre,
|
2025-09-19 17:19:10 -03:00
|
|
|
|
provinciaNombre: nuevoNivel === 'municipio' ? prev.provinciaNombre : (nuevoNivel === 'provincia' ? nuevoNombre : undefined),
|
2025-09-17 11:31:17 -03:00
|
|
|
|
provinciaDistritoId: nuevoNivel === 'provincia' ? nuevoAmbitoId : prev.provinciaDistritoId
|
|
|
|
|
|
}));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleResetToPais = () => {
|
|
|
|
|
|
setAmbitoActual({ id: null, nivel: 'pais', nombre: 'Argentina', provinciaDistritoId: null });
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleVolverAProvincia = () => {
|
|
|
|
|
|
if (ambitoActual.provinciaDistritoId && ambitoActual.provinciaNombre) {
|
|
|
|
|
|
setAmbitoActual({
|
|
|
|
|
|
id: ambitoActual.provinciaDistritoId,
|
|
|
|
|
|
nivel: 'provincia',
|
|
|
|
|
|
nombre: ambitoActual.provinciaNombre,
|
2025-09-19 17:19:10 -03:00
|
|
|
|
provinciaDistritoId: ambitoActual.provinciaDistritoId,
|
|
|
|
|
|
provinciaNombre: ambitoActual.provinciaNombre,
|
2025-09-17 11:31:17 -03:00
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
handleResetToPais();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const selectedCategoria = useMemo(() =>
|
|
|
|
|
|
CATEGORIAS_NACIONALES.find(c => c.value === categoriaId),
|
|
|
|
|
|
[categoriaId]
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2025-10-04 20:41:23 -03:00
|
|
|
|
const mainContentClasses = [
|
|
|
|
|
|
styles.panelMainContent,
|
|
|
|
|
|
!isPanelOpen ? styles.panelCollapsed : '',
|
|
|
|
|
|
isMobile ? styles[`mobile-view-${mobileView}`] : ''
|
|
|
|
|
|
].join(' ');
|
|
|
|
|
|
|
2025-09-17 11:31:17 -03:00
|
|
|
|
return (
|
2025-10-04 20:41:23 -03:00
|
|
|
|
<div className={styles.panelNacionalContainer}>
|
2025-10-17 13:55:38 -03:00
|
|
|
|
<Toaster
|
|
|
|
|
|
position="bottom-center"
|
|
|
|
|
|
containerClassName={styles.widgetToasterContainer}
|
|
|
|
|
|
/>
|
2025-10-04 20:41:23 -03:00
|
|
|
|
<header className={styles.panelHeader}>
|
|
|
|
|
|
<div className={styles.headerTopRow}>
|
2025-09-17 11:31:17 -03:00
|
|
|
|
<Select
|
|
|
|
|
|
options={CATEGORIAS_NACIONALES}
|
|
|
|
|
|
value={selectedCategoria}
|
|
|
|
|
|
onChange={(option) => option && setCategoriaId(option.value)}
|
2025-10-04 20:41:23 -03:00
|
|
|
|
classNamePrefix="categoriaSelector"
|
|
|
|
|
|
className={styles.categoriaSelectorContainer}
|
2025-09-20 22:31:11 -03:00
|
|
|
|
isSearchable={false}
|
2025-09-17 11:31:17 -03:00
|
|
|
|
/>
|
2025-10-03 13:26:20 -03:00
|
|
|
|
</div>
|
2025-10-04 20:41:23 -03:00
|
|
|
|
<div className={styles.headerBottomRow}>
|
2025-10-17 13:55:38 -03:00
|
|
|
|
<Breadcrumbs
|
|
|
|
|
|
nivel={ambitoActual.nivel}
|
|
|
|
|
|
nombreAmbito={ambitoActual.nombre}
|
|
|
|
|
|
nombreProvincia={ambitoActual.provinciaNombre}
|
|
|
|
|
|
onReset={handleResetToPais}
|
|
|
|
|
|
onVolverProvincia={handleVolverAProvincia}
|
|
|
|
|
|
/>
|
|
|
|
|
|
{ambitoActual.nivel === 'provincia' && ambitoActual.provinciaDistritoId && (
|
|
|
|
|
|
<MunicipioSearch
|
|
|
|
|
|
distritoId={ambitoActual.provinciaDistritoId}
|
|
|
|
|
|
onMunicipioSelect={(municipioId, municipioNombre) =>
|
|
|
|
|
|
handleAmbitoSelect(municipioId, 'municipio', municipioNombre)
|
|
|
|
|
|
}
|
2025-10-03 13:26:20 -03:00
|
|
|
|
/>
|
2025-10-17 13:55:38 -03:00
|
|
|
|
)}
|
2025-09-17 11:31:17 -03:00
|
|
|
|
</div>
|
|
|
|
|
|
</header>
|
2025-10-04 20:41:23 -03:00
|
|
|
|
<main className={mainContentClasses}>
|
|
|
|
|
|
<div className={styles.mapaColumn}>
|
|
|
|
|
|
<button className={styles.panelToggleBtn} onClick={() => setIsPanelOpen(!isPanelOpen)} title={isPanelOpen ? "Ocultar panel" : "Mostrar panel"}> {isPanelOpen ? '›' : '‹'} </button>
|
2025-10-17 13:55:38 -03:00
|
|
|
|
|
2025-10-04 20:41:23 -03:00
|
|
|
|
<Suspense fallback={<div className={styles.spinner} />}>
|
2025-10-03 13:26:20 -03:00
|
|
|
|
<MapaNacional eleccionId={eleccionId} categoriaId={categoriaId} nivel={ambitoActual.nivel} nombreAmbito={ambitoActual.nombre} nombreProvinciaActiva={ambitoActual.provinciaNombre} provinciaDistritoId={ambitoActual.provinciaDistritoId ?? null} onAmbitoSelect={handleAmbitoSelect} onVolver={ambitoActual.nivel === 'municipio' ? handleVolverAProvincia : handleResetToPais} isMobileView={isMobile} />
|
2025-09-17 11:31:17 -03:00
|
|
|
|
</Suspense>
|
|
|
|
|
|
</div>
|
2025-10-04 20:41:23 -03:00
|
|
|
|
<div className={styles.resultadosColumn}>
|
|
|
|
|
|
<Suspense fallback={<div className={styles.spinner} />}>
|
2025-10-03 13:26:20 -03:00
|
|
|
|
<PanelContenido eleccionId={eleccionId} ambitoActual={ambitoActual} categoriaId={categoriaId} />
|
2025-09-17 11:31:17 -03:00
|
|
|
|
</Suspense>
|
|
|
|
|
|
</div>
|
2025-09-20 22:31:11 -03:00
|
|
|
|
|
2025-10-03 13:26:20 -03:00
|
|
|
|
<Suspense fallback={null}>
|
2025-10-17 13:55:38 -03:00
|
|
|
|
{isMobile && (
|
|
|
|
|
|
<MobileResultsCard
|
|
|
|
|
|
eleccionId={eleccionId}
|
|
|
|
|
|
ambitoId={ambitoActual.id}
|
|
|
|
|
|
categoriaId={categoriaId}
|
|
|
|
|
|
ambitoNombre={ambitoActual.nombre}
|
|
|
|
|
|
ambitoNivel={ambitoActual.nivel}
|
|
|
|
|
|
mobileView={mobileView}
|
|
|
|
|
|
setMobileView={setMobileView}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
2025-10-03 13:26:20 -03:00
|
|
|
|
</Suspense>
|
|
|
|
|
|
</main>
|
2025-09-17 11:31:17 -03:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|