Ongoing workout session data is persistent now
This commit is contained in:
86
App.tsx
86
App.tsx
@@ -9,7 +9,7 @@ import Plans from './components/Plans';
|
||||
import Login from './components/Login';
|
||||
import Profile from './components/Profile';
|
||||
import { TabView, WorkoutSession, WorkoutSet, WorkoutPlan, User, Language } from './types';
|
||||
import { getSessions, saveSession, deleteSession, getPlans } from './services/storage';
|
||||
import { getSessions, saveSession, deleteSession, getPlans, getActiveSession, updateActiveSession, deleteActiveSession } from './services/storage';
|
||||
import { getCurrentUserProfile, getMe } from './services/auth';
|
||||
import { getSystemLanguage } from './services/i18n';
|
||||
import { generateId } from './utils/uuid';
|
||||
@@ -35,6 +35,20 @@ function App() {
|
||||
const res = await getMe();
|
||||
if (res.success && res.user) {
|
||||
setCurrentUser(res.user);
|
||||
|
||||
// Restore active workout session from database
|
||||
const activeSession = await getActiveSession(res.user.id);
|
||||
if (activeSession) {
|
||||
setActiveSession(activeSession);
|
||||
// Restore plan if session has planId
|
||||
if (activeSession.planId) {
|
||||
const plans = await getPlans(res.user.id);
|
||||
const plan = plans.find(p => p.id === activeSession.planId);
|
||||
if (plan) {
|
||||
setActivePlan(plan);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
localStorage.removeItem('token');
|
||||
}
|
||||
@@ -78,7 +92,7 @@ function App() {
|
||||
setCurrentUser(updatedUser);
|
||||
};
|
||||
|
||||
const handleStartSession = (plan?: WorkoutPlan, startWeight?: number) => {
|
||||
const handleStartSession = async (plan?: WorkoutPlan, startWeight?: number) => {
|
||||
if (!currentUser) return;
|
||||
|
||||
// Get latest weight from profile or default
|
||||
@@ -97,12 +111,15 @@ function App() {
|
||||
setActivePlan(plan || null);
|
||||
setActiveSession(newSession);
|
||||
setCurrentTab('TRACK');
|
||||
|
||||
// Save to database immediately
|
||||
await saveSession(currentUser.id, newSession);
|
||||
};
|
||||
|
||||
const handleEndSession = async () => {
|
||||
if (activeSession && currentUser) {
|
||||
const finishedSession = { ...activeSession, endTime: Date.now() };
|
||||
await saveSession(currentUser.id, finishedSession);
|
||||
await updateActiveSession(currentUser.id, finishedSession);
|
||||
setSessions(prev => [finishedSession, ...prev]);
|
||||
setActiveSession(null);
|
||||
setActivePlan(null);
|
||||
@@ -115,39 +132,47 @@ function App() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleAddSet = (set: WorkoutSet) => {
|
||||
if (activeSession) {
|
||||
setActiveSession(prev => {
|
||||
if (!prev) return null;
|
||||
return {
|
||||
...prev,
|
||||
sets: [...prev.sets, set]
|
||||
};
|
||||
});
|
||||
const handleAddSet = async (set: WorkoutSet) => {
|
||||
if (activeSession && currentUser) {
|
||||
const updatedSession = {
|
||||
...activeSession,
|
||||
sets: [...activeSession.sets, set]
|
||||
};
|
||||
setActiveSession(updatedSession);
|
||||
// Save to database
|
||||
await updateActiveSession(currentUser.id, updatedSession);
|
||||
}
|
||||
};
|
||||
|
||||
const handleRemoveSetFromActive = (setId: string) => {
|
||||
if (activeSession) {
|
||||
setActiveSession(prev => {
|
||||
if (!prev) return null;
|
||||
return {
|
||||
...prev,
|
||||
sets: prev.sets.filter(s => s.id !== setId)
|
||||
};
|
||||
});
|
||||
const handleRemoveSetFromActive = async (setId: string) => {
|
||||
if (activeSession && currentUser) {
|
||||
const updatedSession = {
|
||||
...activeSession,
|
||||
sets: activeSession.sets.filter(s => s.id !== setId)
|
||||
};
|
||||
setActiveSession(updatedSession);
|
||||
// Save to database
|
||||
await updateActiveSession(currentUser.id, updatedSession);
|
||||
}
|
||||
};
|
||||
|
||||
const handleUpdateSetInActive = (updatedSet: WorkoutSet) => {
|
||||
if (activeSession) {
|
||||
setActiveSession(prev => {
|
||||
if (!prev) return null;
|
||||
return {
|
||||
...prev,
|
||||
sets: prev.sets.map(s => s.id === updatedSet.id ? updatedSet : s)
|
||||
};
|
||||
});
|
||||
const handleUpdateSetInActive = async (updatedSet: WorkoutSet) => {
|
||||
if (activeSession && currentUser) {
|
||||
const updatedSession = {
|
||||
...activeSession,
|
||||
sets: activeSession.sets.map(s => s.id === updatedSet.id ? updatedSet : s)
|
||||
};
|
||||
setActiveSession(updatedSession);
|
||||
// Save to database
|
||||
await updateActiveSession(currentUser.id, updatedSession);
|
||||
}
|
||||
};
|
||||
|
||||
const handleQuitSession = async () => {
|
||||
if (currentUser) {
|
||||
await deleteActiveSession(currentUser.id);
|
||||
setActiveSession(null);
|
||||
setActivePlan(null);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -184,6 +209,7 @@ function App() {
|
||||
activePlan={activePlan}
|
||||
onSessionStart={handleStartSession}
|
||||
onSessionEnd={handleEndSession}
|
||||
onSessionQuit={handleQuitSession}
|
||||
onSetAdded={handleAddSet}
|
||||
onRemoveSet={handleRemoveSetFromActive}
|
||||
onUpdateSet={handleUpdateSetInActive}
|
||||
|
||||
Reference in New Issue
Block a user