import React, { useEffect, useState, FormEvent } from 'react'; import { api, Schedule } from '../api'; import { useAuth } from '../auth'; export default function Schedules() { const { role } = useAuth(); const [schedules, setSchedules] = useState([]); const [error, setError] = useState(''); const [showForm, setShowForm] = useState(false); const [form, setForm] = useState({ title: '', starts_at: '', ends_at: '', notes: '' }); useEffect(() => { api.listSchedules().then(setSchedules).catch(() => setError('Could not load schedules.')); }, []); async function handleCreate(e: FormEvent) { e.preventDefault(); setError(''); try { const sc = await api.createSchedule(form); setSchedules(prev => [...prev, sc]); setForm({ title: '', starts_at: '', ends_at: '', notes: '' }); setShowForm(false); } catch (err: any) { setError(err.message); } } async function handleDelete(id: number) { if (!window.confirm('Delete this schedule?')) return; try { await api.deleteSchedule(id); setSchedules(prev => prev.filter(s => s.id !== id)); } catch (err: any) { setError(err.message); } } return (

Schedules

{role === 'admin' && ( )}
{error &&

{error}

} {showForm && (

New Shift