home.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. export default {
  2. rendered: false,
  3. workoutList: document.getElementById("workoutList"),
  4. render: function(update){
  5. if(!this.rendered){
  6. this.getWorkouts();
  7. this.buttons();
  8. this.rendered = true;
  9. }
  10. if(update) this.handleUpdate(update);
  11. },
  12. buttons: function(){
  13. document.getElementById("homeLogout").addEventListener("click", ()=>{
  14. fetch("/user/logout")
  15. .then(r=>r.json())
  16. .then((response)=>{
  17. if(response.error){
  18. notify("error", response.error.message);
  19. }else{
  20. localStorage.removeItem("loggedIn");
  21. changePage("login");
  22. }
  23. })
  24. .catch((err)=>{
  25. notify("error", "Something went wrong, try refreshing the page");
  26. });
  27. });
  28. document.getElementById("homeNewWorkout").addEventListener("click", ()=>{
  29. changePage("newWorkout");
  30. });
  31. },
  32. getWorkouts: function(){
  33. fetch("/workout", {
  34. method: "get",
  35. headers: {
  36. "Content-Type": "application/json"
  37. }
  38. })
  39. .then(r=>r.json())
  40. .then((response)=>{
  41. if(response.error){
  42. notify("error", response.error.message);
  43. }else{
  44. this.renderWorkouts(response);
  45. }
  46. })
  47. .catch((err)=>{
  48. notify("error", "ERROR: Unable to retrieve workouts");
  49. });
  50. },
  51. renderWorkouts: function(workouts){
  52. while(this.workoutList.children.length > 0){
  53. this.workoutList.removeChild(this.workoutList.firstChild);
  54. }
  55. for(let i = 0; i < workouts.length; i++){
  56. this.addWorkout(workouts[i]);
  57. }
  58. },
  59. handleUpdate: function(update){
  60. switch(update.type){
  61. case "new": this.addWorkout(update.workout); break;
  62. case "remove": this.removeWorkout(update.workout); break;
  63. case "replace": this.replaceWorkout(update.workout); break;
  64. }
  65. },
  66. addWorkout: function(workout){
  67. const button = document.createElement("button");
  68. button.workoutId = workout.id;
  69. button.classList.add("button");
  70. button.textContent = workout.name;
  71. button.addEventListener("click", ()=>{
  72. changePage("workoutMenu", workout);
  73. });
  74. this.workoutList.appendChild(button);
  75. },
  76. removeWorkout: function(workoutId){
  77. const workouts = this.workoutList.children;
  78. for(let i = 0; i < workouts.length; i++){
  79. if(workouts[i].workoutId === workoutId){
  80. this.workoutList.removeChild(workouts[i]);
  81. break;
  82. }
  83. }
  84. },
  85. replaceWorkout: function(workout){
  86. const workouts = this.workoutList.children;
  87. for(let i = 0; i < workouts.length; i++){
  88. if(workouts[i].workoutId === workout.id){
  89. this.workoutList.removeChild(workouts[i]);
  90. this.addWorkout(workout);
  91. break;
  92. }
  93. }
  94. }
  95. }