|
|
@@ -1,7 +1,9 @@
|
|
|
export default {
|
|
|
rendered: false,
|
|
|
+ workout: null,
|
|
|
|
|
|
render: function(workout){
|
|
|
+ this.workout = workout;
|
|
|
document.querySelector("#editWorkoutPage h1").textContent = workout.name;
|
|
|
this.displayExercises(workout.exercises);
|
|
|
|
|
|
@@ -19,12 +21,17 @@ export default {
|
|
|
container.querySelector("input").focus();
|
|
|
});
|
|
|
document.getElementById("newExerciseSubmit").addEventListener("click", this.addExercise.bind(this));
|
|
|
+ document.getElementById("editWorkoutSubmit").addEventListener("click", this.submitWorkout.bind(this));
|
|
|
},
|
|
|
|
|
|
displayExercises: function(exercises){
|
|
|
const container = document.getElementById("editWorkoutExercises");
|
|
|
const template = document.getElementById("editWorkoutExercise").content.children[0];
|
|
|
|
|
|
+ while(container.children.length > 0){
|
|
|
+ container.removeChild(container.firstChild);
|
|
|
+ }
|
|
|
+
|
|
|
for(let i = 0; i < exercises.length; i++){
|
|
|
const exercise = template.cloneNode(true);
|
|
|
exercise.setAttribute("data-id", exercises[i]._id);
|
|
|
@@ -76,5 +83,44 @@ export default {
|
|
|
container.appendChild(exercise);
|
|
|
|
|
|
input.value = "";
|
|
|
+ document.getElementById("newExerciseContainer").style.display = "none";
|
|
|
+ },
|
|
|
+
|
|
|
+ submitWorkout: function(){
|
|
|
+ const container = document.getElementById("editWorkoutExercises");
|
|
|
+
|
|
|
+ const existingExercises = [];
|
|
|
+ for(let i = 0; i < container.children.length; i++){
|
|
|
+ const item = container.children[i];
|
|
|
+ const exerciseId = item.getAttribute("data-id");
|
|
|
+ if(exerciseId){
|
|
|
+ existingExercises.push({id: exerciseId});
|
|
|
+ }else{
|
|
|
+ existingExercises.push({
|
|
|
+ new: item.querySelector("p").textContent,
|
|
|
+ type: "weights"
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fetch(`/workout/${this.workout.id}`, {
|
|
|
+ method: "PUT",
|
|
|
+ headers: {
|
|
|
+ "Content-Type": "application/json"
|
|
|
+ },
|
|
|
+ body: JSON.stringify({existingExercises: existingExercises})
|
|
|
+ })
|
|
|
+ .then(r=>r.json())
|
|
|
+ .then((response)=>{
|
|
|
+ if(response.error){
|
|
|
+ notify("error", response.error.message);
|
|
|
+ }else{
|
|
|
+ notify("success", "Workout updated");
|
|
|
+ changePage("workoutMenu", response);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .catch((err)=>{
|
|
|
+ notify("error", "ERROR: unable to save workout");
|
|
|
+ });
|
|
|
}
|
|
|
}
|