diff --git a/app.py b/app.py index fa41487..39393fd 100644 --- a/app.py +++ b/app.py @@ -64,7 +64,10 @@ def index(): day = None heatmap_color = None - # Sort habits by whether they have been checked today and then by slot + # Sort habit_lists based on their order attribute + habit_lists = sorted(habit_lists, key=lambda habitList: habitList.slot) + + # Sort habits within each habit_list by slot for habit_list in habit_lists: habit_list.habits = sorted(habit_list.get_habits(), key=lambda habit: (habit.checked, habit.slot)) for habit in habit_list.get_habits(): @@ -704,7 +707,7 @@ def deny_list(): @login_required def reorder_habit_list(): new_index = request.get_json()["newIndex"] + 1 - habitList = HabitList.get(request.get_json()["habitListId"]) + habitList = HabitList.get(request.get_json()["listId"]) if habitList is None: return {"error": "HabitList not found"} @@ -714,7 +717,7 @@ def reorder_habit_list(): if current_user not in users: return {"error": "HabitList does not belong to user"} - habitList.update_slot(new_index) + habitList.update_slot(current_user.id, new_index) return {} ########################################################### diff --git a/static/script/script-index.js b/static/script/script-index.js index c08b353..31cb15a 100644 --- a/static/script/script-index.js +++ b/static/script/script-index.js @@ -164,6 +164,35 @@ document.addEventListener('DOMContentLoaded', () => { } }); +document.addEventListener('DOMContentLoaded', () => { + const listElements = document.querySelectorAll('.nav-tabs').values(); + + // Loop through the list elements + for (let listEl of listElements) { + Sortable.create(listEl, { + handle: '.nav-link', // Use the nav-link as the handle for dragging + animation: 150, + onEnd: function (evt) { + const listId = evt.item.id.split('-')[1]; + const oldIndex = evt.oldIndex; + const newIndex = evt.newIndex; + + // Send a POST request to reorder the list + axios.post('/reorder-list', { listId: listId, oldIndex: oldIndex, newIndex: newIndex}, { + headers: { + 'Content-Type': 'application/json' + } + }).then(function (response) { + // Handle the success response if needed + }).catch(function (error) { + // Handle the error if needed + console.error('Error:', error); + }); + } + }); + } +}); + $(function () { $('[data-toggle="tooltip"]').tooltip() }) diff --git a/templates/components/habit_lists.html b/templates/components/habit_lists.html index 5c70175..0c7c12b 100644 --- a/templates/components/habit_lists.html +++ b/templates/components/habit_lists.html @@ -1,3 +1,4 @@ +