session.js 11 KB

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