import React, { useState, useEffect } from 'react'; import { useParams, useNavigate, Link } from 'react-router-dom'; import { ArrowLeftIcon } from '@heroicons/react/24/outline'; import { openCandApi, type CandidateDetails, type CandidateAssets, type CandidateRedesSociais, type CandidateExpenses, type CandidateIncome, ApiError } from '../../api'; import ElectionsComponent from './ElectionsComponent'; import AssetsComponent from './AssetsComponent'; import BasicCandidateInfoComponent from './BasicCandidateInfoComponent'; import SocialMediaComponent from './SocialMediaComponent'; import IncomeExpenseComponent from './IncomeExpenseComponent'; import Button from '../../shared/Button'; import RandomCandButton from '../../shared/RandomCandButton'; import ErrorPage from '../ErrorPage'; const CandidatePage: React.FC = () => { const { id } = useParams<{ id: string }>(); const navigate = useNavigate(); const [candidateDetails, setCandidateDetails] = useState(null); const [candidateAssets, setCandidateAssets] = useState(null); const [candidateRedesSociais, setCandidateRedesSociais] = useState(null); const [candidateExpenses, setCandidateExpenses] = useState(null); const [candidateIncome, setCandidateIncome] = useState(null); const [isLoadingDetails, setIsLoadingDetails] = useState(true); const [isLoadingAssets, setIsLoadingAssets] = useState(true); const [isLoadingRedesSociais, setIsLoadingRedesSociais] = useState(true); const [isLoadingExpenses, setIsLoadingExpenses] = useState(true); const [isLoadingIncome, setIsLoadingIncome] = useState(true); const [error, setError] = useState(null); useEffect(() => { if (!id) { navigate('/'); return; } // Fetch candidate details const fetchCandidateDetails = async () => { try { setIsLoadingDetails(true); const details = await openCandApi.getCandidateById(id); setCandidateDetails(details); } catch (err) { console.error('Error fetching candidate details:', err); if (err instanceof ApiError) { setError(`Erro ao carregar dados do candidato: ${err.message}`); } else { setError('Erro inesperado ao carregar dados do candidato'); } } finally { setIsLoadingDetails(false); } }; // Fetch candidate assets const fetchCandidateAssets = async () => { try { setIsLoadingAssets(true); const assets = await openCandApi.getCandidateAssets(id); setCandidateAssets(assets); } catch (err) { console.error('Error fetching candidate assets:', err); // Assets might not be available for all candidates, so we don't set error here } finally { setIsLoadingAssets(false); } }; // Fetch candidate social media const fetchCandidateRedesSociais = async () => { try { setIsLoadingRedesSociais(true); const redesSociais = await openCandApi.getCandidateRedesSociais(id); setCandidateRedesSociais(redesSociais); } catch (err) { console.error('Error fetching candidate social media:', err); // Social media might not be available for all candidates, so we don't set error here } finally { setIsLoadingRedesSociais(false); } }; // Fetch candidate expenses const fetchCandidateExpenses = async () => { try { setIsLoadingExpenses(true); const expenses = await openCandApi.getCandidateDepesas(id); setCandidateExpenses(expenses); } catch (err) { console.error('Error fetching candidate expenses:', err); // Expenses might not be available for all candidates, so we don't set error here } finally { setIsLoadingExpenses(false); } }; // Fetch candidate income const fetchCandidateIncome = async () => { try { setIsLoadingIncome(true); const income = await openCandApi.getCandidateReceitas(id); setCandidateIncome(income); } catch (err) { console.error('Error fetching candidate income:', err); // Income might not be available for all candidates, so we don't set error here } finally { setIsLoadingIncome(false); } }; fetchCandidateDetails(); fetchCandidateAssets(); fetchCandidateRedesSociais(); fetchCandidateExpenses(); fetchCandidateIncome(); }, [id, navigate]); if (error) { return ( ); } return (
{/* Header with back button */}
{candidateDetails && (

{candidateDetails.nome}

)}
{/* Left Column - Basic Information and Social Media */}
{/* Basic Information Panel */} {/* Social Media Panel */}
{/* Right Column - Elections and Assets */}
{/* Elections Panel */} {/* Assets Panel */} {/* Income and Expenses Panel */}
); }; export default CandidatePage;