session.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. this.currentSession = this.createNewSession(workout);
  11. this.buttons();
  12. this.changeExercise(0);
  13. },
  14. getPastSessions: async function(id){
  15. const sessions = await fetch(`/session/${id}`, {
  16. method: "GET",
  17. headers: {
  18. "Content-Type": "application/json"
  19. }
  20. });
  21. if(sessions.error){
  22. notify("error", "ERROR: Unable to retrieve past workouts");
  23. return [];
  24. }
  25. return await sessions.json();
  26. },
  27. buttons: function(){
  28. const nextSessionBtn = document.getElementById("nextSessionBtn");
  29. nextSessionBtn.style.display = "block";
  30. if(!this.rendered){
  31. nextSessionBtn.addEventListener("click", ()=>{this.changeExercise(1)});
  32. document.getElementById("finishSessionBtn").addEventListener("click", ()=>{this.finish()});
  33. document.getElementById("sessionAddSet").addEventListener("click", ()=>{this.addSet()});
  34. this.rendered = true;
  35. }
  36. },
  37. addSet: function(){
  38. const exercise = this.currentSession.exercises[this.exerciseIndex];
  39. const template = document.getElementById("weightSet").content.children[0];
  40. const container = document.getElementById("sessionSets");
  41. switch(exercise.type){
  42. case "weights":
  43. const newSet = {weight: 0, reps: 0};
  44. const setNumber = container.children.length + 1;
  45. exercise.sets.push(newSet);
  46. container.appendChild(this.createWeightSetElement(template, newSet, setNumber));
  47. break;
  48. }
  49. },
  50. createNewSession: function(workout){
  51. session = {
  52. workout: workout.id,
  53. start: new Date(),
  54. notes: "",
  55. exercises: []
  56. };
  57. localStorage.setItem(workout.id, JSON.stringify(session));
  58. return session;
  59. },
  60. changeExercise: function(num){
  61. if(num !== 0) localStorage.setItem(this.workout.id, JSON.stringify(this.currentSession));
  62. this.exerciseIndex += num;
  63. if(this.exerciseIndex === this.workout.exercises.length-1){
  64. document.getElementById("nextSessionBtn").style.display = "none";
  65. }
  66. let exercise = null;
  67. if(this.currentSession.exercises[this.exerciseIndex]){
  68. exercise = this.currentSession.exercises[this.exerciseIndex];
  69. }else{
  70. const workoutExercise = this.workout.exercises[this.exerciseIndex];
  71. exercise = {
  72. exerciseId: workoutExercise._id,
  73. name: workoutExercise.name,
  74. type: workoutExercise.type,
  75. notes: "",
  76. sets: this.getPastSets(workoutExercise._id)
  77. }
  78. this.currentSession.exercises.push(exercise);
  79. }
  80. document.getElementById("sessionExerciseName").textContent = exercise.name;
  81. const setsContainer = document.getElementById("sessionSets");
  82. while(setsContainer.children.length > 0){
  83. setsContainer.removeChild(setsContainer.firstChild);
  84. }
  85. switch(exercise.type){
  86. case "weights": this.displayWeightSets(exercise.sets, setsContainer);
  87. }
  88. },
  89. displayWeightSets: function(sets, container){
  90. const template = document.getElementById("weightSet").content.children[0];
  91. for(let i = 0; i < sets.length; i++){
  92. container.appendChild(this.createWeightSetElement(template, sets[i], i+1));
  93. }
  94. },
  95. createWeightSetElement: function(template, set, num){
  96. const setElem = template.cloneNode(true);
  97. setElem.querySelector("h3").textContent = `Set #${num}`;
  98. const weightInput = setElem.querySelector(".weightSetWeight");
  99. weightInput.value = set.weight;
  100. weightInput.addEventListener("input", ()=>{set.weight = weightInput.value});
  101. const repInput = setElem.querySelector(".weightSetReps");
  102. repInput.value = set.reps;
  103. repInput.addEventListener("input", ()=>{set.reps = repInput.value});
  104. return setElem;
  105. },
  106. getPastSets: function(id){
  107. const note = document.getElementById("previousSession");
  108. for(let i = 0; i < this.pastSessions.length; i++){
  109. for(let j = 0; j < this.pastSessions[i].exercises.length; j++){
  110. if(this.pastSessions[i].exercises[j].exerciseId === id){
  111. note.textContent = "*Data autofilled from previous workout";
  112. return this.pastSessions[i].exercises[j].sets;
  113. }
  114. }
  115. }
  116. note.textContent = "*No previous workout data";
  117. return [{weight: 0, reps: 0}, {weight: 0, reps: 0}, {weight: 0, reps: 0}];
  118. },
  119. finish: function(){
  120. this.currentSession.end = new Date();
  121. fetch(`/session`, {
  122. method: "POST",
  123. headers: {
  124. "Content-Type": "application/json"
  125. },
  126. body: JSON.stringify(this.currentSession)
  127. })
  128. .then(r=>r.json())
  129. .then((response)=>{
  130. if(response.error){
  131. notify("error", response.error.message);
  132. }else{
  133. notify("success", "Workout completed and saved");
  134. localStorage.removeItem(this.currentSession.workout);
  135. this.workout = null;
  136. this.exerciseIndex = 0;
  137. this.pastSessions = null;
  138. this.currentSession = null;
  139. changePage("home");
  140. }
  141. })
  142. .catch((err)=>{
  143. notify("error", "ERROR: unable to save workout to database");
  144. });
  145. }
  146. }