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'; | import { CATEGORIAS_NACIONALES_OPTIONS, CATEGORIAS_PROVINCIALES_OPTIONS } from '../constants/categorias'; | ||||||
|  |  | ||||||
| const ELECCION_OPTIONS = [ | const ELECCION_OPTIONS = [ | ||||||
|  |     { value: 0, label: 'General (Todas las elecciones)' }, | ||||||
|     { value: 2, label: 'Elecciones Nacionales' }, |     { value: 2, label: 'Elecciones Nacionales' }, | ||||||
|     { value: 1, label: 'Elecciones Provinciales' } |     { value: 1, label: 'Elecciones Provinciales' } | ||||||
| ]; | ]; | ||||||
| @@ -31,12 +32,17 @@ export const CandidatoOverridesManager = () => { | |||||||
|     const { data: provincias = [] } = useQuery<ProvinciaSimple[]>({ queryKey: ['provinciasForAdmin'], queryFn: getProvinciasForAdmin }); |     const { data: provincias = [] } = useQuery<ProvinciaSimple[]>({ queryKey: ['provinciasForAdmin'], queryFn: getProvinciasForAdmin }); | ||||||
|     const { data: municipios = [] } = useQuery<MunicipioSimple[]>({ queryKey: ['municipiosForAdmin'], queryFn: getMunicipiosForAdmin }); |     const { data: municipios = [] } = useQuery<MunicipioSimple[]>({ queryKey: ['municipiosForAdmin'], queryFn: getMunicipiosForAdmin }); | ||||||
|     const { data: agrupaciones = [] } = useQuery<AgrupacionPolitica[]>({ queryKey: ['agrupaciones'], queryFn: getAgrupaciones }); |     const { data: agrupaciones = [] } = useQuery<AgrupacionPolitica[]>({ queryKey: ['agrupaciones'], queryFn: getAgrupaciones }); | ||||||
|  |  | ||||||
|     const { data: candidatos = [] } = useQuery<CandidatoOverride[]>({ |     const { data: candidatos = [] } = useQuery<CandidatoOverride[]>({ | ||||||
|         queryKey: ['candidatos', selectedEleccion.value], |         queryKey: ['allCandidatos'], | ||||||
|         queryFn: () => getCandidatos(selectedEleccion.value), |         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 = () => { |     const getAmbitoId = () => { | ||||||
|         if (selectedAmbitoLevel.value === 'municipio' && selectedMunicipio) return parseInt(selectedMunicipio.id); |         if (selectedAmbitoLevel.value === 'municipio' && selectedMunicipio) return parseInt(selectedMunicipio.id); | ||||||
| @@ -48,11 +54,12 @@ export const CandidatoOverridesManager = () => { | |||||||
|         if (!selectedAgrupacion || !selectedCategoria) return ''; |         if (!selectedAgrupacion || !selectedCategoria) return ''; | ||||||
|         const ambitoId = getAmbitoId(); |         const ambitoId = getAmbitoId(); | ||||||
|         return candidatos.find(c => |         return candidatos.find(c => | ||||||
|  |             c.eleccionId === selectedEleccion.value && | ||||||
|             c.ambitoGeograficoId === ambitoId && |             c.ambitoGeograficoId === ambitoId && | ||||||
|             c.agrupacionPoliticaId === selectedAgrupacion.id && |             c.agrupacionPoliticaId === selectedAgrupacion.id && | ||||||
|             c.categoriaId === selectedCategoria.value |             c.categoriaId === selectedCategoria.value | ||||||
|         )?.nombreCandidato || ''; |         )?.nombreCandidato || ''; | ||||||
|     }, [candidatos, selectedAmbitoLevel, selectedProvincia, selectedMunicipio, selectedAgrupacion, selectedCategoria]); |     }, [candidatos, selectedEleccion, selectedAmbitoLevel, selectedProvincia, selectedMunicipio, selectedAgrupacion, selectedCategoria]); | ||||||
|  |  | ||||||
|     useEffect(() => { setNombreCandidato(currentCandidato || ''); }, [currentCandidato]); |     useEffect(() => { setNombreCandidato(currentCandidato || ''); }, [currentCandidato]); | ||||||
|  |  | ||||||
| @@ -64,11 +71,11 @@ export const CandidatoOverridesManager = () => { | |||||||
|             agrupacionPoliticaId: selectedAgrupacion.id, |             agrupacionPoliticaId: selectedAgrupacion.id, | ||||||
|             categoriaId: selectedCategoria.value, |             categoriaId: selectedCategoria.value, | ||||||
|             ambitoGeograficoId: getAmbitoId(), |             ambitoGeograficoId: getAmbitoId(), | ||||||
|             nombreCandidato: nombreCandidato || null |             nombreCandidato: nombreCandidato.trim() || null | ||||||
|         }; |         }; | ||||||
|         try { |         try { | ||||||
|             await updateCandidatos([newCandidatoEntry]); |             await updateCandidatos([newCandidatoEntry]); | ||||||
|             queryClient.invalidateQueries({ queryKey: ['candidatos', selectedEleccion.value] }); |             queryClient.invalidateQueries({ queryKey: ['allCandidatos'] }); | ||||||
|             alert('Override de candidato guardado.'); |             alert('Override de candidato guardado.'); | ||||||
|         } catch (error) { |         } catch (error) { | ||||||
|             console.error(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> |             <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' }}> |             <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={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 |                 <Select | ||||||
|                     options={agrupaciones.map(a => ({ value: a.id, label: a.nombre, ...a }))} |                     options={agrupaciones.map(a => ({ value: a.id, label: a.nombre, ...a }))} | ||||||
|                     getOptionValue={opt => opt.id} |                     getOptionValue={opt => opt.id} | ||||||
|   | |||||||
| @@ -21,7 +21,6 @@ const AMBITO_LEVEL_OPTIONS = [ | |||||||
| export const LogoOverridesManager = () => { | export const LogoOverridesManager = () => { | ||||||
|     const queryClient = useQueryClient(); |     const queryClient = useQueryClient(); | ||||||
|  |  | ||||||
|     // --- ESTADOS --- |  | ||||||
|     const [selectedEleccion, setSelectedEleccion] = useState(ELECCION_OPTIONS[0]); |     const [selectedEleccion, setSelectedEleccion] = useState(ELECCION_OPTIONS[0]); | ||||||
|     const [selectedAmbitoLevel, setSelectedAmbitoLevel] = useState(AMBITO_LEVEL_OPTIONS[0]); |     const [selectedAmbitoLevel, setSelectedAmbitoLevel] = useState(AMBITO_LEVEL_OPTIONS[0]); | ||||||
|     const [selectedProvincia, setSelectedProvincia] = useState<ProvinciaSimple | null>(null); |     const [selectedProvincia, setSelectedProvincia] = useState<ProvinciaSimple | null>(null); | ||||||
| @@ -30,33 +29,37 @@ export const LogoOverridesManager = () => { | |||||||
|     const [selectedAgrupacion, setSelectedAgrupacion] = useState<AgrupacionPolitica | null>(null); |     const [selectedAgrupacion, setSelectedAgrupacion] = useState<AgrupacionPolitica | null>(null); | ||||||
|     const [logoUrl, setLogoUrl] = useState(''); |     const [logoUrl, setLogoUrl] = useState(''); | ||||||
|  |  | ||||||
|     // --- QUERIES --- |  | ||||||
|     const { data: provincias = [] } = useQuery<ProvinciaSimple[]>({ queryKey: ['provinciasForAdmin'], queryFn: getProvinciasForAdmin }); |     const { data: provincias = [] } = useQuery<ProvinciaSimple[]>({ queryKey: ['provinciasForAdmin'], queryFn: getProvinciasForAdmin }); | ||||||
|     const { data: municipios = [] } = useQuery<MunicipioSimple[]>({ queryKey: ['municipiosForAdmin'], queryFn: getMunicipiosForAdmin }); |     const { data: municipios = [] } = useQuery<MunicipioSimple[]>({ queryKey: ['municipiosForAdmin'], queryFn: getMunicipiosForAdmin }); | ||||||
|     const { data: agrupaciones = [] } = useQuery<AgrupacionPolitica[]>({ queryKey: ['agrupaciones'], queryFn: getAgrupaciones }); |     const { data: agrupaciones = [] } = useQuery<AgrupacionPolitica[]>({ queryKey: ['agrupaciones'], queryFn: getAgrupaciones }); | ||||||
|  |  | ||||||
|     const { data: logos = [] } = useQuery<LogoAgrupacionCategoria[]>({ |     const { data: logos = [] } = useQuery<LogoAgrupacionCategoria[]>({ | ||||||
|         queryKey: ['logos', selectedEleccion.value], |         queryKey: ['allLogos'], | ||||||
|         queryFn: () => getLogos(selectedEleccion.value) |         queryFn: () => Promise.all([getLogos(0), getLogos(1), getLogos(2)]).then(res => res.flat()), | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     // --- LÓGICA DE SELECTORES DINÁMICOS --- |     const categoriaOptions = useMemo(() => { | ||||||
|     const categoriaOptions = selectedEleccion.value === 2 ? CATEGORIAS_NACIONALES_OPTIONS : CATEGORIAS_PROVINCIALES_OPTIONS; |         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 = () => { |     const getAmbitoId = () => { | ||||||
|         if (selectedAmbitoLevel.value === 'municipio' && selectedMunicipio) return parseInt(selectedMunicipio.id); |         if (selectedAmbitoLevel.value === 'municipio' && selectedMunicipio) return parseInt(selectedMunicipio.id); | ||||||
|         if (selectedAmbitoLevel.value === 'provincia' && selectedProvincia) return parseInt(selectedProvincia.id); |         if (selectedAmbitoLevel.value === 'provincia' && selectedProvincia) return parseInt(selectedProvincia.id); | ||||||
|         return 0; |         return null; | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     const currentLogo = useMemo(() => { |     const currentLogo = useMemo(() => { | ||||||
|         if (!selectedAgrupacion || !selectedCategoria) return ''; |         if (!selectedAgrupacion || !selectedCategoria) return ''; | ||||||
|         const ambitoId = getAmbitoId(); |         const ambitoId = getAmbitoId(); | ||||||
|         return logos.find(l => |         return logos.find(l => | ||||||
|  |             l.eleccionId === selectedEleccion.value && | ||||||
|             l.ambitoGeograficoId === ambitoId && |             l.ambitoGeograficoId === ambitoId && | ||||||
|             l.agrupacionPoliticaId === selectedAgrupacion.id && |             l.agrupacionPoliticaId === selectedAgrupacion.id && | ||||||
|             l.categoriaId === selectedCategoria.value |             l.categoriaId === selectedCategoria.value | ||||||
|         )?.logoUrl || ''; |         )?.logoUrl || ''; | ||||||
|     }, [logos, selectedAmbitoLevel, selectedProvincia, selectedMunicipio, selectedAgrupacion, selectedCategoria]); |     }, [logos, selectedEleccion, selectedAmbitoLevel, selectedProvincia, selectedMunicipio, selectedAgrupacion, selectedCategoria]); | ||||||
|  |  | ||||||
|     useEffect(() => { setLogoUrl(currentLogo || ''); }, [currentLogo]); |     useEffect(() => { setLogoUrl(currentLogo || ''); }, [currentLogo]); | ||||||
|  |  | ||||||
| @@ -68,11 +71,11 @@ export const LogoOverridesManager = () => { | |||||||
|             agrupacionPoliticaId: selectedAgrupacion.id, |             agrupacionPoliticaId: selectedAgrupacion.id, | ||||||
|             categoriaId: selectedCategoria.value, |             categoriaId: selectedCategoria.value, | ||||||
|             ambitoGeograficoId: getAmbitoId(), |             ambitoGeograficoId: getAmbitoId(), | ||||||
|             logoUrl: logoUrl || null |             logoUrl: logoUrl.trim() || null | ||||||
|         }; |         }; | ||||||
|         try { |         try { | ||||||
|             await updateLogos([newLogoEntry]); |             await updateLogos([newLogoEntry]); | ||||||
|             queryClient.invalidateQueries({ queryKey: ['logos', selectedEleccion.value] }); |             queryClient.invalidateQueries({ queryKey: ['allLogos'] }); | ||||||
|             alert('Override de logo guardado.'); |             alert('Override de logo guardado.'); | ||||||
|         } catch { alert('Error al guardar.'); } |         } 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> |             <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' }}> |             <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={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 |                 <Select | ||||||
|                     options={agrupaciones.map(a => ({ value: a.id, label: a.nombre, ...a }))} |                     options={agrupaciones.map(a => ({ value: a.id, label: a.nombre, ...a }))} | ||||||
|                     getOptionValue={opt => opt.id} |                     getOptionValue={opt => opt.id} | ||||||
|   | |||||||
| @@ -3,8 +3,8 @@ | |||||||
| // Opciones para los selectores en el panel de administración | // Opciones para los selectores en el panel de administración | ||||||
| export const CATEGORIAS_ADMIN_OPTIONS = [ | export const CATEGORIAS_ADMIN_OPTIONS = [ | ||||||
|     // Nacionales |     // Nacionales | ||||||
|     { value: 1, label: 'Senadores Nacionales' }, |     { value: 2, label: 'Senadores Nacionales' }, | ||||||
|     { value: 2, label: 'Diputados Nacionales' }, |     { value: 3, label: 'Diputados Nacionales' }, | ||||||
|     // Provinciales |     // Provinciales | ||||||
|     { value: 5, label: 'Senadores Provinciales' }, |     { value: 5, label: 'Senadores Provinciales' }, | ||||||
|     { value: 6, label: 'Diputados Provinciales' }, |     { value: 6, label: 'Diputados Provinciales' }, | ||||||
| @@ -12,8 +12,8 @@ export const CATEGORIAS_ADMIN_OPTIONS = [ | |||||||
| ]; | ]; | ||||||
|  |  | ||||||
| export const CATEGORIAS_NACIONALES_OPTIONS = [ | export const CATEGORIAS_NACIONALES_OPTIONS = [ | ||||||
|     { value: 1, label: 'Senadores Nacionales' }, |     { value: 2, label: 'Senadores Nacionales' }, | ||||||
|     { value: 2, label: 'Diputados Nacionales' }, |     { value: 3, label: 'Diputados Nacionales' }, | ||||||
| ]; | ]; | ||||||
|  |  | ||||||
| export const CATEGORIAS_PROVINCIALES_OPTIONS = [ | export const CATEGORIAS_PROVINCIALES_OPTIONS = [ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user