91 lines
3.3 KiB
TypeScript
91 lines
3.3 KiB
TypeScript
import { WorkoutSession, WorkoutSet, ExerciseType } from '../types';
|
|
import { api } from './api';
|
|
|
|
// Define the shape of session coming from API (Prisma include)
|
|
interface ApiSession extends Omit<WorkoutSession, 'startTime' | 'endTime' | 'sets'> {
|
|
startTime: string | number; // JSON dates are strings
|
|
endTime?: string | number;
|
|
sets: (Omit<WorkoutSet, 'exerciseName' | 'type'> & {
|
|
exercise?: {
|
|
name: string;
|
|
type: ExerciseType;
|
|
}
|
|
})[];
|
|
}
|
|
|
|
export const getSessions = async (userId: string): Promise<WorkoutSession[]> => {
|
|
try {
|
|
const sessions = await api.get<ApiSession[]>('/sessions');
|
|
// Convert ISO date strings to timestamps
|
|
return sessions.map((session) => ({
|
|
...session,
|
|
startTime: new Date(session.startTime).getTime(),
|
|
endTime: session.endTime ? new Date(session.endTime).getTime() : undefined,
|
|
sets: session.sets.map((set) => ({
|
|
...set,
|
|
exerciseName: set.exercise?.name || 'Unknown',
|
|
type: set.exercise?.type || ExerciseType.STRENGTH
|
|
})) as WorkoutSet[]
|
|
}));
|
|
} 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<{ success: boolean; session?: ApiSession }>('/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) => ({
|
|
...set,
|
|
exerciseName: set.exercise?.name || 'Unknown',
|
|
type: set.exercise?.type || ExerciseType.STRENGTH
|
|
})) as WorkoutSet[]
|
|
};
|
|
} 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<{ success: boolean; updatedSet: WorkoutSet }>(`/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 addSetToActiveSession = async (userId: string, setData: any): Promise<any> => {
|
|
return await api.post('/sessions/active/log-set', setData);
|
|
};
|
|
|
|
export const deleteAllUserData = (userId: string) => {
|
|
// Not implemented in frontend
|
|
};
|