"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; export default function TeamList({ teams, onJoinTeam, onRefresh }) { const [joinCode, setJoinCode] = useState(""); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const router = useRouter(); const handleJoinTeam = async (e) => { e.preventDefault(); if (!joinCode.trim()) { setError("Please enter a team code"); return; } setError(""); setLoading(true); try { const response = await fetch("/api/teams/join", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ code: joinCode.trim() }), }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || "Failed to join team"); } // Call the onJoinTeam callback with the joined team if (onJoinTeam) { onJoinTeam(data); } // Refresh the team list if (onRefresh) { onRefresh(); } router.refresh(); } catch (err) { setError(err.message); } finally { setLoading(false); } }; return (

Join Existing Team

setJoinCode(e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Enter team code" required />
{error &&

{error}

}
{teams && teams.length > 0 ? (

Available Teams:

) : (

No teams available to join

)}
); }