11import React , { createContext , useContext , useState , ReactNode } from 'react' ;
2+ import axios from 'axios' ;
3+ import { useAuth } from '@/contexts/AuthContext' ; // adjust if needed
24
35export interface Exercise {
46 id : string ;
57 name : string ;
8+ description ?: string ;
69 muscle_group : string ;
710 type : 'strength' | 'cardio' | 'flexibility' ;
811}
@@ -34,11 +37,12 @@ export interface Workout {
3437interface WorkoutContextType {
3538 workouts : Workout [ ] ;
3639 currentWorkout : Workout | null ;
37- addWorkout : ( workout : Workout ) => void ;
40+ addWorkout : ( workout : Workout ) => Promise < void > ; // <-- async now
3841 updateWorkout : ( workoutId : string , workout : Workout ) => void ;
42+ getWorkouts : ( ) => void ;
3943 deleteWorkout : ( workoutId : string ) => void ;
4044 startWorkout : ( name : string ) => void ;
41- endWorkout : ( ) => void ;
45+ endWorkout : ( updatedWorkout : Workout ) => Promise < void > ; // <-- async now
4246 addExerciseToCurrentWorkout : ( exercise : Exercise ) => void ;
4347 addSetToExercise : ( exerciseId : string , set : Omit < WorkoutSet , 'id' > ) => void ;
4448 removeSetFromExercise : ( exerciseId : string , setId : string ) => void ;
@@ -50,18 +54,96 @@ const WorkoutContext = createContext<WorkoutContextType | undefined>(undefined);
5054export const WorkoutProvider = ( { children } : { children : ReactNode } ) => {
5155 const [ workouts , setWorkouts ] = useState < Workout [ ] > ( [ ] ) ;
5256 const [ currentWorkout , setCurrentWorkout ] = useState < Workout | null > ( null ) ;
57+ const { token, isAuthenticated, isLoading } = useAuth ( ) ; //token of user
58+
59+ //NO WAIT FOR TOKEN, IS GIVING NULL IN WORKOUT CONTEXT
60+
61+ //console.log("In WorkoutContext - Current User token is: ", token);
5362
5463 const generateId = ( ) => Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) ;
5564
56- const addWorkout = ( workout : Workout ) => {
57- setWorkouts ( prev => [ ...prev , workout ] ) ;
65+ const transformWorkoutForBackend = ( workout : Workout ) => ( {
66+ name : workout . name ,
67+ description : workout . notes || null ,
68+ scheduled_date : workout . date . toISOString ( ) ,
69+ duration_minutes : workout . duration ,
70+ exercises : workout . exercises . map ( ex => ( {
71+ name : ex . exercise . name ,
72+ description : ex . exercise . description || null ,
73+ category : ex . exercise . muscle_group , // or ex.exercise.type if that's better
74+ sets : ex . sets . length ,
75+ reps : ex . sets . reduce ( ( sum , s ) => sum + ( s . reps || 0 ) , 0 ) ,
76+ weight : ex . sets . reduce ( ( max , s ) => Math . max ( max , s . weight || 0 ) , 0 ) ,
77+ } ) ) ,
78+ } ) ;
79+
80+ const addWorkout = async ( workout : Workout ) => {
81+ try {
82+ const payload = transformWorkoutForBackend ( workout ) ;
83+ const response = await axios . post (
84+ 'http://localhost:8000/api/v1/workouts/' , // Replace with deployment endpoint
85+ payload ,
86+ {
87+ headers : {
88+ Authorization : `Bearer ${ token } ` ,
89+ } ,
90+ }
91+ ) ;
92+ //console.log("Response from POST /workouts: ", response);
93+ const savedWorkout = response . data ;
94+
95+ const saved = {
96+ ...response . data ,
97+ date : new Date ( response . data . created_at ) ,
98+ exercises : [ ] , // if backend doesn't return them
99+ duration : response . data . duration_minutes || 0 ,
100+ } ;
101+
102+ setWorkouts ( prev => [ ...prev , saved ] ) ;
103+
104+ } catch ( error ) {
105+ console . error ( 'Failed to save workout to backend:' , error . response ?. data || error . message ) ;
106+ }
58107 } ;
59108
60- const updateWorkout = ( workoutId : string , updatedWorkout : Workout ) => {
109+ const getWorkouts = async ( ) => {
110+ try {
111+ console . log ( "About to get workouts with token: " , token ) ;
112+ const response = await axios . get ( 'http://localhost:8000/api/v1/workouts/' , {
113+ headers : {
114+ Authorization : `Bearer ${ token } ` ,
115+ } ,
116+ } ) ;
117+
118+ //console.log("Get workouts response: ", response);
119+ // Extract array properly
120+ const workoutArray = response . data ;
121+
122+ // Convert string dates to Date objects
123+ const parsed = workoutArray . map ( ( w : any ) => ( {
124+ ...w ,
125+ date : new Date ( w . created_at ) ,
126+ exercises : w . exercises || [ ] , // fallback to empty array
127+ duration : w . duration_minutes || 0 ,
128+ } ) ) ;
129+
130+ setWorkouts ( parsed ) ;
131+ } catch ( error ) {
132+ console . error ( 'Failed to load workouts:' , error . response ?. data || error . message ) ;
133+ }
134+ } ;
135+ const endWorkout = async ( updatedWorkout : Workout ) => {
136+ await addWorkout ( updatedWorkout ) ;
137+ setCurrentWorkout ( null ) ;
138+ } ;
139+
140+ //Everything below this is old - only updates local state rather than send backend requests
141+
142+ const updateWorkout = ( workoutId : string , updatedWorkout : Workout ) => { //Old
61143 setWorkouts ( prev => prev . map ( w => w . id === workoutId ? updatedWorkout : w ) ) ;
62144 } ;
63145
64- const deleteWorkout = ( workoutId : string ) => {
146+ const deleteWorkout = ( workoutId : string ) => { // Old
65147 setWorkouts ( prev => prev . filter ( w => w . id !== workoutId ) ) ;
66148 } ;
67149
@@ -76,13 +158,6 @@ export const WorkoutProvider = ({ children }: { children: ReactNode }) => {
76158 setCurrentWorkout ( newWorkout ) ;
77159 } ;
78160
79- const endWorkout = ( ) => {
80- if ( currentWorkout ) {
81- addWorkout ( currentWorkout ) ;
82- setCurrentWorkout ( null ) ;
83- }
84- } ;
85-
86161 const addExerciseToCurrentWorkout = ( exercise : Exercise ) => {
87162 if ( ! currentWorkout ) return ;
88163
@@ -155,11 +230,12 @@ export const WorkoutProvider = ({ children }: { children: ReactNode }) => {
155230 updateWorkout,
156231 deleteWorkout,
157232 startWorkout,
158- endWorkout,
233+ endWorkout,
159234 addExerciseToCurrentWorkout,
160235 addSetToExercise,
161236 removeSetFromExercise,
162237 updateSet,
238+ getWorkouts,
163239 } } >
164240 { children }
165241 </ WorkoutContext . Provider >
0 commit comments