diff --git a/App.tsx b/App.tsx index 04b50ff..3bacdb4 100644 --- a/App.tsx +++ b/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} diff --git a/components/Profile.tsx b/components/Profile.tsx index f2aa8d7..2dbd7c2 100644 --- a/components/Profile.tsx +++ b/components/Profile.tsx @@ -75,8 +75,11 @@ const Profile: React.FC = ({ user, onLogout, lang, onLanguageChang // eslint-disable-next-line react-hooks/exhaustive-deps }, [user.id, user.role, JSON.stringify(user.profile)]); - const refreshUserList = () => { - setAllUsers(getUsers()); + const refreshUserList = async () => { + const res = await getUsers(); + if (res.success && res.users) { + setAllUsers(res.users); + } }; const refreshExercises = async () => { @@ -143,16 +146,16 @@ const Profile: React.FC = ({ user, onLogout, lang, onLanguageChang } }; - const handleAdminDeleteUser = (uid: string) => { + const handleAdminDeleteUser = async (uid: string) => { if (confirm(t('delete_confirm', lang))) { - deleteUser(uid); - refreshUserList(); + await deleteUser(uid); + await refreshUserList(); } }; - const handleAdminBlockUser = (uid: string, isBlocked: boolean) => { - toggleBlockUser(uid, isBlocked); - refreshUserList(); + const handleAdminBlockUser = async (uid: string, isBlocked: boolean) => { + await toggleBlockUser(uid, isBlocked); + await refreshUserList(); }; const handleAdminResetPass = (uid: string) => { diff --git a/components/Tracker.tsx b/components/Tracker.tsx index aca4f5c..371cc95 100644 --- a/components/Tracker.tsx +++ b/components/Tracker.tsx @@ -14,6 +14,7 @@ interface TrackerProps { activePlan: WorkoutPlan | null; onSessionStart: (plan?: WorkoutPlan, startWeight?: number) => void; onSessionEnd: () => void; + onSessionQuit: () => void; onSetAdded: (set: WorkoutSet) => void; onRemoveSet: (setId: string) => void; onUpdateSet: (set: WorkoutSet) => void; @@ -23,7 +24,7 @@ interface TrackerProps { import FilledInput from './FilledInput'; import ExerciseModal from './ExerciseModal'; -const Tracker: React.FC = ({ userId, userWeight, activeSession, activePlan, onSessionStart, onSessionEnd, onSetAdded, onRemoveSet, onUpdateSet, lang }) => { +const Tracker: React.FC = ({ userId, userWeight, activeSession, activePlan, onSessionStart, onSessionEnd, onSessionQuit, onSetAdded, onRemoveSet, onUpdateSet, lang }) => { const [exercises, setExercises] = useState([]); const [plans, setPlans] = useState([]); const [selectedExercise, setSelectedExercise] = useState(null); @@ -665,8 +666,7 @@ const Tracker: React.FC = ({ userId, userWeight, activeSession, ac