Просмотр исходного кода

Update frontend to save/display notes.

Lee Morgan 1 год назад
Родитель
Сommit
c26419655e
2 измененных файлов с 34 добавлено и 10 удалено
  1. 3 3
      controllers/workout.js
  2. 31 7
      views/js/pages/session.js

+ 3 - 3
controllers/workout.js

@@ -19,9 +19,9 @@ const getRoute = async (req, res, next)=>{
 
 const addNoteRoute = async (req, res, next)=>{
     try{
-        const workout = await Workout.find({_id: req.params.workoutId});
+        const workout = await Workout.findOne({_id: req.params.workoutId});
         confirmOwnership(workout, res.locals.user);
-        const exercise = findExercise(workout, req.body.exerciseId);
+        const exercise = findExercise(workout, req.body.exercise);
         exercise.notes = req.body.note;
         await workout.save();
         res.json(responseWorkout(workout));
@@ -61,7 +61,7 @@ const confirmOwnership = (workout, user)=>{
  */
 const findExercise = (workout, exerciseId)=>{
     for(let i = 0; i < workout.exercises.length; i++){
-        if(workout.exercises._id.toString === exerciseId){
+        if(workout.exercises[i]._id.toString() === exerciseId){
             return workout.exercises[i];
         }
     }

+ 31 - 7
views/js/pages/session.js

@@ -46,9 +46,7 @@ export default {
             document.getElementById("finishSessionBtn").addEventListener("click", ()=>{this.finish()});
             document.getElementById("sessionAddSet").addEventListener("click", ()=>{this.addSet()});
             document.getElementById("sessionNotesBtn").addEventListener("click", this.displayNotes.bind(this));
-            document.getElementById("sessionNotesDone").addEventListener("click", (event)=>{
-                event.target.parentElement.style.display = "none";
-            });
+            document.getElementById("sessionNotesDone").addEventListener("click", this.closeNote.bind(this));
             this.rendered = true;
         }
     },
@@ -58,10 +56,36 @@ export default {
         container.style.display = "flex";
 
         const textarea = container.querySelector("textarea");
-        textarea.textContent = this.currentSession.exercises[this.exerciseIndex].notes;
-        textarea.addEventListener("input", ()=>{
-            this.currentSession.exercises[this.exerciseIndex].notes = textarea.value;
-        });
+        textarea.textContent = this.workout.exercises[this.exerciseIndex].notes;
+    },
+
+    closeNote: function(){
+        const exercise = this.workout.exercises.find(e => e._id === this.currentSession.exercises[this.exerciseIndex].exerciseId);
+        const newNote = document.getElementById("sessionTextArea").value;
+        if(exercise.notes !== newNote){
+            fetch(`/workout/${this.workout.id}/note`, {
+                method: "PUT",
+                headers: {
+                    "Content-Type": "application/json"
+                },
+                body: JSON.stringify({
+                    exercise: this.currentSession.exercises[this.exerciseIndex].exerciseId,
+                    note: newNote
+                })
+            })
+                .then(r=>r.json())
+                .then((response)=>{
+                    if(response.error){
+                        notify("error", response.error.message);
+                    }
+                    exercise.notes = newNote;
+                })
+                .catch((err)=>{
+                    notify("error", "ERROR: unable to save note");
+                });
+        }
+
+        document.getElementById("sessionNotesText").style.display = "none";
     },
 
     addSet: function(){