diff --git a/app.py b/app.py index fa044d5..3f2615a 100644 --- a/app.py +++ b/app.py @@ -124,7 +124,7 @@ def index(): name = "Hallo " + current_user.name else: habits = [] - name = "Bitte melde dich an, du Vollhorst." + name = "Bitte melde dich an." # Add checked attribute to habits (if they have been checked today) for habit in habits: @@ -357,6 +357,24 @@ def check_habit(): "unchecked": not delete_tracking } +@app.route('/delete', methods=['POST']) +@login_required +def delete_habit(): + habit_id = request.get_json()["habitId"] + + habit = Habit.get(habit_id) + + if habit is None: + return {"error": "Habit not found"} + + # Check if habit belongs to user + if habit.user_id != current_user.id: + return {"error": "Habit does not belong to user"} + + habit.delete() + + return {} + # Run the application if __name__ == '__main__': diff --git a/templates/index.html b/templates/index.html index ffc52fe..10c55d9 100644 --- a/templates/index.html +++ b/templates/index.html @@ -77,7 +77,7 @@
-
+ - +
+ {{ habit.note }} +
-
- {{ habit.note }} -
+ + {% endfor %} @@ -126,6 +129,25 @@ console.error('Error:', error); }); } + + function deleteHabit(habitId) { + // Make a POST request to /delete with the habit id + axios.post('/delete', { habitId: habitId }, { + headers: { + 'Content-Type': 'application/json' + } + }).then(function (response) { + // Handle the success response if needed + console.log(response.data); + + // Remove the habit from the DOM + var habitElement = document.getElementById("habit-" + habitId); + habitElement.remove(); + }).catch(function (error) { + // Handle the error if needed + console.error('Error:', error); + }); + }
diff --git a/templates/layouts/main.html b/templates/layouts/main.html index 6d1ed0a..e38aaa5 100644 --- a/templates/layouts/main.html +++ b/templates/layouts/main.html @@ -8,6 +8,7 @@ +