session.js 11 KB

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