session.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. export default {
  2. rendered: false,
  3. workout: null,
  4. pastSessions: null,
  5. exerciseIndex: 0,
  6. currentSession: null,
  7. render: async function(workout){
  8. this.workout = workout;
  9. this.pastSessions = await this.getPastSessions(workout.id);
  10. this.currentSession = this.createNewSession(workout);
  11. this.buttons();
  12. this.changeExercise(0);
  13. },
  14. getPastSessions: async function(id){
  15. const sessions = await fetch(`/session/${id}`, {
  16. method: "GET",
  17. headers: {
  18. "Content-Type": "application/json"
  19. }
  20. });
  21. if(sessions.error){
  22. notify("error", "ERROR: Unable to retrieve past workouts");
  23. return [];
  24. }
  25. return await sessions.json();
  26. },
  27. buttons: function(){
  28. const nextSessionBtn = document.getElementById("nextSessionBtn");
  29. nextSessionBtn.style.display = "block";
  30. if(!this.rendered){
  31. nextSessionBtn.addEventListener("click", ()=>{this.changeExercise(1)});
  32. document.getElementById("finishSessionBtn").addEventListener("click", ()=>{this.finish()});
  33. this.rendered = true;
  34. }
  35. },
  36. createNewSession: function(workout){
  37. session = {
  38. workout: workout.id,
  39. start: new Date(),
  40. notes: "",
  41. exercises: []
  42. };
  43. localStorage.setItem(workout.id, JSON.stringify(session));
  44. return session;
  45. },
  46. changeExercise: function(num){
  47. if(num !== 0) localStorage.setItem(this.workout.id, JSON.stringify(this.currentSession));
  48. this.exerciseIndex += num;
  49. if(this.exerciseIndex === this.workout.exercises.length-1){
  50. document.getElementById("nextSessionBtn").style.display = "none";
  51. }
  52. let exercise = null;
  53. if(this.currentSession.exercises[this.exerciseIndex]){
  54. exercise = this.currentSession.exercises[this.exerciseIndex];
  55. }else{
  56. const workoutExercise = this.workout.exercises[this.exerciseIndex];
  57. exercise = {
  58. exerciseId: workoutExercise._id,
  59. name: workoutExercise.name,
  60. type: workoutExercise.type,
  61. notes: "",
  62. sets: this.getPastSets(workoutExercise._id)
  63. }
  64. this.currentSession.exercises.push(exercise);
  65. }
  66. document.getElementById("sessionExerciseName").textContent = exercise.name;
  67. const setsContainer = document.getElementById("sessionSets");
  68. while(setsContainer.children.length > 0){
  69. setsContainer.removeChild(setsContainer.firstChild);
  70. }
  71. switch(exercise.type){
  72. case "weights": this.displayWeightSets(exercise, setsContainer);
  73. }
  74. },
  75. displayWeightSets: function(exercise, container){
  76. const weightTemplate = document.getElementById("weightSet").content.children[0];
  77. for(let i = 0; i < exercise.sets.length; i++){
  78. const set = weightTemplate.cloneNode(true);
  79. set.querySelector("h3").textContent = `Set #${i+1}`;
  80. const weightInput = set.querySelector(".weightSetWeight");
  81. weightInput.value = exercise.sets[i].weight
  82. weightInput.addEventListener("input", ()=>{exercise.sets[i].weight = weightInput.value});
  83. const repInput = set.querySelector(".weightSetReps");
  84. repInput.value = exercise.sets[i].reps;
  85. repInput.addEventListener("input", ()=>{exercise.sets[i].reps = repInput.value});
  86. container.appendChild(set);
  87. }
  88. },
  89. getPastSets: function(id){
  90. const note = document.getElementById("previousSession");
  91. for(let i = 0; i < this.pastSessions.length; i++){
  92. for(let j = 0; j < this.pastSessions[i].exercises.length; j++){
  93. if(this.pastSessions[i].exercises[j].exerciseId === id){
  94. note.textContent = "*Data autofilled from previous workout";
  95. return this.pastSessions[i].exercises[j].sets;
  96. }
  97. }
  98. }
  99. note.textContent = "*No previous workout data";
  100. return [{weight: 0, reps: 0}, {weight: 0, reps: 0}, {weight: 0, reps: 0}];
  101. },
  102. finish: function(){
  103. this.currentSession.end = new Date();
  104. fetch(`/session`, {
  105. method: "POST",
  106. headers: {
  107. "Content-Type": "application/json"
  108. },
  109. body: JSON.stringify(this.currentSession)
  110. })
  111. .then(r=>r.json())
  112. .then((response)=>{
  113. if(response.error){
  114. notify("error", response.error.message);
  115. }else{
  116. notify("success", "Workout completed and saved");
  117. localStorage.removeItem(this.currentSession.workout);
  118. this.workout = null;
  119. this.exerciseIndex = 0;
  120. this.pastSessions = null;
  121. this.currentSession = null;
  122. changePage("home");
  123. }
  124. })
  125. .catch((err)=>{
  126. notify("error", "ERROR: unable to save workout to database");
  127. });
  128. }
  129. }