session.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. this.currentSession.exerciseIndex = this.exerciseIndex;
  137. localStorage.setItem(this.workout.id, JSON.stringify(this.currentSession));
  138. const nextBtn = document.getElementById("nextSessionBtn");
  139. if(this.exerciseIndex === this.workout.exercises.length-1){
  140. nextBtn.style.display = "none";
  141. }else{
  142. nextBtn.style.display = "block";
  143. }
  144. let exercise = null;
  145. if(this.currentSession.exercises[this.exerciseIndex]){
  146. exercise = this.currentSession.exercises[this.exerciseIndex];
  147. }else{
  148. const workoutExercise = this.workout.exercises[this.exerciseIndex];
  149. exercise = {
  150. exerciseId: workoutExercise._id,
  151. name: workoutExercise.name,
  152. type: workoutExercise.type,
  153. notes: "",
  154. sets: this.getPastSets(workoutExercise._id),
  155. done: false
  156. }
  157. this.currentSession.exercises[this.exerciseIndex] = exercise;
  158. }
  159. document.getElementById("sessionExerciseName").textContent = exercise.name;
  160. const setsContainer = document.getElementById("sessionSets");
  161. while(setsContainer.children.length > 0){
  162. setsContainer.removeChild(setsContainer.firstChild);
  163. }
  164. switch(exercise.type){
  165. case "weights": this.displayWeightSets(exercise.sets, setsContainer);
  166. }
  167. },
  168. displayWeightSets: function(sets, container){
  169. const template = document.getElementById("weightSet").content.children[0];
  170. for(let i = 0; i < sets.length; i++){
  171. container.appendChild(this.createWeightSetElement(template, sets[i], i+1));
  172. }
  173. },
  174. createWeightSetElement: function(template, set, num){
  175. const setElem = template.cloneNode(true);
  176. setElem.querySelector("h3").textContent = `Set #${num}`;
  177. const deleteBtn = setElem.querySelector(".weightSetDelete");
  178. deleteBtn.addEventListener("click", (event)=>{
  179. this.deleteSet(num, event.target.parentElement);
  180. localStorage.setItem(this.currentSession.workout, JSON.stringify(this.currentSession));
  181. });
  182. const weightInput = setElem.querySelector(".weightSetWeight");
  183. if(set.weight > 0) weightInput.value = set.weight;
  184. weightInput.addEventListener("input", ()=>{
  185. set.weight = weightInput.value
  186. localStorage.setItem(this.currentSession.workout, JSON.stringify(this.currentSession));
  187. });
  188. const repInput = setElem.querySelector(".weightSetReps");
  189. if(set.reps !== 0) repInput.value = set.reps;
  190. repInput.addEventListener("input", ()=>{
  191. set.reps = repInput.value
  192. localStorage.setItem(this.currentSession.workout, JSON.stringify(this.currentSession));
  193. });
  194. return setElem;
  195. },
  196. deleteSet: function(num, setElem){
  197. this.currentSession.exercises[this.exerciseIndex].sets.splice(num-1, 1);
  198. const container = setElem.parentElement;
  199. container.removeChild(setElem);
  200. for(let i = 0; i < container.children.length; i++){
  201. container.children[i].querySelector("h3").textContent = `Set #${i+1}`;
  202. }
  203. },
  204. getPastSets: function(id){
  205. const note = document.getElementById("previousSession");
  206. for(let i = 0; i < this.pastSessions.length; i++){
  207. for(let j = 0; j < this.pastSessions[i].exercises.length; j++){
  208. if(this.pastSessions[i].exercises[j].exerciseId === id){
  209. note.textContent = "*Data autofilled from previous workout";
  210. return this.pastSessions[i].exercises[j].sets;
  211. }
  212. }
  213. }
  214. note.textContent = "*No previous workout data";
  215. return [{weight: 0, reps: 0}, {weight: 0, reps: 0}, {weight: 0, reps: 0}];
  216. },
  217. finish: function(){
  218. this.currentSession.end = new Date();
  219. fetch(`/session`, {
  220. method: "POST",
  221. headers: {
  222. "Content-Type": "application/json"
  223. },
  224. body: JSON.stringify(this.currentSession)
  225. })
  226. .then(r=>r.json())
  227. .then((response)=>{
  228. if(response.error){
  229. notify("error", response.error.message);
  230. }else{
  231. notify("success", "Workout completed and saved");
  232. localStorage.removeItem(this.currentSession.workout);
  233. this.workout = null;
  234. this.exerciseIndex = 0;
  235. this.pastSessions = null;
  236. this.currentSession = null;
  237. changePage("home");
  238. }
  239. })
  240. .catch((err)=>{
  241. notify("error", "ERROR: unable to save workout to database");
  242. });
  243. }
  244. }