'use client'; import { useState, useEffect } from 'react'; import { useParams, useRouter } from 'next/navigation'; import Dropdown from '@/components/Dropdown'; import { Team } from '@/types'; type Stream = { id: number; name: string; obs_source_name: string; url: string; team_id: number | null; }; export default function EditStream() { const params = useParams(); const router = useRouter(); const streamId = params.id as string; const [formData, setFormData] = useState<{ name: string; obs_source_name: string; url: string; team_id: number | null; }>({ name: '', obs_source_name: '', url: '', team_id: null, }); const [teams, setTeams] = useState([]); const [message, setMessage] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); const [isLoading, setIsLoading] = useState(true); const [stream, setStream] = useState(null); // Fetch stream data and teams useEffect(() => { const fetchData = async () => { try { const [streamRes, teamsRes] = await Promise.all([ fetch(`/api/streams/${streamId}`), fetch('/api/teams') ]); if (!streamRes.ok) { throw new Error('Stream not found'); } const [streamData, teamsData] = await Promise.all([ streamRes.json(), teamsRes.json() ]); setStream(streamData); setFormData({ name: streamData.name, obs_source_name: streamData.obs_source_name, url: streamData.url, team_id: streamData.team_id, }); // Map teams for dropdown setTeams( teamsData.map((team: Team) => ({ id: team.team_id, name: team.team_name, })) ); } catch (error) { console.error('Failed to fetch data:', error); setMessage('Failed to load stream data'); } finally { setIsLoading(false); } }; if (streamId) { fetchData(); } }, [streamId]); const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData((prev) => ({ ...prev, [name]: value })); }; const handleTeamSelect = (teamId: number) => { setFormData((prev) => ({ ...prev, team_id: teamId })); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setMessage(''); setIsSubmitting(true); try { const response = await fetch(`/api/streams/${streamId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData), }); const data = await response.json(); if (response.ok) { setMessage('Stream updated successfully!'); // Redirect back to home after a short delay setTimeout(() => { router.push('/'); }, 1500); } else { setMessage(data.error || 'Something went wrong.'); } } catch (error) { console.error('Error updating stream:', error); setMessage('Failed to update stream.'); } finally { setIsSubmitting(false); } }; const handleDelete = async () => { if (!confirm('Are you sure you want to delete this stream? This action cannot be undone.')) { return; } try { const response = await fetch(`/api/streams/${streamId}`, { method: 'DELETE', }); const data = await response.json(); if (response.ok) { setMessage('Stream deleted successfully!'); // Redirect back to home after a short delay setTimeout(() => { router.push('/'); }, 1500); } else { setMessage(data.error || 'Failed to delete stream.'); } } catch (error) { console.error('Error deleting stream:', error); setMessage('Failed to delete stream.'); } }; if (isLoading) { return (
Loading stream data...
); } if (!stream) { return (

Stream Not Found

The requested stream could not be found.

); } return (
{/* Title */}

Edit Stream

Update the details for "{stream.name}"

{/* Form */}
{/* Stream Name */}
{/* OBS Source Name */}
{/* URL */}
{/* Team Selection */}
{/* Action Buttons */}
{/* Success/Error Message */} {message && (
{message.includes('successfully') ? ( ) : ( )}
{message}
)}
); }