session.js 10 KB

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