Fix Overrides Logos Y Candidatos
This commit is contained in:
		| @@ -7,6 +7,7 @@ import type { MunicipioSimple, AgrupacionPolitica, CandidatoOverride, ProvinciaS | ||||
| import { CATEGORIAS_NACIONALES_OPTIONS, CATEGORIAS_PROVINCIALES_OPTIONS } from '../constants/categorias'; | ||||
|  | ||||
| const ELECCION_OPTIONS = [ | ||||
|     { value: 0, label: 'General (Todas las elecciones)' }, | ||||
|     { value: 2, label: 'Elecciones Nacionales' }, | ||||
|     { value: 1, label: 'Elecciones Provinciales' } | ||||
| ]; | ||||
| @@ -31,12 +32,17 @@ export const CandidatoOverridesManager = () => { | ||||
|     const { data: provincias = [] } = useQuery<ProvinciaSimple[]>({ queryKey: ['provinciasForAdmin'], queryFn: getProvinciasForAdmin }); | ||||
|     const { data: municipios = [] } = useQuery<MunicipioSimple[]>({ queryKey: ['municipiosForAdmin'], queryFn: getMunicipiosForAdmin }); | ||||
|     const { data: agrupaciones = [] } = useQuery<AgrupacionPolitica[]>({ queryKey: ['agrupaciones'], queryFn: getAgrupaciones }); | ||||
|  | ||||
|     const { data: candidatos = [] } = useQuery<CandidatoOverride[]>({ | ||||
|         queryKey: ['candidatos', selectedEleccion.value], | ||||
|         queryFn: () => getCandidatos(selectedEleccion.value), | ||||
|         queryKey: ['allCandidatos'], | ||||
|         queryFn: () => Promise.all([getCandidatos(0), getCandidatos(1), getCandidatos(2)]).then(res => res.flat()), | ||||
|     }); | ||||
|  | ||||
|     const categoriaOptions = selectedEleccion.value === 2 ? CATEGORIAS_NACIONALES_OPTIONS : CATEGORIAS_PROVINCIALES_OPTIONS; | ||||
|     const categoriaOptions = useMemo(() => { | ||||
|         if (selectedEleccion.value === 2) return CATEGORIAS_NACIONALES_OPTIONS; | ||||
|         if (selectedEleccion.value === 1) return CATEGORIAS_PROVINCIALES_OPTIONS; | ||||
|         return [...CATEGORIAS_NACIONALES_OPTIONS, ...CATEGORIAS_PROVINCIALES_OPTIONS]; | ||||
|     }, [selectedEleccion]); | ||||
|  | ||||
|     const getAmbitoId = () => { | ||||
|         if (selectedAmbitoLevel.value === 'municipio' && selectedMunicipio) return parseInt(selectedMunicipio.id); | ||||
| @@ -48,11 +54,12 @@ export const CandidatoOverridesManager = () => { | ||||
|         if (!selectedAgrupacion || !selectedCategoria) return ''; | ||||
|         const ambitoId = getAmbitoId(); | ||||
|         return candidatos.find(c => | ||||
|             c.eleccionId === selectedEleccion.value && | ||||
|             c.ambitoGeograficoId === ambitoId && | ||||
|             c.agrupacionPoliticaId === selectedAgrupacion.id && | ||||
|             c.categoriaId === selectedCategoria.value | ||||
|         )?.nombreCandidato || ''; | ||||
|     }, [candidatos, selectedAmbitoLevel, selectedProvincia, selectedMunicipio, selectedAgrupacion, selectedCategoria]); | ||||
|     }, [candidatos, selectedEleccion, selectedAmbitoLevel, selectedProvincia, selectedMunicipio, selectedAgrupacion, selectedCategoria]); | ||||
|  | ||||
|     useEffect(() => { setNombreCandidato(currentCandidato || ''); }, [currentCandidato]); | ||||
|  | ||||
| @@ -64,11 +71,11 @@ export const CandidatoOverridesManager = () => { | ||||
|             agrupacionPoliticaId: selectedAgrupacion.id, | ||||
|             categoriaId: selectedCategoria.value, | ||||
|             ambitoGeograficoId: getAmbitoId(), | ||||
|             nombreCandidato: nombreCandidato || null | ||||
|             nombreCandidato: nombreCandidato.trim() || null | ||||
|         }; | ||||
|         try { | ||||
|             await updateCandidatos([newCandidatoEntry]); | ||||
|             queryClient.invalidateQueries({ queryKey: ['candidatos', selectedEleccion.value] }); | ||||
|             queryClient.invalidateQueries({ queryKey: ['allCandidatos'] }); | ||||
|             alert('Override de candidato guardado.'); | ||||
|         } catch (error) { | ||||
|             console.error(error); | ||||
| @@ -82,7 +89,7 @@ export const CandidatoOverridesManager = () => { | ||||
|             <p>Configure un nombre de candidato específico para un partido en un contexto determinado.</p> | ||||
|             <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: '1rem', alignItems: 'flex-end' }}> | ||||
|                 <Select options={ELECCION_OPTIONS} value={selectedEleccion} onChange={(opt) => { setSelectedEleccion(opt!); setSelectedCategoria(null); }} /> | ||||
|                 <Select options={categoriaOptions} value={selectedCategoria} onChange={setSelectedCategoria} placeholder="Seleccione Categoría..." isDisabled={!selectedEleccion} /> | ||||
|                 <Select options={categoriaOptions} value={selectedCategoria} onChange={setSelectedCategoria} placeholder="Seleccione Categoría..." /> | ||||
|                 <Select | ||||
|                     options={agrupaciones.map(a => ({ value: a.id, label: a.nombre, ...a }))} | ||||
|                     getOptionValue={opt => opt.id} | ||||
|   | ||||
| @@ -21,7 +21,6 @@ const AMBITO_LEVEL_OPTIONS = [ | ||||
| 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<ProvinciaSimple | null>(null); | ||||
| @@ -30,33 +29,37 @@ export const LogoOverridesManager = () => { | ||||
|     const [selectedAgrupacion, setSelectedAgrupacion] = useState<AgrupacionPolitica | null>(null); | ||||
|     const [logoUrl, setLogoUrl] = useState(''); | ||||
|  | ||||
|     // --- QUERIES --- | ||||
|     const { data: provincias = [] } = useQuery<ProvinciaSimple[]>({ queryKey: ['provinciasForAdmin'], queryFn: getProvinciasForAdmin }); | ||||
|     const { data: municipios = [] } = useQuery<MunicipioSimple[]>({ queryKey: ['municipiosForAdmin'], queryFn: getMunicipiosForAdmin }); | ||||
|     const { data: agrupaciones = [] } = useQuery<AgrupacionPolitica[]>({ queryKey: ['agrupaciones'], queryFn: getAgrupaciones }); | ||||
|  | ||||
|     const { data: logos = [] } = useQuery<LogoAgrupacionCategoria[]>({ | ||||
|         queryKey: ['logos', selectedEleccion.value], | ||||
|         queryFn: () => getLogos(selectedEleccion.value) | ||||
|         queryKey: ['allLogos'], | ||||
|         queryFn: () => Promise.all([getLogos(0), getLogos(1), getLogos(2)]).then(res => res.flat()), | ||||
|     }); | ||||
|  | ||||
|     // --- LÓGICA DE SELECTORES DINÁMICOS --- | ||||
|     const categoriaOptions = selectedEleccion.value === 2 ? CATEGORIAS_NACIONALES_OPTIONS : CATEGORIAS_PROVINCIALES_OPTIONS; | ||||
|     const categoriaOptions = useMemo(() => { | ||||
|         if (selectedEleccion.value === 2) return CATEGORIAS_NACIONALES_OPTIONS; | ||||
|         if (selectedEleccion.value === 1) return CATEGORIAS_PROVINCIALES_OPTIONS; | ||||
|         return [...CATEGORIAS_NACIONALES_OPTIONS, ...CATEGORIAS_PROVINCIALES_OPTIONS]; | ||||
|     }, [selectedEleccion]); | ||||
|  | ||||
|     const getAmbitoId = () => { | ||||
|         if (selectedAmbitoLevel.value === 'municipio' && selectedMunicipio) return parseInt(selectedMunicipio.id); | ||||
|         if (selectedAmbitoLevel.value === 'provincia' && selectedProvincia) return parseInt(selectedProvincia.id); | ||||
|         return 0; | ||||
|         return null; | ||||
|     }; | ||||
|  | ||||
|     const currentLogo = useMemo(() => { | ||||
|         if (!selectedAgrupacion || !selectedCategoria) return ''; | ||||
|         const ambitoId = getAmbitoId(); | ||||
|         return logos.find(l => | ||||
|             l.eleccionId === selectedEleccion.value && | ||||
|             l.ambitoGeograficoId === ambitoId && | ||||
|             l.agrupacionPoliticaId === selectedAgrupacion.id && | ||||
|             l.categoriaId === selectedCategoria.value | ||||
|         )?.logoUrl || ''; | ||||
|     }, [logos, selectedAmbitoLevel, selectedProvincia, selectedMunicipio, selectedAgrupacion, selectedCategoria]); | ||||
|     }, [logos, selectedEleccion, selectedAmbitoLevel, selectedProvincia, selectedMunicipio, selectedAgrupacion, selectedCategoria]); | ||||
|  | ||||
|     useEffect(() => { setLogoUrl(currentLogo || ''); }, [currentLogo]); | ||||
|  | ||||
| @@ -68,11 +71,11 @@ export const LogoOverridesManager = () => { | ||||
|             agrupacionPoliticaId: selectedAgrupacion.id, | ||||
|             categoriaId: selectedCategoria.value, | ||||
|             ambitoGeograficoId: getAmbitoId(), | ||||
|             logoUrl: logoUrl || null | ||||
|             logoUrl: logoUrl.trim() || null | ||||
|         }; | ||||
|         try { | ||||
|             await updateLogos([newLogoEntry]); | ||||
|             queryClient.invalidateQueries({ queryKey: ['logos', selectedEleccion.value] }); | ||||
|             queryClient.invalidateQueries({ queryKey: ['allLogos'] }); | ||||
|             alert('Override de logo guardado.'); | ||||
|         } catch { alert('Error al guardar.'); } | ||||
|     }; | ||||
| @@ -83,7 +86,7 @@ export const LogoOverridesManager = () => { | ||||
|             <p>Configure una imagen específica para un partido en un contexto determinado.</p> | ||||
|             <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: '1rem', alignItems: 'flex-end' }}> | ||||
|                 <Select options={ELECCION_OPTIONS} value={selectedEleccion} onChange={(opt) => { setSelectedEleccion(opt!); setSelectedCategoria(null); }} /> | ||||
|                 <Select options={categoriaOptions} value={selectedCategoria} onChange={setSelectedCategoria} placeholder="Seleccione Categoría..." isDisabled={!selectedEleccion} /> | ||||
|                 <Select options={categoriaOptions} value={selectedCategoria} onChange={setSelectedCategoria} placeholder="Seleccione Categoría..." /> | ||||
|                 <Select | ||||
|                     options={agrupaciones.map(a => ({ value: a.id, label: a.nombre, ...a }))} | ||||
|                     getOptionValue={opt => opt.id} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user