Add ability to add/delete exercises from dropdown

This commit is contained in:
Peter Stockings
2024-11-04 16:46:22 +11:00
parent 7d43965289
commit 7d65f9b8e8
4 changed files with 42 additions and 3 deletions

13
app.py
View File

@@ -305,10 +305,10 @@ def update_exercise(exercise_id):
return render_template('partials/exercise.html', exercise_id=exercise_id, name=new_name) return render_template('partials/exercise.html', exercise_id=exercise_id, name=new_name)
@ app.route("/exercise/<int:exercise_id>/delete", methods=['DELETE']) """ @ app.route("/exercise/<int:exercise_id>/delete", methods=['DELETE'])
def delete_exercise(exercise_id): def delete_exercise(exercise_id):
db.delete_exercise(exercise_id) db.delete_exercise(exercise_id)
return "" return "" """
@ app.route("/settings") @ app.route("/settings")
@@ -454,7 +454,16 @@ def edit_exercise_name(exercise_id):
updated_exercise = db.exercises.update_exercise_name(exercise_id, updated_name) updated_exercise = db.exercises.update_exercise_name(exercise_id, updated_name)
return render_template('partials/exercise/exercise_list_item.html', exercise=updated_exercise) return render_template('partials/exercise/exercise_list_item.html', exercise=updated_exercise)
@app.route("/exercises/add", methods=['POST'])
def add_exercise():
exercise_name = request.form['query']
new_exercise = db.exercises.add_exercise(exercise_name)
return render_template('partials/exercise/exercise_list_item.html', exercise=new_exercise)
@ app.route("/exercise/<int:exercise_id>/delete", methods=['DELETE'])
def delete_exercise(exercise_id):
db.exercises.delete_exercise(exercise_id)
return ""
@app.teardown_appcontext @app.teardown_appcontext
def closeConnection(exception): def closeConnection(exception):

View File

@@ -18,3 +18,14 @@ class Exercises:
self.execute(sql, params) self.execute(sql, params)
updated_exercise = self.get_exercise(exercise_id) updated_exercise = self.get_exercise(exercise_id)
return updated_exercise return updated_exercise
def delete_exercise(self, exercise_id):
self.execute('DELETE FROM exercise WHERE exercise_id=%s', [
exercise_id], commit=True)
def add_exercise(self, name):
result = self.execute('INSERT INTO exercise (name) VALUES (%s) RETURNING exercise_id', [name], commit=True, one=True)
exercise_id = result['exercise_id']
new_exercise = self.get_exercise(exercise_id)
return new_exercise

View File

@@ -13,4 +13,15 @@
<path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" /> <path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" />
</svg> </svg>
</button> </button>
<!-- Delete Icon -->
<button hx-delete="{{ url_for('delete_exercise', exercise_id=exercise.exercise_id) }}" hx-target="closest li"
hx-swap="outerHTML" class="text-red-500 hover:text-red-700 ml-2"
_="on click from me call event.stopPropagation()">
<!-- Trash icon SVG -->
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"
stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round"
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6M9 7h6m2 0a2 2 0 00-2-2H9a2 2 0 00-2 2h12z" />
</svg>
</button>
</li> </li>

View File

@@ -5,5 +5,13 @@
{% endfor %} {% endfor %}
</ul> </ul>
{% else %} {% else %}
<div class="py-2 px-4 text-gray-500">No results found</div> <div class="py-2 px-4 text-gray-500 flex items-center justify-between">
<span>No results found</span>
<!-- Add Exercise Button -->
<button hx-post="{{ url_for('add_exercise') }}" hx-target="closest div" hx-swap="outerHTML"
hx-include="[name='query']" class="text-blue-500 hover:text-blue-700 font-semibold"
_="on click from me call event.stopPropagation()">
Add Exercise
</button>
</div>
{% endif %} {% endif %}