session.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. const noteIcon = document.getElementById("sessionNotesIcon");
  113. console.log(newNote);
  114. if(newNote === ""){
  115. noteIcon.style.display = "none";
  116. }else{
  117. noteIcon.style.display = "flex";
  118. }
  119. document.getElementById("sessionNotesText").style.display = "none";
  120. },
  121. addSet: function(){
  122. const exercise = this.currentSession.exercises[this.exerciseIndex];
  123. const template = document.getElementById("weightSet").content.children[0];
  124. const container = document.getElementById("sessionSets");
  125. switch(exercise.type){
  126. case "weights":
  127. const newSet = {weight: 0, reps: 0};
  128. const setNumber = container.children.length + 1;
  129. exercise.sets.push(newSet);
  130. container.appendChild(this.createWeightSetElement(template, newSet, setNumber));
  131. break;
  132. }
  133. },
  134. createNewSession: function(workout){
  135. session = {
  136. workout: workout.id,
  137. start: new Date(),
  138. notes: "",
  139. exercises: Array(this.workout.exercises.length).fill(null),
  140. exerciseIndex: this.exerciseIndex
  141. };
  142. localStorage.setItem(workout.id, JSON.stringify(session));
  143. return session;
  144. },
  145. changeExercise: function(num){
  146. this.exerciseIndex = num;
  147. this.currentSession.exerciseIndex = this.exerciseIndex;
  148. localStorage.setItem(this.workout.id, JSON.stringify(this.currentSession));
  149. const nextBtn = document.getElementById("nextSessionBtn");
  150. if(this.exerciseIndex === this.workout.exercises.length-1){
  151. nextBtn.style.display = "none";
  152. }else{
  153. nextBtn.style.display = "block";
  154. }
  155. let exercise = null;
  156. if(this.currentSession.exercises[this.exerciseIndex]){
  157. exercise = this.currentSession.exercises[this.exerciseIndex];
  158. }else{
  159. const workoutExercise = this.workout.exercises[this.exerciseIndex];
  160. exercise = {
  161. exerciseId: workoutExercise._id,
  162. name: workoutExercise.name,
  163. type: workoutExercise.type,
  164. notes: "",
  165. sets: this.getPastSets(workoutExercise._id),
  166. done: false
  167. }
  168. this.currentSession.exercises[this.exerciseIndex] = exercise;
  169. }
  170. document.getElementById("sessionExerciseName").textContent = exercise.name;
  171. const setsContainer = document.getElementById("sessionSets");
  172. while(setsContainer.children.length > 0){
  173. setsContainer.removeChild(setsContainer.firstChild);
  174. }
  175. switch(exercise.type){
  176. case "weights": this.displayWeightSets(exercise.sets, setsContainer);
  177. }
  178. const noteIcon = document.getElementById("sessionNotesIcon");
  179. if(this.workout.exercises[this.exerciseIndex].notes !== ""){
  180. noteIcon.style.display = "flex";
  181. }else{
  182. noteIcon.style.display = "none";
  183. }
  184. window.scrollTo(0, 0);
  185. },
  186. displayWeightSets: function(sets, container){
  187. const template = document.getElementById("weightSet").content.children[0];
  188. for(let i = 0; i < sets.length; i++){
  189. container.appendChild(this.createWeightSetElement(template, sets[i], i+1));
  190. }
  191. },
  192. createWeightSetElement: function(template, set, num){
  193. const setElem = template.cloneNode(true);
  194. setElem.querySelector("h3").textContent = `Set #${num}`;
  195. const deleteBtn = setElem.querySelector(".weightSetDelete");
  196. deleteBtn.addEventListener("click", (event)=>{
  197. this.deleteSet(num, event.target.parentElement);
  198. localStorage.setItem(this.currentSession.workout, JSON.stringify(this.currentSession));
  199. });
  200. const weightInput = setElem.querySelector(".weightSetWeight");
  201. if(set.weight !== 0) weightInput.value = set.weight;
  202. weightInput.addEventListener("input", ()=>{
  203. set.weight = weightInput.value
  204. localStorage.setItem(this.currentSession.workout, JSON.stringify(this.currentSession));
  205. });
  206. const repInput = setElem.querySelector(".weightSetReps");
  207. if(set.reps !== 0) repInput.value = set.reps;
  208. repInput.addEventListener("input", ()=>{
  209. set.reps = repInput.value
  210. localStorage.setItem(this.currentSession.workout, JSON.stringify(this.currentSession));
  211. });
  212. return setElem;
  213. },
  214. deleteSet: function(num, setElem){
  215. this.currentSession.exercises[this.exerciseIndex].sets.splice(num-1, 1);
  216. const container = setElem.parentElement;
  217. container.removeChild(setElem);
  218. for(let i = 0; i < container.children.length; i++){
  219. container.children[i].querySelector("h3").textContent = `Set #${i+1}`;
  220. }
  221. },
  222. getPastSets: function(id){
  223. const note = document.getElementById("previousSession");
  224. for(let i = 0; i < this.pastSessions.length; i++){
  225. for(let j = 0; j < this.pastSessions[i].exercises.length; j++){
  226. if(this.pastSessions[i].exercises[j]?.exerciseId === id){
  227. const exercise = this.pastSessions[i].exercises[j];
  228. const date = new Date(this.pastSessions[i].start);
  229. note.textContent = `*Data autofilled from ${this.getDate(date)}`;
  230. return exercise.sets;
  231. }
  232. }
  233. }
  234. note.textContent = "*No previous workout data";
  235. return [{weight: 0, reps: 0}, {weight: 0, reps: 0}, {weight: 0, reps: 0}];
  236. },
  237. finish: function(){
  238. this.currentSession.end = new Date();
  239. fetch(`/session`, {
  240. method: "POST",
  241. headers: {
  242. "Content-Type": "application/json"
  243. },
  244. body: JSON.stringify(this.currentSession)
  245. })
  246. .then(r=>r.json())
  247. .then((response)=>{
  248. if(response.error){
  249. notify("error", response.error.message);
  250. }else{
  251. notify("success", "Workout completed and saved");
  252. localStorage.removeItem(this.currentSession.workout);
  253. this.workout = null;
  254. this.exerciseIndex = 0;
  255. this.pastSessions = null;
  256. this.currentSession = null;
  257. changePage("home");
  258. }
  259. })
  260. .catch((err)=>{
  261. notify("error", "ERROR: unable to save workout to database");
  262. });
  263. },
  264. getDate: function(date){
  265. return date.toLocaleDateString("en-US", {
  266. year: "numeric",
  267. month: "long",
  268. day: "numeric"
  269. });
  270. }
  271. }