import React, { useEffect, useState } from "react"; import { useParams, useNavigate } from "react-router-dom"; import { useUser } from "../hooks/useUser"; import { Box } from "@mui/material"; import { SetSnackType } from '../components/Snack'; import { LoadingComponent } from "../components/LoadingComponent"; import { User, Guest, Candidate } from 'types/types'; import { ApiClient } from "types/api-client"; interface CandidateRouteProps { guest?: Guest | null; user?: User | null; setSnack: SetSnackType, }; const CandidateRoute: React.FC = (props: CandidateRouteProps) => { const apiClient = new ApiClient(); const { setSnack } = props; const { username } = useParams<{ username: string }>(); const [candidate, setCandidate] = useState(null); const navigate = useNavigate(); useEffect(() => { if (candidate?.username === username || !username) { return; } const getCandidate = async (username: string) => { try { const result : Candidate = await apiClient.getCandidate(username); setCandidate(result); navigate('/chat'); } catch { setSnack(`Unable to obtain information for ${username}.`, "error"); } } getCandidate(username); }, [candidate, username, setCandidate]); if (candidate === null) { return ( ); } else { return (<>); } }; export { CandidateRoute };