Skip to content

Commit

Permalink
Make params arg of trigger_client_event() optional
Browse files Browse the repository at this point in the history
  • Loading branch information
cjtapper committed Sep 1, 2022
1 parent 132c3c4 commit fe64ddd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
History
=======

* Make the ``params`` argument of ``trigger_client_event()`` optional.

Thanks to Chris Tapper in `PR #263 <https://github.com/adamchainz/django-htmx/pull/263>`__.

* Add ``django_htmx.http.push_url()`` for setting the browser location.

Thanks to Chris Tapper in `PR #264 <https://github.com/adamchainz/django-htmx/pull/264>`__.
Expand Down
2 changes: 1 addition & 1 deletion docs/http.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ HTTP
.. autofunction:: trigger_client_event

Modify the |HX-Trigger headers|__ of ``response`` to trigger client-side events, and return the response.
Takes the name of the event to trigger and any JSON-compatible parameters for it, and stores them in the appropriate header. Uses |DjangoJSONEncoder|__ for its extended data type support.
Takes the name of the event to trigger and (optionally) any JSON-compatible parameters for it, and stores them in the appropriate header. Uses |DjangoJSONEncoder|__ for its extended data type support.

.. |HX-Trigger headers| replace:: ``HX-Trigger`` headers
__ https://htmx.org/headers/hx-trigger/
Expand Down
4 changes: 3 additions & 1 deletion src/django_htmx/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ def push_url(response: _R, url: str | Literal[False]) -> _R:
def trigger_client_event(
response: _R,
name: str,
params: dict[str, Any],
params: dict[str, Any] | None = None,
*,
after: EventAfterType = "receive",
) -> _R:
params = params or {}

if after == "receive":
header = "HX-Trigger"
elif after == "settle":
Expand Down
8 changes: 8 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ def test_success(self):
response["HX-Trigger"] == '{"showConfetti": {"colours": ["purple", "red"]}}'
)

def test_success_no_params(self):
response = HttpResponse()

result = trigger_client_event(response, "showConfetti")

assert result is response
assert response["HX-Trigger"] == '{"showConfetti": {}}'

def test_success_streaming(self):
response = StreamingHttpResponse(iter((b"hello",)))

Expand Down

0 comments on commit fe64ddd

Please sign in to comment.