Skip to content

Commit

Permalink
Fix wrong swapping of info or reset responses
Browse files Browse the repository at this point in the history
  • Loading branch information
henhuy committed Nov 26, 2024
1 parent d46da86 commit 4499e9e
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions building_dialouge_webapp/heat/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ def __init__(self, html: str) -> None:
super().__init__(html)


class HTMLResetStateResponse(StateResponse):
"""State response holding HTML in order to reset page content."""

def __init__(self, html: str) -> None:
super().__init__(html)
class SwapHTMLStateResponse(HTMLStateResponse):
"""State response holding HTML to be swapped into other target."""


class RedirectStateResponse(StateResponse):
Expand Down Expand Up @@ -239,7 +236,7 @@ def response(self) -> dict[str, StateResponse]:
def reset_response(self) -> dict[str, StateResponse]:
"""Return HTML including HTMX swap-oob in order to remove/reset HTML for current target."""
return {
self.name: HTMLResetStateResponse(
self.name: SwapHTMLStateResponse(
f'<div id="{self.name}" hx-swap-oob="innerHTML"></div>',
),
}
Expand Down Expand Up @@ -355,7 +352,7 @@ def __init__( # noqa: PLR0913
@property
def response(self) -> dict[str, StateResponse]:
form_response = {
"info": HTMLStateResponse(
"_info": SwapHTMLStateResponse(
f'<div id="info" hx-swap-oob="innerHTML">{self.info_text}</div>',
),
}
Expand All @@ -365,7 +362,7 @@ def response(self) -> dict[str, StateResponse]:
@property
def reset_response(self) -> dict[str, StateResponse]:
form_response = {
"info": HTMLStateResponse('<div id="info" hx-swap-oob="innerHTML"></div>'),
"_info": SwapHTMLStateResponse('<div id="info" hx-swap-oob="innerHTML"></div>'),
}
form_response.update(**super().reset_response)
return form_response
Expand Down Expand Up @@ -457,9 +454,15 @@ def dispatch(self, request, *args, **kwargs) -> HttpResponse:
# Merge reset responses
target = None
html_response = ""
for name, state_response in state_partials.items():
ordered_partials = dict(
sorted(state_partials.items(), key=lambda item: isinstance(item[1], SwapHTMLStateResponse)),
)
for name, state_response in ordered_partials.items():
html_response += state_response.content
if isinstance(state_response, HTMLStateResponse):
if isinstance(state_response, HTMLStateResponse) and not isinstance(
state_response,
SwapHTMLStateResponse,
):
target = name
response = HttpResponse(html_response, content_type="text/html")
return retarget(response, f"#{target}")
Expand All @@ -470,7 +473,7 @@ def dispatch(self, request, *args, **kwargs) -> HttpResponse:
state_partials = {
name: response
for name, response in state_partials.items()
if not isinstance(response, (HTMLResetStateResponse, RedirectStateResponse))
if not isinstance(response, (SwapHTMLStateResponse, RedirectStateResponse))
}
context.update(state_partials)
# Fill template with state partials by adding them with their target_id
Expand Down

0 comments on commit 4499e9e

Please sign in to comment.