From af585ff49ffd14f65d3780a2589b90001cdf36c1 Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Wed, 12 Jun 2024 10:30:12 +0200 Subject: [PATCH] fix(views): set htmx to use API delete endpoint The API provides an endpoint to delete references. We can use that for the htmx `hx-delete` argument instead of using the delete view and configuring it to react on htmx requests. --- README.md | 15 +++++++++++++++ .../apis_bibsonomy/partials/reference_list.html | 2 +- apis_bibsonomy/views.py | 7 ------- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e34e292..5191b66 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,21 @@ APIS_BIBSONOMY_FIELDS = ['name', 'first_name', 'profession'] Restart your server and you are good to go. +### htmx + +APIS Bibsonomy uses htmx. The delete buttons for references trigger deletion +via the API delete route which give an empty response on success. HTMX needs to +be [configured to swap the content even if the response is empty](https://github.com/bigskysoftware/htmx/issues/199): +``` +document.body.addEventListener('htmx:beforeSwap', function(event) { + if (event.detail.xhr.status === 204) { + // Swap content even when the response is empty. + event.detail.shouldSwap = true; + } +}); +``` + + ## Usage *not needed if you are using standard APIS templates* diff --git a/apis_bibsonomy/templates/apis_bibsonomy/partials/reference_list.html b/apis_bibsonomy/templates/apis_bibsonomy/partials/reference_list.html index 0ec64a5..cbe378d 100644 --- a/apis_bibsonomy/templates/apis_bibsonomy/partials/reference_list.html +++ b/apis_bibsonomy/templates/apis_bibsonomy/partials/reference_list.html @@ -5,7 +5,7 @@ {% if request.user.is_authenticated %} {{ reference }} ({{ reference.id }}) Delete diff --git a/apis_bibsonomy/views.py b/apis_bibsonomy/views.py index 522548b..a7f2ff0 100644 --- a/apis_bibsonomy/views.py +++ b/apis_bibsonomy/views.py @@ -28,13 +28,6 @@ def get_success_url(self): ) return red - def delete(self, request, *args, **kwargs): - resp = super().delete(request, *args, **kwargs) - # we set the status code to 200 for HTMX requests, so they don't get redirected - if "HX-Request" in request.headers: - resp.status_code = 200 - return resp - class ReferenceListView(ListView): model = Reference