"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 (
No teams available to join
)}