Fix Overrides Logos Y Candidatos

This commit is contained in:
2025-10-22 15:53:40 -03:00
parent f89903feda
commit 814b24cefb
3 changed files with 32 additions and 22 deletions

View File

@@ -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}