File size: 3,941 Bytes
40518b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
document.addEventListener('DOMContentLoaded', function () {
  // Elements
  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');

  // Variables
  let selectedExercise = null;
  let workoutRunning = false;
  let statusCheckInterval = null;

  // Select exercise
  exerciseOptions.forEach((option) => {
    option.addEventListener('click', function () {
      // Remove selected class from all options
      exerciseOptions.forEach((opt) => opt.classList.remove('selected'));

      // Add selected class to clicked option
      this.classList.add('selected');
      selectedExercise = this.getAttribute('data-exercise');
    });
  });

  // Start workout
  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;
    }

    // Start the exercise via API
    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;

          // Update UI
          currentExercise.textContent = selectedExercise.replace('_', ' ').toUpperCase();
          currentSet.textContent = `1 / ${sets}`;
          currentReps.textContent = `0 / ${reps}`;

          // Start status polling
          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.');
      });
  });

  // Stop workout
  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 to check status
  function checkStatus() {
    fetch('/get_status')
      .then((response) => response.json())
      .then((data) => {
        if (!data.exercise_running && workoutRunning) {
          // Workout has ended
          resetWorkoutUI();
          return;
        }

        // Update status display
        currentSet.textContent = `${data.current_set} / ${data.total_sets}`;
        currentReps.textContent = `${data.current_reps} / ${data.rep_goal}`;
      })
      .catch((error) => {
        console.error('Error checking status:', error);
      });
  }

  // Reset UI after workout ends
  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';
  }
});