Add src/components/workout/WorkoutSaver.tsx
This commit is contained in:
129
src/components/workout/WorkoutSaver.tsx
Normal file
129
src/components/workout/WorkoutSaver.tsx
Normal file
@@ -0,0 +1,129 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { useWorkoutData } from '@/hooks/useWorkoutData';
|
||||
import { WorkoutSession, ExerciseLog } from '@/lib/storage/workoutStorage';
|
||||
|
||||
interface WorkoutSaverProps {
|
||||
onSave?: (session: WorkoutSession) => void;
|
||||
onError?: (error: string) => void;
|
||||
}
|
||||
|
||||
export const WorkoutSaver: React.FC<WorkoutSaverProps> = ({ onSave, onError }) => {
|
||||
const { addSession } = useWorkoutData();
|
||||
const [isSaving, setIsSaving] = useState(false);
|
||||
|
||||
const saveCardioWorkout = async (data: {
|
||||
distance: number;
|
||||
duration: number;
|
||||
pace: string;
|
||||
calories: number;
|
||||
steps?: number;
|
||||
}) => {
|
||||
setIsSaving(true);
|
||||
try {
|
||||
const session: Omit<WorkoutSession, 'id'> = {
|
||||
date: new Date().toISOString(),
|
||||
type: 'cardio',
|
||||
distance: data.distance,
|
||||
duration: data.duration,
|
||||
pace: data.pace,
|
||||
calories: data.calories,
|
||||
steps: data.steps
|
||||
};
|
||||
const success = addSession(session);
|
||||
if (success && onSave) {
|
||||
onSave(session as WorkoutSession);
|
||||
} else if (!success && onError) {
|
||||
onError('Failed to save cardio workout');
|
||||
}
|
||||
} catch (error) {
|
||||
if (onError) onError('Error saving cardio workout');
|
||||
console.error(error);
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const saveTrainingWorkout = async (data: {
|
||||
exercises: ExerciseLog[];
|
||||
duration: number;
|
||||
calories?: number;
|
||||
}) => {
|
||||
setIsSaving(true);
|
||||
try {
|
||||
const session: Omit<WorkoutSession, 'id'> = {
|
||||
date: new Date().toISOString(),
|
||||
type: 'training',
|
||||
exercises: data.exercises,
|
||||
duration: data.duration,
|
||||
calories: data.calories
|
||||
};
|
||||
const success = addSession(session);
|
||||
if (success && onSave) {
|
||||
onSave(session as WorkoutSession);
|
||||
} else if (!success && onError) {
|
||||
onError('Failed to save training workout');
|
||||
}
|
||||
} catch (error) {
|
||||
if (onError) onError('Error saving training workout');
|
||||
console.error(error);
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const saveNutritionLog = async (data: {
|
||||
meals: Array<{
|
||||
name: string;
|
||||
calories: number;
|
||||
protein: number;
|
||||
carbs: number;
|
||||
fats: number;
|
||||
}>;
|
||||
}) => {
|
||||
setIsSaving(true);
|
||||
try {
|
||||
const meals = data.meals.map((meal, index) => ({
|
||||
id: `meal-${index}-${Date.now()}`,
|
||||
name: meal.name,
|
||||
calories: meal.calories,
|
||||
protein: meal.protein,
|
||||
carbs: meal.carbs,
|
||||
fats: meal.fats,
|
||||
timestamp: new Date().toISOString()
|
||||
}));
|
||||
|
||||
const totalCalories = data.meals.reduce((sum, meal) => sum + meal.calories, 0);
|
||||
const totalProtein = data.meals.reduce((sum, meal) => sum + meal.protein, 0);
|
||||
|
||||
const session: Omit<WorkoutSession, 'id'> = {
|
||||
date: new Date().toISOString(),
|
||||
type: 'nutrition',
|
||||
meals,
|
||||
calories: totalCalories,
|
||||
notes: `Total Protein: ${totalProtein}g`
|
||||
};
|
||||
const success = addSession(session);
|
||||
if (success && onSave) {
|
||||
onSave(session as WorkoutSession);
|
||||
} else if (!success && onError) {
|
||||
onError('Failed to save nutrition log');
|
||||
}
|
||||
} catch (error) {
|
||||
if (onError) onError('Error saving nutrition log');
|
||||
console.error(error);
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
saveCardioWorkout,
|
||||
saveTrainingWorkout,
|
||||
saveNutritionLog,
|
||||
isSaving
|
||||
};
|
||||
};
|
||||
|
||||
export default WorkoutSaver;
|
||||
Reference in New Issue
Block a user