"use client";
import { useState } 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(false);
const router = useRouter();
if (status === "loading") {
return
Loading...
;
}
if (!session || status === "unauthenticated") {
router.replace("/login");
return null;
}
if (session?.user?.role === "admin") {
router.replace("/admin");
return null;
}
const handleCreateTeam = async (e) => {
e.preventDefault();
if (isLoading || !teamName.trim()) return;
setIsLoading(true);
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) {
// Update the user's session with the new teamId
await fetch("/api/auth/session", {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
teamId: result.teamId,
}),
});
router.push(`/team/${result.teamId}`);
} else {
setError(result.error || "Failed to create team");
}
} catch (err) {
console.error("Team creation error:", err);
setError("An unexpected error occurred");
} finally {
setIsLoading(false);
}
};
return (
Create a New Team
{error &&
{error}
}
);
}