Issue 431 cancel ticket htmx toast - #437
Open
egorvaganov wants to merge 3 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #431 — Show the ticket cancellation confirmation immediately, without a page reload
Problem
When a user cancels a single ticket from
/my_account/my_reservations/, theconfirmation message only appears after a manual page reload.
Root cause: the HTMX branch of
MyAccount.cancel_ticketqueued theconfirmation via
django.contrib.messagesbut returned an emptyHttpResponse(""). Since the cancel button useshx-swap="delete", no pagerender happens, so the queued message stays invisible until the next full
page load. The error branch (
HttpResponse("", status=400)) had the sameproblem.
Solution
Implements the pattern already documented in
GUIDELINES.md("HX-Trigger + django.messages") and recommended in
tests/PIEGES.mdfor thisexact class of problem, following the existing
HX-Triggerprecedent in thecrowdsapp:BaseBillet/views.py—cancel_ticket: both HTMX branches now drainthe queued messages with
get_messages(request)and ship them in theHX-Triggerresponse header as{"toast": {"items": [{"level": "...", "text": "..."}]}}.200→ htmx performs thehx-swap="delete"(the ticket rowdisappears) and the success toast shows immediately.
400→ htmx does not swap on 4xx (the row correctly stays, sincethe ticket was NOT cancelled) but still processes the
HX-Triggerheader, so the error toast shows immediately.
HttpResponseClientRedirect) is unchanged.BaseBillet/templates/reunion/partials/toasts.html: new global JSlistener for the
toastevent. It replicates the exact server-sideBootstrap toast markup of the same partial (same classes, colors,
aria attributes and
{% trans %}labels) and appends the toasts into theexisting
#toastContainer. Since the partial is included by all four basetemplates (
reunion/base.html,headless.html,blank_base.html,faire_festival/base.html), the listener is available on every page andreusable for any future HTMX partial that wants to display messages.
textContent(neverinnerHTML), labels go through|escapejs.json.dumps(defaultensure_ascii=True)produces pure ASCII, so no CR/LF can reach the header value.
window.__tibilletToastListener).{% trans %}labelsalready present in the partial — no
.pochanges needed.Tests
New file
tests/pytest/test_cancel_ticket_htmx_toast.py(3 tests, all green):200,HX-Triggerheader carries the success toastpayload, ticket is
CANCELEDin DB, cancellation email task queued once.400,HX-Triggercarries theerror toast, ticket stays
SCANNED, no email task.200+HX-Redirectto/my_account/my_reservations/, noHX-Trigger.