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 ;
@@ -34,11 +36,12 @@ export interface Workout {
3436interface WorkoutContextType {
3537 workouts : Workout [ ] ;
3638 currentWorkout : Workout | null ;
37- addWorkout : ( workout : Workout ) => void ;
39+ addWorkout : ( workout : Workout ) => Promise < void > ; // <-- async now
3840 updateWorkout : ( workoutId : string , workout : Workout ) => void ;
41+ getWorkouts : ( ) => void ;
3942 deleteWorkout : ( workoutId : string ) => void ;
4043 startWorkout : ( name : string ) => void ;
41- endWorkout : ( ) => void ;
44+ endWorkout : ( updatedWorkout : Workout ) => Promise < void > ; // <-- async now
4245 addExerciseToCurrentWorkout : ( exercise : Exercise ) => void ;
4346 addSetToExercise : ( exerciseId : string , set : Omit < WorkoutSet , 'id' > ) => void ;
4447 removeSetFromExercise : ( exerciseId : string , setId : string ) => void ;
@@ -50,18 +53,72 @@ const WorkoutContext = createContext<WorkoutContextType | undefined>(undefined);
5053export const WorkoutProvider = ( { children } : { children : ReactNode } ) => {
5154 const [ workouts , setWorkouts ] = useState < Workout [ ] > ( [ ] ) ;
5255 const [ currentWorkout , setCurrentWorkout ] = useState < Workout | null > ( null ) ;
56+ const { token, isAuthenticated, isLoading } = useAuth ( ) ; //token of user
57+
58+
59+ // Early return while loading or not authenticated
60+ if ( isLoading || ! isAuthenticated || ! token ) {
61+ return null ; // or <LoadingIndicator />
62+ }
63+ console . log ( "In WorkoutContext - Current User token is: " , token ) ;
5364
5465 const generateId = ( ) => Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) ;
5566
56- const addWorkout = ( workout : Workout ) => {
57- setWorkouts ( prev => [ ...prev , workout ] ) ;
67+ const addWorkout = async ( workout : Workout ) => {
68+ try {
69+ const response = await axios . post (
70+ 'http://localhost:8000/api/v1/workouts/' , // Replace with deployment endpoint
71+ workout ,
72+ {
73+ headers : {
74+ Authorization : `Bearer ${ token } ` ,
75+ } ,
76+ }
77+ ) ;
78+ console . log ( "Response from POST /workouts: " , response ) ;
79+ const savedWorkout = response . data ;
80+ setWorkouts ( prev => [ ...prev , savedWorkout ] ) ;
81+ } catch ( error ) {
82+ console . error ( 'Failed to save workout to backend:' , error . response ?. data || error . message ) ;
83+ }
5884 } ;
5985
60- const updateWorkout = ( workoutId : string , updatedWorkout : Workout ) => {
86+ const getWorkouts = async ( ) => {
87+ try {
88+ const response = await axios . get ( 'http://localhost:8000/api/v1/workouts/' , {
89+ headers : {
90+ Authorization : `Bearer ${ token } ` ,
91+ } ,
92+ } ) ;
93+
94+ // Extract array properly
95+ const workoutArray = response . data . data ;
96+
97+ // Convert string dates to Date objects
98+ const parsed = workoutArray . map ( ( w : any ) => ( {
99+ ...w ,
100+ date : new Date ( w . created_at ) ,
101+ exercises : w . exercises || [ ] , // fallback to empty array
102+ duration : w . duration_minutes || 0 ,
103+ } ) ) ;
104+
105+ setWorkouts ( parsed ) ;
106+ } catch ( error ) {
107+ console . error ( 'Failed to load workouts:' , error . response ?. data || error . message ) ;
108+ }
109+ } ;
110+ const endWorkout = async ( updatedWorkout : Workout ) => {
111+ await addWorkout ( updatedWorkout ) ;
112+ setCurrentWorkout ( null ) ;
113+ } ;
114+
115+ //Everything below this is old - only updates local state rather than send backend requests
116+
117+ const updateWorkout = ( workoutId : string , updatedWorkout : Workout ) => { //Old
61118 setWorkouts ( prev => prev . map ( w => w . id === workoutId ? updatedWorkout : w ) ) ;
62119 } ;
63120
64- const deleteWorkout = ( workoutId : string ) => {
121+ const deleteWorkout = ( workoutId : string ) => { // Old
65122 setWorkouts ( prev => prev . filter ( w => w . id !== workoutId ) ) ;
66123 } ;
67124
@@ -76,13 +133,6 @@ export const WorkoutProvider = ({ children }: { children: ReactNode }) => {
76133 setCurrentWorkout ( newWorkout ) ;
77134 } ;
78135
79- const endWorkout = ( ) => {
80- if ( currentWorkout ) {
81- addWorkout ( currentWorkout ) ;
82- setCurrentWorkout ( null ) ;
83- }
84- } ;
85-
86136 const addExerciseToCurrentWorkout = ( exercise : Exercise ) => {
87137 if ( ! currentWorkout ) return ;
88138
@@ -155,11 +205,12 @@ export const WorkoutProvider = ({ children }: { children: ReactNode }) => {
155205 updateWorkout,
156206 deleteWorkout,
157207 startWorkout,
158- endWorkout,
208+ endWorkout,
159209 addExerciseToCurrentWorkout,
160210 addSetToExercise,
161211 removeSetFromExercise,
162212 updateSet,
213+ getWorkouts,
163214 } } >
164215 { children }
165216 </ WorkoutContext . Provider >
0 commit comments