session.js 8.6 KB

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