session.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. export default{
  2. workout: null,
  3. pastSessions: null,
  4. exerciseIndex: 0,
  5. currentSession: null,
  6. render: async function(workout){
  7. this.workout = workout;
  8. this.pastSessions = await this.getPastSessions(workout.id);
  9. this.buttons();
  10. this.createNewSession(workout);
  11. this.changeExercise(0);
  12. },
  13. getPastSessions: async function(id){
  14. const sessions = await fetch(`/session/${id}`, {
  15. method: "GET",
  16. headers: {
  17. "Content-Type": "application/json"
  18. }
  19. });
  20. if(sessions.error){
  21. notify("error", "ERROR: Unable to retrieve past workouts");
  22. return [];
  23. }
  24. return await sessions.json();
  25. },
  26. buttons: function(){
  27. document.getElementById("nextSessionBtn").addEventListener("click", ()=>{this.changeExercise(1)});
  28. document.getElementById("finishSessionBtn").addEventListener("click", ()=>{this.finish()});
  29. },
  30. createNewSession: function(workout){
  31. this.currentSession = {
  32. workout: workout.id,
  33. start: new Date(),
  34. notes: "",
  35. exercises: []
  36. };
  37. localStorage.setItem(workout.id, JSON.stringify(this.currentSession));
  38. },
  39. changeExercise: function(num){
  40. if(num !== 0) localStorage.setItem(this.workout.id, JSON.stringify(this.currentSession));
  41. this.exerciseIndex += num;
  42. if(this.exerciseIndex === this.workout.exercises.length-1){
  43. document.getElementById("nextSessionBtn").style.display = "none";
  44. }
  45. let exercise = null;
  46. if(this.currentSession[this.exerciseIndex]){
  47. exercise = currentSession[this.exerciseIndex];
  48. }else{
  49. const workoutExercise = this.workout.exercises[this.exerciseIndex];
  50. exercise = {
  51. exerciseId: workoutExercise._id,
  52. name: workoutExercise.name,
  53. type: workoutExercise.type,
  54. notes: "",
  55. sets: this.getPastSets(workoutExercise._id)
  56. }
  57. this.currentSession.exercises.push(exercise);
  58. }
  59. document.getElementById("sessionExerciseName").textContent = exercise.name;
  60. const setsContainer = document.getElementById("sessionSets");
  61. while(setsContainer.children.length > 0){
  62. setsContainer.removeChild(setsContainer.firstChild);
  63. }
  64. switch(exercise.type){
  65. case "weights": this.displayWeightSets(exercise, setsContainer);
  66. }
  67. },
  68. displayWeightSets: function(exercise, container){
  69. const weightTemplate = document.getElementById("weightSet").content.children[0];
  70. for(let i = 0; i < exercise.sets.length; i++){
  71. const set = weightTemplate.cloneNode(true);
  72. set.querySelector("h3").textContent = `Set #${i+1}`;
  73. const weightInput = set.querySelector(".weightSetWeight");
  74. weightInput.value = exercise.sets[i].weight
  75. weightInput.addEventListener("input", ()=>{exercise.sets[i].weight = weightInput.value});
  76. const repInput = set.querySelector(".weightSetReps");
  77. repInput.value = exercise.sets[i].reps;
  78. repInput.addEventListener("input", ()=>{exercise.sets[i].reps = repInput.value});
  79. container.appendChild(set);
  80. }
  81. },
  82. getPastSets: function(id){
  83. for(let i = 0; i < this.pastSessions.length; i++){
  84. for(let j = 0; j < this.pastSessions[i].exercises.length; j++){
  85. if(this.pastSessions[i].exercises[j].exerciseId === id){
  86. return this.pastSessions[i].exercises[j].sets;
  87. }
  88. }
  89. }
  90. return [{weight: 0, reps: 0}, {weight: 0, reps: 0}, {weight: 0, reps: 0}];
  91. },
  92. finish: function(){
  93. //save to database
  94. //delete from localStorage
  95. console.log(this.currentSession);
  96. this.currentSession.end = new Date();
  97. fetch(`/session`, {
  98. method: "POST",
  99. headers: {
  100. "Content-Type": "application/json"
  101. },
  102. body: JSON.stringify(this.currentSession)
  103. })
  104. .then(r=>r.json())
  105. .then((response)=>{
  106. if(response.error){
  107. notify("error", response.error.message);
  108. }else{
  109. notify("success", "Workout completed and saved");
  110. localStorage.removeItem(this.currentSession.workout);
  111. changePage("home");
  112. }
  113. })
  114. .catch((err)=>{
  115. notify("error", "ERROR: unable to save workout to database");
  116. });
  117. }
  118. }