"use client"; import { useState, useEffect } from "react"; import { useSession } from "next-auth/react"; import { useRouter } from "next/navigation"; import Link from "next/link"; const styles = { navbar: { position: "fixed", bottom: 0, width: "100%", backgroundColor: "#fff", borderTop: "1px solid #ccc", padding: "10px 0", textAlign: "center", }, }; export default function CreateTeam() { const { data: session, status } = useSession(); const [teamName, setTeamName] = useState(""); const [error, setError] = useState(""); const [isLoading, setIsLoading] = useState(true); const router = useRouter(); useEffect(() => { if (status === "authenticated") { if (session.user.teamId) { router.push(`/team/${session.user.teamId}`); } else { setIsLoading(false); } } else if (status === "unauthenticated") { router.push("/login"); } }, [status, session, router]); const handleCreateTeam = async (e) => { e.preventDefault(); setIsLoading(true); setError(""); try { const response = await fetch("/api/teams", { method: "POST", headers: { "Content-Type": "application/json", }, credentials: "include", // Important for cookies body: JSON.stringify({ name: teamName }), }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || "Failed to create team"); } // Force a session update to get the new teamId try { // Update the client-side session data with the new teamId if (session && session.user) { session.user.teamId = data.teamId; } console.log("Team created successfully, redirecting to:", `/team/${data.teamId}`); // Add a small delay to allow session update setTimeout(() => { router.push(`/team/${data.teamId}`); }, 100); } catch (navError) { console.error("Navigation error:", navError); // Fallback to direct navigation window.location.href = `/team/${data.teamId}`; } } catch (err) { setError(err.message); setIsLoading(false); } }; if (isLoading) { return
Loading...
; } return (
Create Team

Create a New Team

setTeamName(e.target.value)} required className="auth-input" />
{error &&

{error}

}
🏠
📊
🏆
👤
); }