home.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.workoutList.appendChild(this.createWorkoutElem(workouts[i]));
  57. }
  58. },
  59. handleUpdate: function(update){
  60. switch(update.type){
  61. case "new":
  62. this.workoutList.appendChild(this.createWorkoutElem(update.workout));
  63. break;
  64. case "remove": this.removeWorkout(update.workout); break;
  65. case "replace": this.replaceWorkout(update.workout); break;
  66. }
  67. },
  68. createWorkoutElem: function(workout){
  69. const button = document.createElement("button");
  70. button.workoutId = workout.id;
  71. button.classList.add("button");
  72. button.textContent = workout.name;
  73. button.addEventListener("click", ()=>{
  74. changePage("workoutMenu", workout);
  75. });
  76. return button;
  77. },
  78. removeWorkout: function(workoutId){
  79. const workouts = this.workoutList.children;
  80. for(let i = 0; i < workouts.length; i++){
  81. if(workouts[i].workoutId === workoutId){
  82. this.workoutList.removeChild(workouts[i]);
  83. break;
  84. }
  85. }
  86. },
  87. replaceWorkout: function(workout){
  88. const workouts = this.workoutList.children;
  89. for(let i = 0; i < workouts.length; i++){
  90. if(workouts[i].workoutId === workout.id){
  91. workouts[i].replaceWith(this.createWorkoutElem(workout));
  92. break;
  93. }
  94. }
  95. }
  96. }