Add ability to add/update/delete exercise categories

This commit is contained in:
Peter Stockings
2026-02-08 16:48:47 +11:00
parent ce28f7f749
commit a6eca1b4ac
6 changed files with 269 additions and 3 deletions

View File

@@ -0,0 +1,45 @@
<div id="attribute-{{ attribute.attribute_id }}"
class="group flex items-center px-3 py-1 bg-white border border-gray-200 rounded-lg shadow-sm hover:border-cyan-300 transition-all">
{% if is_edit %}
<form hx-put="{{ url_for('exercises.update_attribute', attribute_id=attribute.attribute_id) }}"
hx-target="#attribute-{{ attribute.attribute_id }}" hx-swap="outerHTML" class="flex items-center space-x-2">
<input type="text" name="name" value="{{ attribute.name }}"
class="text-xs font-semibold text-gray-700 bg-gray-50 border-none p-0 focus:ring-0 w-20" autofocus
onfocus="this.select()">
<button type="submit" class="text-green-600 hover:text-green-700">
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 13l4 4L19 7"></path>
</svg>
</button>
<button type="button" hx-get="{{ url_for('exercises.update_attribute', attribute_id=attribute.attribute_id) }}"
hx-target="#attribute-{{ attribute.attribute_id }}" hx-swap="outerHTML"
class="text-gray-400 hover:text-gray-600">
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
</form>
{% else %}
<span class="text-xs font-medium text-gray-700 mr-2">{{ attribute.name }}</span>
<div class="hidden group-hover:flex items-center space-x-1 ml-auto">
<button hx-get="{{ url_for('exercises.update_attribute', attribute_id=attribute.attribute_id) }}?is_edit=true"
hx-target="#attribute-{{ attribute.attribute_id }}" hx-swap="outerHTML"
class="p-1 text-gray-400 hover:text-cyan-600 transition-colors">
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z">
</path>
</svg>
</button>
<button hx-delete="{{ url_for('exercises.update_attribute', attribute_id=attribute.attribute_id) }}"
hx-target="#attribute-{{ attribute.attribute_id }}" hx-swap="outerHTML" hx-confirm="Delete this attribute?"
class="p-1 text-gray-400 hover:text-red-600 transition-colors">
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16">
</path>
</svg>
</button>
</div>
{% endif %}
</div>

View File

@@ -0,0 +1,76 @@
<div id="category-{{ category_id }}"
class="bg-gray-50 border border-gray-200 rounded-2xl p-6 transition-all hover:shadow-md">
<div class="flex items-center justify-between mb-6">
<div class="flex items-center space-x-4">
<div class="p-2 bg-cyan-100 rounded-lg text-cyan-600">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M7 7h.01M7 11h.01M7 15h.01M11 7h.01M11 11h.01M11 15h.01M15 7h.01M15 11h.01M15 15h.01M19 7h.01M19 11h.01M19 15h.01M7 3h10a2 2 0 012 2v14a2 2 0 01-2 2H7a2 2 0 01-2-2V5a2 2 0 012-2z">
</path>
</svg>
</div>
{% if is_edit %}
<form hx-put="{{ url_for('exercises.update_category', category_id=category_id) }}"
hx-target="#category-{{ category_id }}" hx-swap="outerHTML" class="flex items-center space-x-2">
<input type="text" name="name" value="{{ name }}"
class="text-lg font-bold text-gray-900 bg-white border border-cyan-200 rounded-lg px-2 py-1 focus:ring-2 focus:ring-cyan-500/20 focus:border-cyan-500 outline-none"
autofocus onfocus="this.select()">
<button type="submit" class="text-green-600 hover:text-green-700 font-bold p-1">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 13l4 4L19 7"></path>
</svg>
</button>
<button type="button" hx-get="{{ url_for('exercises.update_category', category_id=category_id) }}"
hx-target="#category-{{ category_id }}" hx-swap="outerHTML"
class="text-gray-400 hover:text-gray-600 p-1">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M6 18L18 6M6 6l12 12">
</path>
</svg>
</button>
</form>
{% else %}
<h5 class="text-lg font-bold text-gray-900">{{ name }}</h5>
<button hx-get="{{ url_for('exercises.update_category', category_id=category_id) }}?is_edit=true"
hx-target="#category-{{ category_id }}" hx-swap="outerHTML"
class="text-gray-400 hover:text-cyan-600 p-1 transition-colors">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z">
</path>
</svg>
</button>
{% endif %}
</div>
<button hx-delete="{{ url_for('exercises.update_category', category_id=category_id) }}"
hx-target="#category-{{ category_id }}" hx-swap="outerHTML"
hx-confirm="Deleteting '{{ name }}' will also delete all its attributes. Are you sure?"
class="text-gray-400 hover:text-red-600 p-2 rounded-lg hover:bg-red-50 transition-all">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16">
</path>
</svg>
</button>
</div>
<div class="flex flex-wrap gap-2 mb-6" id="attributes-list-{{ category_id }}">
{% for attr in attributes %}
{{ render_partial('partials/exercise/attribute_admin.html', attribute=attr) }}
{% endfor %}
</div>
<form hx-post="{{ url_for('exercises.create_attribute') }}" hx-target="#attributes-list-{{ category_id }}"
hx-swap="beforeend" _="on htmx:afterRequest reset() me" class="flex items-center space-x-2">
<input type="hidden" name="category_id" value="{{ category_id }}">
<input type="text" name="name" placeholder="Add new option..."
class="flex-1 text-sm bg-white border border-gray-200 rounded-xl px-4 py-2 focus:ring-2 focus:ring-cyan-500/20 focus:border-cyan-500 outline-none transition-all">
<button type="submit"
class="bg-white border border-gray-200 text-gray-600 hover:text-cyan-600 hover:border-cyan-200 p-2 rounded-xl transition-all shadow-sm active:scale-95">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
</svg>
</button>
</form>
</div>