Damijuda/fitstart
0
1// Shared JavaScript across all pages2document.addEventListener('DOMContentLoaded', () => {3 // Initialize dark mode from localStorage or system preference4 const storedTheme = localStorage.getItem('theme') || 5 (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');6 document.documentElement.classList.add(storedTheme);7 8 // Toggle dark/light mode9 const themeToggle = document.getElementById('theme-toggle');10 if (themeToggle) {11 themeToggle.addEventListener('click', () => {12 const currentTheme = document.documentElement.classList.contains('dark') ? 'light' : 'dark';13 document.documentElement.classList.remove('light', 'dark');14 document.documentElement.classList.add(currentTheme);15 localStorage.setItem('theme', currentTheme);16 17 // Update icon18 const icon = themeToggle.querySelector('i');19 if (currentTheme === 'dark') {20 icon.setAttribute('data-feather', 'sun');21 } else {22 icon.setAttribute('data-feather', 'moon');23 }24 feather.replace();25 });26 }27});28// Adaptive workout generator based on user progress29async function generateAdaptiveWorkout(userData) {30 try {31 // Get base workout plan from API32 const response = await fetch('/api/workouts/adaptive', {33 method: 'POST',34 headers: {35 'Content-Type': 'application/json',36 },37 body: JSON.stringify({38 userId: userData.id,39 currentLevel: userData.fitnessLevel,40 recentProgress: userData.progress,41 preferences: userData.preferences42 })43 });44 45 const workoutPlan = await response.json();46 47 // Adjust based on local progress data48 if (userData.recentWorkouts) {49 const lastWorkout = userData.recentWorkouts[0];50 if (lastWorkout.difficultyRating > 7) {51 workoutPlan.intensity += 0.1;52 } else if (lastWorkout.difficultyRating < 4) {53 workoutPlan.intensity -= 0.1;54 }55 }56 57 return workoutPlan;58 } catch (error) {59 console.error('Error generating adaptive workout:', error);60 return null;61 }62}63// Trainer access functionality64function setupTrainerAccess() {65 const trainerAccessSection = document.getElementById('trainer-access');66 if (trainerAccessSection) {67 const shareButton = trainerAccessSection.querySelector('button');68 const emailInput = trainerAccessSection.querySelector('input[type="email"]');69 const inviteButton = trainerAccessSection.querySelector('button.bg-green-500');70 71 shareButton.addEventListener('click', () => {72 trainerAccessSection.querySelector('.bg-gray-50').classList.toggle('hidden');73 });74 75 inviteButton.addEventListener('click', async () => {76 if (emailInput.value) {77 try {78 const response = await fetch('/api/trainers/invite', {79 method: 'POST',80 headers: {81 'Content-Type': 'application/json',82 },83 body: JSON.stringify({84 email: emailInput.value85 })86 });87 88 if (response.ok) {89 alert('Invitation sent successfully!');90 emailInput.value = '';91 } else {92 alert('Error sending invitation');93 }94 } catch (error) {95 alert('Error sending invitation');96 }97 }98 });99 }100}101 102// Track workout performance and adjust future plans103function trackWorkoutPerformance(workoutData) {104 const performanceData = {105 workoutId: workoutData.id,106 completionRate: workoutData.completedExercises / workoutData.totalExercises,107 difficultyRating: workoutData.userRating,108 prCount: workoutData.prsAchieved109 };110 111 // Store in local storage for adaptive algorithm112 let userData = JSON.parse(localStorage.getItem('userData')) || {};113 userData.recentWorkouts = userData.recentWorkouts || [];114 userData.recentWorkouts.unshift(performanceData);115 localStorage.setItem('userData', JSON.stringify(userData));116 117 return performanceData;118}119// Initialize on dashboard page120if (window.location.pathname.includes('dashboard.html')) {121 document.addEventListener('DOMContentLoaded', () => {122 fetchWorkoutData();123 setupTrainerAccess();124 });125}126 