session.js 8.5 KB

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