workoutMenu.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. export default {
  2. render: function(workout){
  3. document.getElementById("workoutMenuTitle").textContent = workout.name;
  4. this.buttons(workout);
  5. },
  6. buttons: function(workout){
  7. document.getElementById("menuStartWorkout").addEventListener("click", ()=>{
  8. const existingWorkout = localStorage.getItem(workout.id);
  9. if(existingWorkout === null){
  10. changePage("session", workout);
  11. }else{
  12. const container = document.getElementById("resumeWorkoutContainer");
  13. container.style.display = "flex";
  14. this.addResumeButtons(container, workout);
  15. }
  16. });
  17. document.getElementById("menuReturnHome").addEventListener("click", ()=>{
  18. changePage("home");
  19. });
  20. },
  21. addResumeButtons: function(container, workout){
  22. const finish = document.createElement("button");
  23. finish.classList.add("button");
  24. finish.textContent = "Save Previous and Start New";
  25. finish.addEventListener("click", ()=>{this.finish(workout)});
  26. container.appendChild(finish);
  27. },
  28. finish: function(workout){
  29. const session = JSON.parse(localStorage.getItem(workout.id));
  30. session.end = new Date();
  31. fetch("/session", {
  32. method: "POST",
  33. headers: {
  34. "Content-Type": "application/json"
  35. },
  36. body: JSON.stringify(session)
  37. })
  38. .then(r=>r.json())
  39. .then((response)=>{
  40. if(response.error){
  41. notify("error", response.error.message);
  42. }else{
  43. notify("success", "Workout saved");
  44. }
  45. localStorage.removeItem(workout.id);
  46. changePage("session", workout);
  47. })
  48. .catch((err)=>{
  49. notify("error", "ERROR: unable to start workout");
  50. });
  51. }
  52. }