'use client'; import { useState, useEffect } from 'react'; import { Team } from '@/types'; export default function Teams() { const [teams, setTeams] = useState([]); const [isLoading, setIsLoading] = useState(true); const [newTeamName, setNewTeamName] = useState(''); const [editingTeam, setEditingTeam] = useState(null); const [editingName, setEditingName] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); useEffect(() => { fetchTeams(); }, []); const fetchTeams = async () => { setIsLoading(true); try { const res = await fetch('/api/teams'); const data = await res.json(); setTeams(data); } catch (error) { console.error('Error fetching teams:', error); } finally { setIsLoading(false); } }; const handleAddTeam = async (e: React.FormEvent) => { e.preventDefault(); if (!newTeamName.trim()) return; setIsSubmitting(true); try { const res = await fetch('/api/teams', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ team_name: newTeamName }), }); if (res.ok) { setNewTeamName(''); fetchTeams(); } else { const error = await res.json(); alert(`Error adding team: ${error.error}`); } } catch (error) { console.error('Error adding team:', error); alert('Failed to add team'); } finally { setIsSubmitting(false); } }; const handleUpdateTeam = async (teamId: number) => { if (!editingName.trim()) return; try { const res = await fetch(`/api/teams/${teamId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ team_name: editingName }), }); if (res.ok) { setEditingTeam(null); setEditingName(''); fetchTeams(); } else { const error = await res.json(); alert(`Error updating team: ${error.error}`); } } catch (error) { console.error('Error updating team:', error); alert('Failed to update team'); } }; const handleDeleteTeam = async (teamId: number) => { if (!confirm('Are you sure you want to delete this team? This will also delete all associated streams.')) { return; } try { const res = await fetch(`/api/teams/${teamId}`, { method: 'DELETE', }); if (res.ok) { fetchTeams(); } else { const error = await res.json(); alert(`Error deleting team: ${error.error}`); } } catch (error) { console.error('Error deleting team:', error); alert('Failed to delete team'); } }; const startEditing = (team: Team) => { setEditingTeam(team); setEditingName(team.team_name); }; const cancelEditing = () => { setEditingTeam(null); setEditingName(''); }; return (
{/* Title */}

Team Management

Organize your streams by creating and managing teams

{/* Add New Team */}

Add New Team

setNewTeamName(e.target.value)} placeholder="Enter team name" className="input" style={{ flex: 1 }} required />
{/* Teams List */}

Existing Teams

{isLoading ? (
Loading teams...
) : teams.length === 0 ? (
No teams found
Create your first team above!
) : (
{teams.map((team) => (
{editingTeam?.team_id === team.team_id ? (
setEditingName(e.target.value)} className="input" style={{ flex: 1 }} autoFocus />
) : (
{team.team_name.charAt(0).toUpperCase()}
{team.team_name}
ID: {team.team_id}
)}
))}
)}
); }