| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- export default {
- rendered: false,
- workoutList: document.getElementById("workoutList"),
- render: function(update){
- if(!this.rendered){
- this.getWorkouts();
- this.buttons();
- this.rendered = true;
- }
- if(update) this.handleUpdate(update);
- },
- buttons: function(){
- document.getElementById("homeLogout").addEventListener("click", ()=>{
- fetch("/user/logout")
- .then(r=>r.json())
- .then((response)=>{
- if(response.error){
- notify("error", response.error.message);
- }else{
- localStorage.removeItem("loggedIn");
- changePage("login");
- }
- })
- .catch((err)=>{
- notify("error", "Something went wrong, try refreshing the page");
- });
- });
- document.getElementById("homeNewWorkout").addEventListener("click", ()=>{
- changePage("newWorkout");
- });
- },
- getWorkouts: function(){
- fetch("/workout", {
- method: "get",
- headers: {
- "Content-Type": "application/json"
- }
- })
- .then(r=>r.json())
- .then((response)=>{
- if(response.error){
- notify("error", response.error.message);
- }else{
- this.renderWorkouts(response);
- }
- })
- .catch((err)=>{
- notify("error", "ERROR: Unable to retrieve workouts");
- });
- },
- renderWorkouts: function(workouts){
- while(this.workoutList.children.length > 0){
- this.workoutList.removeChild(this.workoutList.firstChild);
- }
- for(let i = 0; i < workouts.length; i++){
- this.addWorkout(workouts[i]);
- }
- },
- addWorkout: function(workout){
- const button = document.createElement("button");
- button.classList.add("button");
- button.textContent = workout.name;
- button.addEventListener("click", ()=>{
- changePage("workoutMenu", workout);
- });
- this.workoutList.appendChild(button);
- },
- handleUpdate: function(update){
- switch(update.type){
- case "new": this.addWorkout(update.workout); break;
- }
- }
- }
|