| | document.addEventListener('DOMContentLoaded', function () { |
| | |
| | const exerciseOptions = document.querySelectorAll('.exercise-option'); |
| | const startBtn = document.getElementById('start-btn'); |
| | const stopBtn = document.getElementById('stop-btn'); |
| | const setsInput = document.getElementById('sets'); |
| | const repsInput = document.getElementById('reps'); |
| | const currentExercise = document.getElementById('current-exercise'); |
| | const currentSet = document.getElementById('current-set'); |
| | const currentReps = document.getElementById('current-reps'); |
| |
|
| | |
| | let selectedExercise = null; |
| | let workoutRunning = false; |
| | let statusCheckInterval = null; |
| |
|
| | |
| | exerciseOptions.forEach((option) => { |
| | option.addEventListener('click', function () { |
| | |
| | exerciseOptions.forEach((opt) => opt.classList.remove('selected')); |
| |
|
| | |
| | this.classList.add('selected'); |
| | selectedExercise = this.getAttribute('data-exercise'); |
| | }); |
| | }); |
| |
|
| | |
| | startBtn.addEventListener('click', function () { |
| | if (!selectedExercise) { |
| | alert('Please select an exercise first!'); |
| | return; |
| | } |
| |
|
| | const sets = parseInt(setsInput.value); |
| | const reps = parseInt(repsInput.value); |
| |
|
| | if (isNaN(sets) || sets < 1 || isNaN(reps) || reps < 1) { |
| | alert('Please enter valid numbers for sets and repetitions.'); |
| | return; |
| | } |
| |
|
| | |
| | fetch('/start_exercise', { |
| | method: 'POST', |
| | headers: { |
| | 'Content-Type': 'application/json', |
| | }, |
| | body: JSON.stringify({ |
| | exercise_type: selectedExercise, |
| | sets: sets, |
| | reps: reps, |
| | }), |
| | }) |
| | .then((response) => response.json()) |
| | .then((data) => { |
| | if (data.success) { |
| | workoutRunning = true; |
| | startBtn.disabled = true; |
| | stopBtn.disabled = false; |
| |
|
| | |
| | currentExercise.textContent = selectedExercise.replace('_', ' ').toUpperCase(); |
| | currentSet.textContent = `1 / ${sets}`; |
| | currentReps.textContent = `0 / ${reps}`; |
| |
|
| | |
| | statusCheckInterval = setInterval(checkStatus, 1000); |
| | } else { |
| | alert('Failed to start exercise: ' + (data.error || 'Unknown error')); |
| | } |
| | }) |
| | .catch((error) => { |
| | console.error('Error:', error); |
| | alert('An error occurred while starting the exercise.'); |
| | }); |
| | }); |
| |
|
| | |
| | stopBtn.addEventListener('click', function () { |
| | fetch('/stop_exercise', { |
| | method: 'POST', |
| | headers: { |
| | 'Content-Type': 'application/json', |
| | }, |
| | }) |
| | .then((response) => response.json()) |
| | .then((data) => { |
| | if (data.success) { |
| | resetWorkoutUI(); |
| | } |
| | }) |
| | .catch((error) => { |
| | console.error('Error:', error); |
| | }); |
| | }); |
| |
|
| | |
| | function checkStatus() { |
| | fetch('/get_status') |
| | .then((response) => response.json()) |
| | .then((data) => { |
| | if (!data.exercise_running && workoutRunning) { |
| | |
| | resetWorkoutUI(); |
| | return; |
| | } |
| |
|
| | |
| | currentSet.textContent = `${data.current_set} / ${data.total_sets}`; |
| | currentReps.textContent = `${data.current_reps} / ${data.rep_goal}`; |
| | }) |
| | .catch((error) => { |
| | console.error('Error checking status:', error); |
| | }); |
| | } |
| |
|
| | |
| | function resetWorkoutUI() { |
| | workoutRunning = false; |
| | startBtn.disabled = false; |
| | stopBtn.disabled = true; |
| |
|
| | if (statusCheckInterval) { |
| | clearInterval(statusCheckInterval); |
| | statusCheckInterval = null; |
| | } |
| |
|
| | currentExercise.textContent = 'None'; |
| | currentSet.textContent = '0 / 0'; |
| | currentReps.textContent = '0 / 0'; |
| | } |
| | }); |
| |
|