session.js 6.9 KB

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