session.js 9.7 KB

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