session.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. window.scrollTo(0, 0);
  168. },
  169. displayWeightSets: function(sets, container){
  170. const template = document.getElementById("weightSet").content.children[0];
  171. for(let i = 0; i < sets.length; i++){
  172. container.appendChild(this.createWeightSetElement(template, sets[i], i+1));
  173. }
  174. },
  175. createWeightSetElement: function(template, set, num){
  176. const setElem = template.cloneNode(true);
  177. setElem.querySelector("h3").textContent = `Set #${num}`;
  178. const deleteBtn = setElem.querySelector(".weightSetDelete");
  179. deleteBtn.addEventListener("click", (event)=>{
  180. this.deleteSet(num, event.target.parentElement);
  181. localStorage.setItem(this.currentSession.workout, JSON.stringify(this.currentSession));
  182. });
  183. const weightInput = setElem.querySelector(".weightSetWeight");
  184. if(set.weight !== 0) weightInput.value = set.weight;
  185. weightInput.addEventListener("input", ()=>{
  186. set.weight = weightInput.value
  187. localStorage.setItem(this.currentSession.workout, JSON.stringify(this.currentSession));
  188. });
  189. const repInput = setElem.querySelector(".weightSetReps");
  190. if(set.reps !== 0) repInput.value = set.reps;
  191. repInput.addEventListener("input", ()=>{
  192. set.reps = repInput.value
  193. localStorage.setItem(this.currentSession.workout, JSON.stringify(this.currentSession));
  194. });
  195. return setElem;
  196. },
  197. deleteSet: function(num, setElem){
  198. this.currentSession.exercises[this.exerciseIndex].sets.splice(num-1, 1);
  199. const container = setElem.parentElement;
  200. container.removeChild(setElem);
  201. for(let i = 0; i < container.children.length; i++){
  202. container.children[i].querySelector("h3").textContent = `Set #${i+1}`;
  203. }
  204. },
  205. getPastSets: function(id){
  206. const note = document.getElementById("previousSession");
  207. for(let i = 0; i < this.pastSessions.length; i++){
  208. for(let j = 0; j < this.pastSessions[i].exercises.length; j++){
  209. if(this.pastSessions[i].exercises[j]?.exerciseId === id){
  210. const exercise = this.pastSessions[i].exercises[j];
  211. const date = new Date(this.pastSessions[i].start);
  212. note.textContent = `*Data autofilled from ${this.getDate(date)}`;
  213. return exercise.sets;
  214. }
  215. }
  216. }
  217. note.textContent = "*No previous workout data";
  218. return [{weight: 0, reps: 0}, {weight: 0, reps: 0}, {weight: 0, reps: 0}];
  219. },
  220. finish: function(){
  221. this.currentSession.end = new Date();
  222. fetch(`/session`, {
  223. method: "POST",
  224. headers: {
  225. "Content-Type": "application/json"
  226. },
  227. body: JSON.stringify(this.currentSession)
  228. })
  229. .then(r=>r.json())
  230. .then((response)=>{
  231. if(response.error){
  232. notify("error", response.error.message);
  233. }else{
  234. notify("success", "Workout completed and saved");
  235. localStorage.removeItem(this.currentSession.workout);
  236. this.workout = null;
  237. this.exerciseIndex = 0;
  238. this.pastSessions = null;
  239. this.currentSession = null;
  240. changePage("home");
  241. }
  242. })
  243. .catch((err)=>{
  244. notify("error", "ERROR: unable to save workout to database");
  245. });
  246. },
  247. getDate: function(date){
  248. return date.toLocaleDateString("en-US", {
  249. year: "numeric",
  250. month: "long",
  251. day: "numeric"
  252. });
  253. }
  254. }