// src/components/LogoOverridesManager.tsx import { useState, useMemo, useEffect } from 'react'; import { useQuery, useQueryClient } from '@tanstack/react-query'; import Select from 'react-select'; import { getProvinciasForAdmin, getMunicipiosForAdmin, getAgrupaciones, getLogos, updateLogos } from '../services/apiService'; import type { MunicipioSimple, AgrupacionPolitica, LogoAgrupacionCategoria, ProvinciaSimple } from '../types'; import { CATEGORIAS_NACIONALES_OPTIONS, CATEGORIAS_PROVINCIALES_OPTIONS } from '../constants/categorias'; const ELECCION_OPTIONS = [ { value: 0, label: 'General (Toda la elección)' }, { value: 2, label: 'Elecciones Nacionales' }, { value: 1, label: 'Elecciones Provinciales' } ]; const AMBITO_LEVEL_OPTIONS = [ { value: 'general', label: 'General (Toda la elección)' }, { value: 'provincia', label: 'Por Provincia' }, { value: 'municipio', label: 'Por Municipio' } ]; export const LogoOverridesManager = () => { const queryClient = useQueryClient(); // --- ESTADOS --- const [selectedEleccion, setSelectedEleccion] = useState(ELECCION_OPTIONS[0]); const [selectedAmbitoLevel, setSelectedAmbitoLevel] = useState(AMBITO_LEVEL_OPTIONS[0]); const [selectedProvincia, setSelectedProvincia] = useState(null); const [selectedMunicipio, setSelectedMunicipio] = useState(null); const [selectedCategoria, setSelectedCategoria] = useState<{ value: number; label: string } | null>(null); const [selectedAgrupacion, setSelectedAgrupacion] = useState(null); const [logoUrl, setLogoUrl] = useState(''); // --- QUERIES --- const { data: provincias = [] } = useQuery({ queryKey: ['provinciasForAdmin'], queryFn: getProvinciasForAdmin }); const { data: municipios = [] } = useQuery({ queryKey: ['municipiosForAdmin'], queryFn: getMunicipiosForAdmin }); const { data: agrupaciones = [] } = useQuery({ queryKey: ['agrupaciones'], queryFn: getAgrupaciones }); const { data: logos = [] } = useQuery({ queryKey: ['logos', selectedEleccion.value], queryFn: () => getLogos(selectedEleccion.value) }); // --- LÓGICA DE SELECTORES DINÁMICOS --- const categoriaOptions = selectedEleccion.value === 2 ? CATEGORIAS_NACIONALES_OPTIONS : CATEGORIAS_PROVINCIALES_OPTIONS; const getAmbitoId = () => { if (selectedAmbitoLevel.value === 'municipio' && selectedMunicipio) return parseInt(selectedMunicipio.id); if (selectedAmbitoLevel.value === 'provincia' && selectedProvincia) return parseInt(selectedProvincia.id); return 0; }; const currentLogo = useMemo(() => { if (!selectedAgrupacion || !selectedCategoria) return ''; const ambitoId = getAmbitoId(); return logos.find(l => l.ambitoGeograficoId === ambitoId && l.agrupacionPoliticaId === selectedAgrupacion.id && l.categoriaId === selectedCategoria.value )?.logoUrl || ''; }, [logos, selectedAmbitoLevel, selectedProvincia, selectedMunicipio, selectedAgrupacion, selectedCategoria]); useEffect(() => { setLogoUrl(currentLogo || ''); }, [currentLogo]); const handleSave = async () => { if (!selectedAgrupacion || !selectedCategoria) return; const newLogoEntry: LogoAgrupacionCategoria = { id: 0, eleccionId: selectedEleccion.value, agrupacionPoliticaId: selectedAgrupacion.id, categoriaId: selectedCategoria.value, ambitoGeograficoId: getAmbitoId(), logoUrl: logoUrl || null }; try { await updateLogos([newLogoEntry]); queryClient.invalidateQueries({ queryKey: ['logos', selectedEleccion.value] }); alert('Override de logo guardado.'); } catch { alert('Error al guardar.'); } }; return (

Overrides de Logos

Configure una imagen específica para un partido en un contexto determinado.

{ setSelectedAmbitoLevel(opt!); setSelectedProvincia(null); setSelectedMunicipio(null); }} /> {selectedAmbitoLevel.value === 'provincia' || selectedAmbitoLevel.value === 'municipio' ? ( ({ value: m.id, label: m.nombre, ...m }))} getOptionValue={opt => opt.id} getOptionLabel={opt => opt.nombre} value={selectedMunicipio} onChange={setSelectedMunicipio} placeholder="Seleccione Municipio..." isDisabled={!selectedProvincia} /> ) :
}
setLogoUrl(e.target.value)} style={{ width: '100%' }} disabled={!selectedAgrupacion || !selectedCategoria} />
); };