import { useState, useEffect } from 'react' import { useParams, useNavigate, Link } from 'react-router-dom' import { marathonsApi } from '@/api' import type { MarathonPublicInfo } from '@/types' import { useAuthStore } from '@/store/auth' import { Button, Card, CardContent, CardHeader, CardTitle } from '@/components/ui' import { Users, Loader2, Trophy, UserPlus, LogIn } from 'lucide-react' export function InvitePage() { const { code } = useParams<{ code: string }>() const navigate = useNavigate() const { isAuthenticated, setPendingInviteCode } = useAuthStore() const [marathon, setMarathon] = useState(null) const [isLoading, setIsLoading] = useState(true) const [isJoining, setIsJoining] = useState(false) const [error, setError] = useState(null) useEffect(() => { loadMarathon() }, [code]) const loadMarathon = async () => { if (!code) return try { const data = await marathonsApi.getByCode(code) setMarathon(data) } catch { setError('Марафон не найден или ссылка недействительна') } finally { setIsLoading(false) } } const handleJoin = async () => { if (!code) return setIsJoining(true) try { const joined = await marathonsApi.join(code) navigate(`/marathons/${joined.id}`) } catch (err: unknown) { const apiError = err as { response?: { data?: { detail?: string } } } const detail = apiError.response?.data?.detail if (detail === 'Already joined this marathon') { // Already a member, just redirect navigate(`/marathons/${marathon?.id}`) } else { setError(detail || 'Не удалось присоединиться') } } finally { setIsJoining(false) } } const handleAuthRedirect = (path: string) => { if (code) { setPendingInviteCode(code) } navigate(path) } if (isLoading) { return (
) } if (error || !marathon) { return (
{error || 'Марафон не найден'}
) } const statusText = { preparing: 'Подготовка', active: 'Активен', finished: 'Завершён', }[marathon.status] return (
Приглашение в марафон {/* Marathon info */}

{marathon.title}

{marathon.description && (

{marathon.description}

)}
{marathon.participants_count} участников {statusText}

Организатор: {marathon.creator_nickname}

{marathon.status === 'finished' ? (
Этот марафон уже завершён
) : isAuthenticated ? ( /* Authenticated - show join button */ ) : ( /* Not authenticated - show login/register options */

Чтобы присоединиться, войдите или зарегистрируйтесь

)}
) }