session.js 10 KB

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