Files
gymflow/services/storage.ts

118 lines
3.8 KiB
TypeScript

import { WorkoutSession, ExerciseDef, ExerciseType, WorkoutSet, WorkoutPlan } from '../types';
import { api } from './api';
export const getSessions = async (userId: string): Promise<WorkoutSession[]> => {
try {
const sessions = await api.get('/sessions');
// Convert ISO date strings to timestamps
return sessions.map((session: any) => ({
...session,
startTime: new Date(session.startTime).getTime(),
endTime: session.endTime ? new Date(session.endTime).getTime() : undefined,
sets: session.sets.map((set: any) => ({
...set,
exerciseName: set.exercise?.name || 'Unknown',
type: set.exercise?.type || 'STRENGTH'
}))
}));
} catch {
return [];
}
};
export const saveSession = async (userId: string, session: WorkoutSession): Promise<void> => {
await api.post('/sessions', session);
};
export const getActiveSession = async (userId: string): Promise<WorkoutSession | null> => {
try {
const response = await api.get('/sessions/active');
if (!response.success || !response.session) {
return null;
}
const session = response.session;
// Convert ISO date strings to timestamps
return {
...session,
startTime: new Date(session.startTime).getTime(),
endTime: session.endTime ? new Date(session.endTime).getTime() : undefined,
sets: session.sets.map((set: any) => ({
...set,
exerciseName: set.exercise?.name || 'Unknown',
type: set.exercise?.type || 'STRENGTH'
}))
};
} catch {
return null;
}
};
export const updateActiveSession = async (userId: string, session: WorkoutSession): Promise<void> => {
await api.put('/sessions/active', session);
};
export const deleteSetFromActiveSession = async (userId: string, setId: string): Promise<void> => {
await api.delete(`/sessions/active/set/${setId}`);
};
export const updateSetInActiveSession = async (userId: string, setId: string, setData: Partial<WorkoutSet>): Promise<WorkoutSet> => {
const response = await api.put(`/sessions/active/set/${setId}`, setData);
return response.updatedSet;
};
export const deleteActiveSession = async (userId: string): Promise<void> => {
await api.delete('/sessions/active');
};
export const deleteSession = async (userId: string, id: string): Promise<void> => {
await api.delete(`/sessions/${id}`);
};
export const deleteAllUserData = (userId: string) => {
// Not implemented in frontend
};
export const getExercises = async (userId: string): Promise<ExerciseDef[]> => {
try {
return await api.get('/exercises');
} catch {
return [];
}
};
export const saveExercise = async (userId: string, exercise: ExerciseDef): Promise<void> => {
await api.post('/exercises', exercise);
};
export const getLastSetForExercise = async (userId: string, exerciseId: string): Promise<WorkoutSet | undefined> => {
// This requires fetching sessions or a specific endpoint.
// For performance, we should probably have an endpoint for this.
// For now, let's fetch sessions and find it client side, or implement endpoint later.
// Given the async nature, we need to change the signature to Promise.
// The caller needs to await this.
const sessions = await getSessions(userId);
for (const session of sessions) {
for (let i = session.sets.length - 1; i >= 0; i--) {
if (session.sets[i].exerciseId === exerciseId) {
return session.sets[i];
}
}
}
return undefined;
}
export const getPlans = async (userId: string): Promise<WorkoutPlan[]> => {
try {
return await api.get('/plans');
} catch {
return [];
}
};
export const savePlan = async (userId: string, plan: WorkoutPlan): Promise<void> => {
await api.post('/plans', plan);
};
export const deletePlan = async (userId: string, id: string): Promise<void> => {
await api.delete(`/plans/${id}`);
};