'use client'; // Mark as a Client Component import { useState } from 'react'; import { useSession } from 'next-auth/react'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; export default function CreateTeam() { const { data: session, status } = useSession(); const [teamName, setTeamName] = useState(''); const [error, setError] = useState(''); const [isLoading, setIsLoading] = useState(false); const router = useRouter(); // Redirect if not authenticated if (status === 'unauthenticated') { router.push('/login'); return null; } // Redirect if user is admin if (status === 'authenticated' && session?.user?.role === 'admin') { router.push('/admin'); return null; } const handleCreateTeam = async (e) => { e.preventDefault(); setIsLoading(true); setError(''); try { const response = await fetch('/api/teams/create', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ name: teamName, userId: session.user.id }), }); const result = await response.json(); if (result.success) { router.push(`/team/${result.teamId}`); } else { setError(result.error || 'Failed to create team'); } } catch (err) { setError('An unexpected error occurred'); console.error(err); } finally { setIsLoading(false); } }; if (status === 'loading') { return
{error}
}