Skip to content

Commit 227a3a9

Browse files
committed
Fixed Recent Workouts
1 parent 5214c70 commit 227a3a9

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

KonditionExpo/app/(tabs)/progress.tsx

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import React, { useState, useEffect, useMemo } from 'react';
2+
import { format } from 'date-fns';
3+
24
import {
35
SafeAreaView,
46
View,
@@ -17,35 +19,46 @@ import { Input } from '@/components/ui/Input';
1719
import { router } from 'expo-router';
1820
import { useAuth } from '@/contexts/AuthContext';
1921
const { 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+
};
2125
interface WorkoutItemProps {
2226
workout: Workout;
2327
onPress: () => void;
2428
}
2529

2630
const 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
);

KonditionExpo/package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

KonditionExpo/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@react-navigation/native": "^7.1.6",
2222
"axios": "^1.9.0",
2323
"d3-shape": "^3.2.0",
24+
"date-fns": "^4.1.0",
2425
"expo": "^53.0.9",
2526
"expo-blur": "~14.1.4",
2627
"expo-constants": "~17.1.6",

0 commit comments

Comments
 (0)