import { useEffect, useState } from 'react'; import { Link, useNavigate } from 'react-router-dom'; import { FaPlus, FaPlay, FaEdit, FaTrash, FaFilter } from 'react-icons/fa'; import type { Routine } from '../types/models'; import { RoutineService } from '../services/api'; const WorkoutsPage = () => { const [routines, setRoutines] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [searchTerm, setSearchTerm] = useState(''); const navigate = useNavigate(); useEffect(() => { const fetchRoutines = async () => { try { setLoading(true); const data = await RoutineService.getAll(); setRoutines(data); setError(null); } catch (err) { console.error('Failed to fetch routines:', err); setError('Could not load workout routines. Please try again later.'); } finally { setLoading(false); } }; fetchRoutines(); }, []); const handleStartWorkout = (routine: Routine) => { // Navigate to new workout page with the selected routine navigate('/new-workout', { state: { routineId: routine.id } }); }; const handleDeleteRoutine = async (id: number) => { if (window.confirm('Are you sure you want to delete this routine?')) { try { await RoutineService.delete(id); setRoutines(routines.filter(routine => routine.id !== id)); } catch (err) { console.error('Failed to delete routine:', err); alert('Failed to delete routine. Please try again.'); } } }; const filteredRoutines = routines.filter(routine => routine.name.toLowerCase().includes(searchTerm.toLowerCase()) || routine.description.toLowerCase().includes(searchTerm.toLowerCase()) ); if (loading) { return (

Workouts

Loading routines...
); } if (error) { return (

Workouts

{error}
Create New Routine
); } return (

Workout Routines

{/* Search and Filter */}
setSearchTerm(e.target.value)} className="search-input" />
{/* Create New Button */}
Create New Routine
{/* Routines List */} {filteredRoutines.length > 0 ? (
{filteredRoutines.map(routine => (

{routine.name}

{routine.description}

))}
) : (
{searchTerm ? (

No routines found matching "{searchTerm}"

) : ( <>

You haven't created any workout routines yet.

Create your first routine to get started!

Create Routine )}
)}
); }; export default WorkoutsPage;