session.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. document.getElementById("sessionAddSet").addEventListener("click", ()=>{this.addSet()});
  34. this.rendered = true;
  35. }
  36. },
  37. addSet: function(){
  38. const exercise = this.currentSession.exercises[this.exerciseIndex];
  39. const template = document.getElementById("weightSet").content.children[0];
  40. const container = document.getElementById("sessionSets");
  41. switch(exercise.type){
  42. case "weights":
  43. const newSet = {weight: 0, reps: 0};
  44. const setNumber = container.children.length + 1;
  45. exercise.sets.push(newSet);
  46. container.appendChild(this.createWeightSetElement(template, newSet, setNumber));
  47. break;
  48. }
  49. },
  50. createNewSession: function(workout){
  51. session = {
  52. workout: workout.id,
  53. start: new Date(),
  54. notes: "",
  55. exercises: []
  56. };
  57. localStorage.setItem(workout.id, JSON.stringify(session));
  58. return session;
  59. },
  60. changeExercise: function(num){
  61. if(num !== 0) localStorage.setItem(this.workout.id, JSON.stringify(this.currentSession));
  62. this.exerciseIndex += num;
  63. if(this.exerciseIndex === this.workout.exercises.length-1){
  64. document.getElementById("nextSessionBtn").style.display = "none";
  65. }
  66. let exercise = null;
  67. if(this.currentSession.exercises[this.exerciseIndex]){
  68. exercise = this.currentSession.exercises[this.exerciseIndex];
  69. }else{
  70. const workoutExercise = this.workout.exercises[this.exerciseIndex];
  71. exercise = {
  72. exerciseId: workoutExercise._id,
  73. name: workoutExercise.name,
  74. type: workoutExercise.type,
  75. notes: "",
  76. sets: this.getPastSets(workoutExercise._id)
  77. }
  78. this.currentSession.exercises.push(exercise);
  79. }
  80. document.getElementById("sessionExerciseName").textContent = exercise.name;
  81. const setsContainer = document.getElementById("sessionSets");
  82. while(setsContainer.children.length > 0){
  83. setsContainer.removeChild(setsContainer.firstChild);
  84. }
  85. switch(exercise.type){
  86. case "weights": this.displayWeightSets(exercise.sets, setsContainer);
  87. }
  88. },
  89. displayWeightSets: function(sets, container){
  90. const template = document.getElementById("weightSet").content.children[0];
  91. for(let i = 0; i < sets.length; i++){
  92. container.appendChild(this.createWeightSetElement(template, sets[i], i+1));
  93. }
  94. },
  95. createWeightSetElement: function(template, set, num){
  96. const setElem = template.cloneNode(true);
  97. setElem.querySelector("h3").textContent = `Set #${num}`;
  98. const deleteBtn = setElem.querySelector(".weightSetDelete");
  99. deleteBtn.addEventListener("click", (event)=>{
  100. this.deleteSet(num, event.target.parentElement);
  101. });
  102. const weightInput = setElem.querySelector(".weightSetWeight");
  103. if(set.weight > 0) weightInput.value = set.weight;
  104. weightInput.addEventListener("input", ()=>{set.weight = weightInput.value});
  105. const repInput = setElem.querySelector(".weightSetReps");
  106. if(set.reps > 0) repInput.value = set.reps;
  107. repInput.addEventListener("input", ()=>{set.reps = repInput.value});
  108. return setElem;
  109. },
  110. deleteSet: function(num, setElem){
  111. this.currentSession.exercises[this.exerciseIndex].sets.splice(num-1, 1);
  112. const container = setElem.parentElement;
  113. container.removeChild(setElem);
  114. for(let i = 0; i < container.children.length; i++){
  115. container.children[i].querySelector("h3").textContent = `Set #${i+1}`;
  116. }
  117. },
  118. getPastSets: function(id){
  119. const note = document.getElementById("previousSession");
  120. for(let i = 0; i < this.pastSessions.length; i++){
  121. for(let j = 0; j < this.pastSessions[i].exercises.length; j++){
  122. if(this.pastSessions[i].exercises[j].exerciseId === id){
  123. note.textContent = "*Data autofilled from previous workout";
  124. return this.pastSessions[i].exercises[j].sets;
  125. }
  126. }
  127. }
  128. note.textContent = "*No previous workout data";
  129. return [{weight: 0, reps: 0}, {weight: 0, reps: 0}, {weight: 0, reps: 0}];
  130. },
  131. finish: function(){
  132. this.currentSession.end = new Date();
  133. fetch(`/session`, {
  134. method: "POST",
  135. headers: {
  136. "Content-Type": "application/json"
  137. },
  138. body: JSON.stringify(this.currentSession)
  139. })
  140. .then(r=>r.json())
  141. .then((response)=>{
  142. if(response.error){
  143. notify("error", response.error.message);
  144. }else{
  145. notify("success", "Workout completed and saved");
  146. localStorage.removeItem(this.currentSession.workout);
  147. this.workout = null;
  148. this.exerciseIndex = 0;
  149. this.pastSessions = null;
  150. this.currentSession = null;
  151. changePage("home");
  152. }
  153. })
  154. .catch((err)=>{
  155. notify("error", "ERROR: unable to save workout to database");
  156. });
  157. }
  158. }