Skip to content

Commit

Permalink
fix(views): set htmx to use API delete endpoint
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
b1rger committed Jul 29, 2024
1 parent 181852b commit af585ff
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% if request.user.is_authenticated %}
<a href="{{ reference.get_absolute_url }}">{{ reference }} ({{ reference.id }})</a>
<a href="{% url "apis_bibsonomy:referencedelete" reference.id %}?redirect={{ request.path }}"
hx-delete="{% url "apis_bibsonomy:referencedelete" reference.id %}"
hx-delete="{% url "apis_bibsonomy:reference-detail" reference.id %}"
hx-confirm="Are your sure you want to delete reference {{ reference }} for {{ reference.referenced_object }}"
hx-target="closest li"
hx-swap="outerHTML swap:1s">Delete</a>
Expand Down
7 changes: 0 additions & 7 deletions apis_bibsonomy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit af585ff

Please sign in to comment.