Skip to content

Commit 02dc835

Browse files
committed
Fixed History Tab to show exercises now
1 parent 639b50a commit 02dc835

File tree

4 files changed

+87
-20
lines changed

4 files changed

+87
-20
lines changed

KonditionExpo/app/(tabs)/history.tsx

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,30 @@ import {
1111
Alert,
1212
} from 'react-native';
1313
import { router } from 'expo-router';
14-
import { apiService, WorkoutResponse } from '@/services/api';
14+
import { apiService } from '@/services/api';
1515
import { useAuth } from '@/contexts/AuthContext';
1616
import axios from 'axios';
1717

18+
export type Exercise = {
19+
id: string;
20+
name: string;
21+
sets?: number;
22+
reps?: number;
23+
weight?: number;
24+
category: string;
25+
};
26+
27+
export type WorkoutResponse = {
28+
id: string;
29+
name: string;
30+
description?: string;
31+
is_completed: boolean;
32+
created_at: string;
33+
completed_date?: string;
34+
duration_minutes?: number;
35+
exercises: Exercise[]; // <-- include this
36+
};
37+
1838
const WorkoutHistoryScreen = () => {
1939
const { user } = useAuth();
2040
const [workouts, setWorkouts] = useState<WorkoutResponse[]>([]);
@@ -34,7 +54,7 @@ const WorkoutHistoryScreen = () => {
3454

3555
//const workoutData = await apiService.getWorkouts(); - Uses old api.ts, idk
3656

37-
const response = await axios.get('http://localhost:8000/api/v1/workouts/', {
57+
const response = await axios.get('http://localhost:8000/api/v1/workouts/exercises/', {
3858
headers: {
3959
Authorization: `Bearer ${token}`,
4060
},
@@ -95,54 +115,66 @@ const WorkoutHistoryScreen = () => {
95115
return workout.is_completed ? '#4CAF50' : '#FF9800';
96116
};
97117

98-
const handleWorkoutPress = (workout: WorkoutResponse) => {
118+
/*const handleWorkoutPress = (workout: WorkoutResponse) => {
99119
// Navigate to the detailed workout view
100120
router.push({
101121
pathname: '/workout-detail',
102122
params: { workoutId: workout.id }
103123
});
104-
};
124+
}; Not needed */
105125

106126
const renderWorkoutItem = (workout: WorkoutResponse) => (
107-
<TouchableOpacity
108-
key={workout.id}
109-
style={styles.workoutCard}
110-
onPress={() => handleWorkoutPress(workout)}
111-
>
127+
<View key={workout.id} style={styles.workoutCard}>
128+
{/* Workout Header */}
112129
<View style={styles.workoutHeader}>
113130
<Text style={styles.workoutName}>{workout.name}</Text>
114131
<View style={[styles.statusBadge, { backgroundColor: getStatusColor(workout) }]}>
115132
<Text style={styles.statusText}>{getCompletionStatus(workout)}</Text>
116133
</View>
117134
</View>
118-
135+
136+
{/* Workout Date */}
119137
<Text style={styles.workoutDate}>
120138
{formatDate(workout.completed_date || workout.created_at)}
121139
</Text>
122-
140+
141+
{/* Workout Metadata */}
123142
<View style={styles.workoutDetails}>
124143
<View style={styles.detailItem}>
125144
<Text style={styles.detailLabel}>Duration</Text>
126145
<Text style={styles.detailValue}>{formatDuration(workout.duration_minutes)}</Text>
127146
</View>
128-
147+
129148
<View style={styles.detailItem}>
130149
<Text style={styles.detailLabel}>Exercises</Text>
131-
<Text style={styles.detailValue}>{workout.exercise_count || 0}</Text>
150+
<Text style={styles.detailValue}>{workout.exercises?.length || 0}</Text>
132151
</View>
133152
</View>
134-
153+
154+
{/* Workout Description */}
135155
{workout.description && (
136156
<Text style={styles.workoutDescription} numberOfLines={2}>
137157
{workout.description}
138158
</Text>
139159
)}
140-
141-
<View style={styles.viewDetailsContainer}>
142-
<Text style={styles.viewDetailsText}>Tap to view exercises →</Text>
143-
</View>
144-
</TouchableOpacity>
160+
161+
{/* Exercise List */}
162+
{workout.exercises?.length > 0 && (
163+
<View style={{ marginTop: 12 }}>
164+
<Text style={{ fontWeight: '600', color: '#444', marginBottom: 4 }}>Exercises:</Text>
165+
{workout.exercises.map((ex) => (
166+
<View key={ex.id} style={{ marginBottom: 6, paddingLeft: 8 }}>
167+
<Text style={{ color: '#333' }}>
168+
{ex.name}{ex.sets || 0} × {ex.reps || 0} @ {ex.weight || 0} lbs
169+
</Text>
170+
</View>
171+
))}
172+
</View>
173+
)}
174+
</View>
145175
);
176+
177+
146178

147179
const renderEmptyState = () => (
148180
<View style={styles.emptyState}>

backend/app/api/routes/workouts.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
ExerciseCreate,
1717
ExerciseUpdate,
1818
ExercisePublic,
19+
WorkoutWithExercisesPublic
1920
)
2021
from app.crudFuncs import create_or_update_personal_best, update_personal_bests_after_workout
2122

@@ -191,6 +192,25 @@ def get_workout(
191192

192193
return workout
193194

195+
@router.get("/exercises/", response_model=List[WorkoutWithExercisesPublic])
196+
def get_workouts_with_exercises(
197+
session: SessionDep,
198+
current_user: CurrentUser,
199+
) -> Any:
200+
"""
201+
Get all workouts with their exercises for the current user.
202+
"""
203+
# Fetch workouts
204+
stmt = select(Workout).where(Workout.user_id == current_user.id)
205+
workouts = session.exec(stmt).all()
206+
207+
# Load and attach exercises manually
208+
for workout in workouts:
209+
workout.exercises = session.exec(
210+
select(Exercise).where(Exercise.workout_id == workout.id)
211+
).all()
212+
213+
return workouts
194214

195215
@router.put("/{workout_id}", response_model=WorkoutPublic)
196216
def update_workout(

backend/app/models/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
PersonalBest,
4343
PersonalBestCreate,
4444
PersonalBestPublic,
45+
WorkoutWithExercisesPublic
4546
)
4647
# not needed - from app.models.personal_best import PersonalBest, PersonalBestCreate
4748

@@ -93,5 +94,6 @@
9394
"ExercisePublic",
9495
"PersonalBest",
9596
"PersonalBestCreate",
96-
"PersonalBestPublic"
97+
"PersonalBestPublic",
98+
"WorkoutWithExercisesPublic"
9799
]

backend/app/models/workout.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ class ExercisePublic(SQLModel):
132132
created_at: datetime
133133
updated_at: Optional[datetime] = None
134134

135+
#Workout with Exercise Public Schema
136+
class WorkoutWithExercisesPublic(SQLModel):
137+
id: uuid.UUID
138+
user_id: uuid.UUID
139+
name: str
140+
description: Optional[str] = None
141+
scheduled_date: Optional[datetime] = None
142+
completed_date: Optional[datetime] = None
143+
duration_minutes: Optional[int] = None
144+
is_completed: bool
145+
created_at: datetime
146+
updated_at: Optional[datetime] = None
147+
exercises: List[ExercisePublic] = []
135148

136149
#Personal Bests Model - Related to Workouts
137150
class PersonalBest(SQLModel, table=True):

0 commit comments

Comments
 (0)