11import React , { useState , useEffect , useMemo } from 'react' ;
2+ import { format } from 'date-fns' ;
3+
24import {
35 SafeAreaView ,
46 View ,
@@ -17,35 +19,46 @@ import { Input } from '@/components/ui/Input';
1719import { router } from 'expo-router' ;
1820import { useAuth } from '@/contexts/AuthContext' ;
1921const { width } = Dimensions . get ( 'window' ) ;
20-
22+ const formatDate = ( date : Date ) => {
23+ return format ( new Date ( date ) , 'MMM d, yyyy' ) ; // e.g. Jun 9, 2025
24+ } ;
2125interface WorkoutItemProps {
2226 workout : Workout ;
2327 onPress : ( ) => void ;
2428}
2529
2630const WorkoutItem = ( { workout, onPress } : WorkoutItemProps ) => {
27- const { workouts, currentWorkout, exerSets, exerReps, exerWeights, startWorkout, getWorkouts, getExercises, getExercises_2} = useWorkout ( ) ;
28- const formatDate = ( date : Date ) => {
29- return date . toLocaleDateString ( 'en-US' , {
30- month : 'short' ,
31- day : 'numeric' ,
32- year : 'numeric'
33- } ) ;
34- } ;
31+ const { getExercises } = useWorkout ( ) ;
32+ const [ stats , setStats ] = useState ( { sets : 0 , reps : 0 , weight : 0 } ) ;
33+ const { token, isAuthenticated, isLoading } = useAuth ( ) ; //token of user
3534
36- //getWorkouts();
37- //console.log(workout.id);
38-
39- //getExercises(workout.id); // Set the exercises
40- //console.log(getExercises_2()); // Get the exercises
41-
42- //const exercises = getExercises_2();
35+ useEffect ( ( ) => {
36+ const loadStats = async ( ) => {
37+ await getExercises ( workout . id ) ; // this updates global `exercises`, which we can't reliably read here
38+ const res = await fetch ( `http://localhost:8000/api/v1/workouts/${ workout . id } /exercises` , {
39+ headers : {
40+ Authorization : `Bearer ${ token } ` ,
41+ "Content-Type" : "application/json" ,
42+ } ,
43+ } ) ;
44+ const json = await res . json ( ) ;
45+ //console.log(json);
46+ let sets = 0 , reps = 0 , weight = 0 ;
47+ for ( const ex of json ) {
48+ sets += ex . sets ?? 0 ;
49+ reps += ex . reps ?? 0 ;
50+ weight += ex . weight ?? 0 ;
51+ }
52+
53+ setStats ( { sets, reps, weight } ) ;
54+ } ;
55+
56+ loadStats ( ) ;
57+ } , [ workout . id ] ) ;
4358
44- //for (const exercise of exercises) {
45- // exerSets += exercise.sets || 0;
46- // exerReps += exercise.reps || 0;
47- // exerWeight += exercise.weight || 0;
48- //}
59+ const formatDate = ( date : Date ) => {
60+ return format ( new Date ( date ) , 'MMM d, yyyy' ) ;
61+ } ;
4962
5063 return (
5164 < TouchableOpacity style = { styles . workoutItem } onPress = { onPress } >
@@ -54,9 +67,9 @@ const WorkoutItem = ({ workout, onPress }: WorkoutItemProps) => {
5467 < Text style = { styles . workoutDate } > { formatDate ( workout . date ) } </ Text >
5568 </ View >
5669 < View style = { styles . workoutStats } >
57- < Text style = { styles . workoutStat } > { exerSets } sets</ Text >
58- < Text style = { styles . workoutStat } > { exerReps } reps</ Text >
59- < Text style = { styles . workoutStat } > { exerWeights } weights</ Text >
70+ < Text style = { styles . workoutStat } > { stats . sets } sets</ Text >
71+ < Text style = { styles . workoutStat } > { stats . reps } reps</ Text >
72+ < Text style = { styles . workoutStat } > { stats . weight } weights</ Text >
6073 </ View >
6174 </ TouchableOpacity >
6275 ) ;
0 commit comments