session.js 7.6 KB

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